<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ArvinderKang.com &#187; Ruby</title>
	<atom:link href="http://arvinderkang.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://arvinderkang.com</link>
	<description>The world is my backyard.</description>
	<lastBuildDate>Thu, 29 Jul 2010 22:58:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Upload recipe using Net::SCP</title>
		<link>http://arvinderkang.com/2009/11/01/upload-recipe-using-netscp/</link>
		<comments>http://arvinderkang.com/2009/11/01/upload-recipe-using-netscp/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 23:11:58 +0000</pubDate>
		<dc:creator>Arvinder Kang</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[net-scp]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://arvinderkang.com/2009/11/01/upload-recipe-using-netscp/</guid>
		<description><![CDATA[Sharing a simple ruby recipe to automate uploads of static website, if rsync is giving you problems(like with my school web server). This script uses Net::SCP. Net::SCP is a pure-Ruby implementation of the SCP protocol. This operates over SSH (and &#8230; <a href="http://arvinderkang.com/2009/11/01/upload-recipe-using-netscp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sharing a simple ruby recipe to automate uploads of static website, if <a href="http://tatey.com/2009/10/29/simpler-deployment-for-jekyll-using-a-rakefile-and-rsync.html" title="Simpler deployment for Jekyll using a Rakefile and rsync">rsync</a> is giving you problems(like with my school web server). This script uses <a href="http://github.com/jamis/net-scp" title="jamis's net-scp at master - GitHub">Net::SCP</a>.</p>

<pre><code>Net::SCP is a pure-Ruby implementation of the SCP protocol. This operates over SSH (and requires the Net::SSH library), and allows files and directory trees to copied to and from a remote server.
</code></pre>

<p>This solution lies somewhere between manually uploading and fully automation using <a href="http://www.capify.org/" title="Capistrano">Capistrano</a> or <a href="http://rubyhitsquad.com/Vlad_the_Deployer.html" title="Vlad the Deployer">Vlad</a>.</p>

<script src="http://gist.github.com/223781.js"></script>
]]></content:encoded>
			<wfw:commentRss>http://arvinderkang.com/2009/11/01/upload-recipe-using-netscp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Thinking of PHP, Apache, Ruby, MySQL and Unicode</title>
		<link>http://arvinderkang.com/2009/02/27/thinking-of-php-apache-ruby-mysql-and-unicode/</link>
		<comments>http://arvinderkang.com/2009/02/27/thinking-of-php-apache-ruby-mysql-and-unicode/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 01:05:45 +0000</pubDate>
		<dc:creator>Arvinder Kang</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Unicode]]></category>

		<guid isPermaLink="false">http://arvinderkang.com/2009/02/27/thinking-of-php-apache-ruby-mysql-and-unicode/</guid>
		<description><![CDATA[In the last post, I mentioned meeting my old pal Tux. Now back to coniguring the machine. I will be using the test server for Drupal websites, Rails projects and possibly Lift projects. Also, I love Punjabi and I love &#8230; <a href="http://arvinderkang.com/2009/02/27/thinking-of-php-apache-ruby-mysql-and-unicode/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In the last post, I mentioned meeting my old pal Tux.</p>

<p>Now back to coniguring the machine. I will be using the test server for <a href="http://drupal.org/" title="drupal.org | Community plumbing">Drupal</a> websites, <a href="http://rubyonrails.org/" title="Ruby on Rails">Rails</a> projects and possibly <a href="http://liftweb.net/" title="Lift &ndash; The Simply Functional Web Framework &ndash; Home">Lift</a> projects. Also, I love <a href="http://en.wikipedia.org/wiki/Punjabi" title="Punjabi - Wikipedia, the free encyclopedia">Punjabi</a> and I love <a href="http://satluj.com/" title="Satluj | ਆਪਣੀ ਬੋਲੀ, ਆਪਣਾ ਮਾਣ!">Unicode</a>. So I&#8217;m going to try to make things as Unicode friendly as they can be.</p>

<p>Let&#8217;s start.</p>

<p><span id="more-179"></span>
People love internet, internet loves images, and images love <a href="http://www.boutell.com/gd/" title="GD Graphics Library">GD</a>.</p>

<pre><code>sudo aptitude search gd
</code></pre>

<p>It gave a numbers of options. I was confused between installing libgd or php5-gd. It turns out after giving</p>

<pre><code>sudo aptitude install php5-gd
</code></pre>

<p>all other gd libraries were installed as dependencies.</p>

<p>Restart Apache</p>

<pre><code>sudo /etc/init.d/apache2 restart
</code></pre>

<p><strong>Tuning PHP/MySQL</strong></p>

<pre><code>cd /etc/php5/apache2/
</code></pre>

<p>By default the installations comes without a php.ini file. Rename it to php.ini-orignal.</p>

<pre><code>sudo mv php.ini php.ini-orignal
</code></pre>

<p>Find recommended file</p>

<pre><code>locate php.ini-recommended
</code></pre>

<p>Found it in  /usr/share/doc/php5-common/examples/php.ini-recommended . Copy it to orignal php.ini localtion and rename it.</p>

<pre><code>sudo cp /usr/share/doc/php5-common/examples/php.ini-recommended ./php.ini
</code></pre>

<p>For drupal to work properly, change <code>session.save_handler = files</code> to <code>to session.save_handler = user</code></p>

<p>Restart Apache</p>

<pre><code>sudo /etc/init.d/apache2 restart
</code></pre>

<p><strong>MySQL fun</strong></p>

<p>Login into mysql as root. By default, root mysql account has no password</p>

<pre><code>mysql -u root
</code></pre>

<p>Lets change the mysql superuser using a hard to guess password.</p>

<pre><code>mysql&gt; SET PASSWORD FOR 'root'@'localhost' = PASSWORD('$a-hard-to-guess-password-here$');
Query OK, 0 rows affected (0.00 sec)

mysql&gt; flush privileges;
Query OK, 0 rows affected (0.00 sec)
</code></pre>

<p>Now lets look around</p>

<pre><code>show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mysql              | 
+--------------------+
2 rows in set (0.01 sec)

mysql&gt; use mysql;

mysql&gt; show tables;
</code></pre>

<p>There is a table name user. Aha! This is the one with mysql user list.</p>

<pre><code>mysql&gt; desc user;
</code></pre>

<p>Amoung other things, it did show me three important fields &#8211; Host, User, Password</p>

<p>Lets look into it. Lets filter our view to just these three.</p>

<pre><code>mysql&gt; select Host,User, Password from user;

+------------------------+------------------+---------------------------------
| Host                   | User             | Password                                  
+------------------------+------------------+---------------------------------
| localhost              | root             | *6F50F9BB1F8C9DD174B66A8087FD4FA 
| xxx.xxxx.olemiss.edu   | root             | *6F50F9BB1F8C9DD174B66A8087FD4FA 
| 127.0.0.1              | root             | *6F50F9BB1F8C9DD174B66A8087FD4FA 
| localhost              |                  |                                 
| xxx.xxxx.olemiss.edu   |                  |                                  
| localhost              | debian-sys-maint | *C9D6F50F9B087D174B66A8FD4B1F8FA
</code></pre>

<p>What? No information in user column? Then what kind of user is it. Aha! Thats guest user also called anonymous user. Means any user while on the server can log into localhost. Guests should be served Tea and biscuits, not a mysql database. So from now &#8220;O mighty guest, I banish thee from my MySQL database.&#8221;</p>

<pre><code>mysql&gt; delete from user where User='';
Query OK, 2 rows affected (0.00 sec)
</code></pre>

<p>Much better. Now I have only two users(4 listed, although root is the same user using different hostnames) and debian-sys-maint. I refuse to get super-paranoid about security for now and this is good enough. (Remeber I did set my root password while installation. If you have not, I strongly recommend you to.) Other thing you can do is make another user and grant limited privilges. Keep root user as a backup only with a super secure password, and do not list it in any application code.</p>

<p>Now lets identify mysql database encoding. Wrong encoding can mess up non-Roman characters.</p>

<pre><code>mysql&gt; status;
</code></pre>

<p>Shows latin1. We want default to utf8. There might be other recommended changes too. Like PHP uses php.ini for configurations, MySQL uses my.cnf. Exit mysql.</p>

<pre><code>mysql&gt; exit
locate my-large.cnf
cp /usr/share/doc/mysql-server-5.0/examples/my-large.cnf /etc/mysql/my.cnf
</code></pre>

<p>Adding following to my.cnf</p>

<pre><code>[client]
default-character-set = utf8

[mysqld]
skip-character-set-client-handshake
default-character-set = utf8
character-set-server = utf8
collation-server = utf8_general_ci
init-connect = SET NAMES utf8

[mysqldump]
default-character-set = utf8

[mysql]
default-character-set = utf8
</code></pre>

<p>Optimize my.cnf, but do not waste too much time here now. You can do it later. Keep moving. Save my.cnf and restart mysql.</p>

<p>Log back into mysql. Now lets create a test database and see how it goes.</p>

<pre><code>mysql&gt; create database dm_drupal;
Query OK, 1 row affected (0.00 sec)

mysql&gt; GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON dm_drupal.* TO 'someuser'@'localhost' IDENTIFIED BY 'somepassword';
Query OK, 0 rows affected (0.00 sec)
</code></pre>

<p>Before leaving, you need to flush, otherwise changes will start stinking and fill the room.</p>

<pre><code>flush privileges;

status;
</code></pre>

<p>The encoding is happily unicode.</p>

<p>Now a little MySQL benchmarking (Never fully trust benchmarks though. Remember there are lies, damned lies and benchmarks:))
Output shows 10000000 simple additions can be performed in 0.71 seconds</p>

<pre><code>mysql&gt; SELECT BENCHMARK(1000000,1+1);
+------------------------+
| BENCHMARK(1000000,1+1) |
+------------------------+
|                      0 | 
+------------------------+
1 row in set (0.08 sec)

mysql&gt; SELECT BENCHMARK(10000000,1+1);
+-------------------------+
| BENCHMARK(10000000,1+1) |
+-------------------------+
|                       0 | 
+-------------------------+
1 row in set (0.71 sec)
</code></pre>

<p>I&#8217;m not a testing-for-a-living-guy, however what this seems to me is that my database is pretty decently configured.</p>

<p><strong>Setting up Virtual Hosts</strong></p>

<p>I&#8217;m going to test the setting using a test installation of Drupal.  First I&#8217;m going to create a virtual named host for drupal.</p>

<pre><code>cd  /etc/apache2/sites-available/
cp default drupal
</code></pre>

<p>Modify drupal to point to where you want to keep your installs (by default in <code>/var/www/</code>). I am keeping my sites in my home directory in <code>~/Sites</code> directory</p>

<pre><code>NameVirtualHost *
&lt;VirtualHost *&gt;
    ServerAdmin me@myself.com 

    DocumentRoot /home/myname/Sites/drupal/ 

    &lt;Directory /home/myname/Sites/drupal&gt;
        Options Indexes FollowSymLinks MultiViews
        # AllowOverride None
        AllowOverride All 
        Order allow,deny
        allow from all
    &lt;/Directory&gt;

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined
    ServerSignature On

&lt;/VirtualHost&gt;
</code></pre>

<p>Save and restart Apache.</p>

<p>To start with, I&#8217;m going to test a Drupal installation. Download drupal in your Sites folder</p>

<pre><code>cd ~/Sites
wget http://ftp.drupal.org/files/projects/drupal-6.10.tar.gz
tar -xvzf drupal-6.10.tar.gz
mv drupal-6.10 to drupal
</code></pre>

<p>Copy settings.php in director</p>

<pre><code>cp drupal/sites/default.settings.php drupal/sites/settings.php

chmod a+w drupal/sites/settings.php
</code></pre>

<p>We are ready. Point your browser to <code>http://localhost</code> to start the install process.</p>

<p><strong>Enabling clean URLs</strong></p>

<p>Okay now I found out apache was missing the mod-rewrite, required for clean URL&#8217;s. To enable I typed</p>

<pre><code>sudo a2enmod rewrite
sudo /etc/init.d/apache2 force-reload
apache2ctl -M
</code></pre>

<p>The last command showed mod-rewrite was enables, however the drupal installation still could not make clean URLs. After some Googling and reading the forums, I found the solution.</p>

<p>Disable the site by</p>

<pre><code>sudo a2dissite drupal 
vi /etc/apache2/sties-available/thedmonline
</code></pre>

<p>find AllowOverride and change  <code>AllowOverride None</code> to  <code>AllowOverride All</code> at four places.</p>

<p>Now enable site again
    sudo a2ensite drupal
    sudo /etc/init.d/apache2 restart</p>

<p>Turn on the clean URL option in drupal.</p>

<p><strong>Ruby 1.9</strong></p>

<p>Ruby1.9 is out of beta. So is the <a href="http://www.pragprog.com/titles/ruby3/programming-ruby-1-9" title="The Pragmatic Bookshelf | Programming Ruby 1.9">Pragmatic Ruby book</a> (kind of).</p>

<p>So I rolled my sleeves up (That was a lie my friend, added to give drama&#8230; I was wearing a half sleeve t-shirt).</p>

<pre><code>wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p0.tar.gz
tar xvzf ruby-1.9.1-p0.tar.gz
cd ruby-1.9.1-p0
sudo aptitude install build-essential libssl-dev libreadline5-dev
./configure --prefix=/usr/local
make &amp;&amp; make install
</code></pre>

<p>Okay I rolled my (virtual)sleeves down. Lets test</p>

<pre><code>ruby -v # ruby 1.9.1p0 (2009-01-30 revision 21907) [powerpc64-linux]
</code></pre>

<p>The post has gone too long(Who cares, its for my own documentation). In the next post, we get to managing development using new content versioning kid around the block, <a href="http://git-scm.com/" title="Git - Fast Version Control System">git</a>. We&#8217;ll probe how to install multiple Drupal and Ruby sites using Git and how it can co-exist and interact with <a href="http://www.nongnu.org/cvs/" title="CVS - Open Source Version Control">CVS</a>/<a href="http://subversion.tigris.org/" title="subversion.tigris.org">SVN</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://arvinderkang.com/2009/02/27/thinking-of-php-apache-ruby-mysql-and-unicode/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Jukebox in a single line</title>
		<link>http://arvinderkang.com/2008/06/28/jukebox-in-a-single-line/</link>
		<comments>http://arvinderkang.com/2008/06/28/jukebox-in-a-single-line/#comments</comments>
		<pubDate>Sun, 29 Jun 2008 04:05:34 +0000</pubDate>
		<dc:creator>Arvinder Kang</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[jukebox]]></category>
		<category><![CDATA[line]]></category>
		<category><![CDATA[mp3 player]]></category>
		<category><![CDATA[one]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[single]]></category>

		<guid isPermaLink="false">http://arvinderkang.com/2008/06/29/jukebox-in-a-single-line/</guid>
		<description><![CDATA[If you are like me, who starts starts iTunes with default library and realize you have heard the same song 8 times in the last week, here are two one-line-Jukebox scripts for you. The scripts randomly choose and play songs &#8230; <a href="http://arvinderkang.com/2008/06/28/jukebox-in-a-single-line/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you are like me, who starts starts iTunes with default library and  realize you have heard the same song 8 times in the last week, here are two one-line-Jukebox scripts for you. The scripts randomly choose and play songs right from the command line.</p>

<p>Before using the script make sure you have an mp3 player installed. <a href="http://arvinderkang.com/2008/06/28/installing-command-line-mp3-player/" title="Installing command line MP3 player | ArvinderKang.com">I have installed lightweight mpg123</a>, however you can use any player. I would recommend <a href="http://www.mplayerhq.hu/" title="Entering MPlayer homepage">mplayer</a> for Linux.</p>

<p>While running the scripts, I pass ~/Music folder argument. Change the path to point to folder where you store your mp3s.</p>

<p>First one is in <a href="http://www.ruby-lang.org/" title="Ruby Programming Language">Ruby</a>, and shamelessly inspired by <a href="http://www.amazon.com/Ruby-Example-Concepts-Kevin-Baird/dp/1593271484" title="Amazon.com: Ruby by Example: Concepts and Code: Kevin Baird: Books">Kevin Baird&#8217;s book</a>.</p>

<p>Open a text editor and save this script. I named it shuffle_play.rb. (You need to have ruby installed on your system. Installed on Leopard and most Linux distributions by default.)</p>

<pre><code>#!/usr/bin/env ruby -w
ARGV.sort_by{ rand }.each {|file| system("mpg123 \"#{file}\"")}
</code></pre>

<p>To run the jukebox</p>

<pre><code>ruby shuffle_play.rb ~/Music/*.mp3
</code></pre>

<p>For the second, I ported above  script to bash, which is even more simple and can run on any Linux or OS X. Lets name the script shuffle&#95;play&#95;shell.sh</p>

<pre><code>#!/usr/bin/env bash
n=0;for songs in "$@" ; do song_array[n]=$songs ;((n++)) ; done; for ((i=0;i&lt;$n;i+=1));do number=$RANDOM; let "number %= $n"; `mpg123 "${song_array[number]}"`; done
</code></pre>

<p>To run the jukebox</p>

<pre><code>source shuffle_play_shell.sh ~/Music/*.mp3
</code></pre>

<p><em>Update</em>: The second one is not completely random. It will choose n random songs out of n songs. However it does not checks if each song is played at-least once, thus chances of songs getting repeated. Any suggestions?</p>
]]></content:encoded>
			<wfw:commentRss>http://arvinderkang.com/2008/06/28/jukebox-in-a-single-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->