<?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>Geek Rant Zone</title>
	<atom:link href="http://dhruvasagar.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://dhruvasagar.net</link>
	<description>A place where I go on and on without any rhyme or reason</description>
	<lastBuildDate>Wed, 10 Aug 2011 00:20:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Kotak Mahindra Bank &#8211; Epic Fail &#8211; Part 2</title>
		<link>http://dhruvasagar.net/2011/04/04/kotak-mahindra-bank-epic-fail-part-2/</link>
		<comments>http://dhruvasagar.net/2011/04/04/kotak-mahindra-bank-epic-fail-part-2/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 06:10:12 +0000</pubDate>
		<dc:creator>Dhruva Sagar</dc:creator>
				<category><![CDATA[Fail]]></category>
		<category><![CDATA[Bank]]></category>
		<category><![CDATA[Epic]]></category>
		<category><![CDATA[Kotak]]></category>
		<category><![CDATA[Mahindra]]></category>

		<guid isPermaLink="false">http://dhruvasagar.net/?p=26314</guid>
		<description><![CDATA[You might have read my previous article &#8211; Kotak Mahindra Bank &#8211; Epik Fail about my misfortune that my company had to go with Kotak Mahindra Bank for providing Salary accounts. Today I will list out another reason why Kotak Mahindra Bank not only sucks but is the employees worst nightmare when it comes to [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2011%2F04%2F04%2Fkotak-mahindra-bank-epic-fail-part-2%2F' data-shr_title='Kotak+Mahindra+Bank+-+Epic+Fail+-+Part+2'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2011%2F04%2F04%2Fkotak-mahindra-bank-epic-fail-part-2%2F' data-shr_title='Kotak+Mahindra+Bank+-+Epic+Fail+-+Part+2'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p>You might have read my previous article &#8211; <a href="http://dhruvasagar.net/2010/11/04/kotak-mahindra-bank-epic-fail/" title="Kotak Mahindra Bank - Epic Fail">Kotak Mahindra Bank &#8211; Epik Fail</a> about my misfortune that my company had to go with Kotak Mahindra Bank for providing Salary accounts. Today I will list out another reason why Kotak Mahindra Bank not only sucks but is the employees worst nightmare when it comes to having a salary account with them.</p>
<p>Apparently at Kotak Mahindra Bank, the salary transactions for employees of a company (at least a small company like ours) are carried out manually (yes, you heard me right, manually!). Now you know what that means, that means that if it is a Bank Holiday, or a weekend, or a World Cup Final, or if your badass luck isn&#8217;t already bad enough that their employees are on sick leave, you won&#8217;t get salary!!! Yes, that&#8217;s right, forget salary for as long as it takes, you will have to call them, go to their bank and ask them time and again before the salary is actually credited, which could easily be at least a week long effort, and they will give you reasons like it was a holiday saaar, their staff was on sick leave saaar, it was the world cup final saaar&#8230;</p>
<p>It is 4th April today and no I haven&#8217;t received salary, for most of the reasons stated above. It might happen tomorrow, or maybe day after tomorrow, who knows ?</p>
<p>All you can do is pull your hair and curse them on the top of your voice making yourself look like a lunatic. I am not sure how long I can take this shit from them. It is now an undeniable &#038; undisputed fact that Kotak Mahindra Bank is the most ridiculously worthless piece of shit of a bank there ever could possibly be, and worse.</p>
<p>At Kotak Mahindra Bank every day is an April Fool&#8217;s Day, for their customers. Sometimes there is money, sometimes there isn&#8217;t, keep guessing!</p>
<p>PS: To Kotak Mahindra Bank employees, if you are reading this, don&#8217;t even think about commenting on my blog.</p>
<div class="shr-publisher-26314"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://dhruvasagar.net/2011/04/04/kotak-mahindra-bank-epic-fail-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting started with Ruby &#8211; TOC</title>
		<link>http://dhruvasagar.net/2010/12/24/getting-started-with-ruby-toc/</link>
		<comments>http://dhruvasagar.net/2010/12/24/getting-started-with-ruby-toc/#comments</comments>
		<pubDate>Fri, 24 Dec 2010 08:04:33 +0000</pubDate>
		<dc:creator>Dhruva Sagar</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[toc]]></category>

		<guid isPermaLink="false">http://dhruvasagar.net/?p=20768</guid>
		<description><![CDATA[Hi, I have written a series of articles (in several parts) in an attempt to guide beginners to get comfortable with Ruby. This article is my attempt to build a TOC (Table of Contents) for it to act as an index for people to get a brief idea regarding what each article is about and [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F12%2F24%2Fgetting-started-with-ruby-toc%2F' data-shr_title='Getting+started+with+Ruby+-+TOC'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F12%2F24%2Fgetting-started-with-ruby-toc%2F' data-shr_title='Getting+started+with+Ruby+-+TOC'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Hi,</p>
<p>I have written a series of articles (in several parts) in an attempt to guide beginners to get comfortable with Ruby. This article is my attempt to build a TOC (Table of Contents) for it to act as an index for people to get a brief idea regarding what each article is about and help them get to the sections they are interested in.</p>
<p><span id="more-20768"></span></p>
<ul>
<li><a href="http://dhruvasagar.net/2010/10/19/getting-started-with-ruby-part1/">Getting started with Ruby &#8211; Part 1</a> : In this article I begin with an introduction to myself and my motivation behind writing these articles. I give an introduction to Ruby, about how to get started with building your development environment and introduce a few tools that ship along with Ruby which will make your life that much better.</li>
<li><a href="http://dhruvasagar.net/2010/10/19/getting-started-with-ruby-part2/">Getting started with Ruby &#8211; Part 2</a> : In this article I introduce Ruby in slightly more detail and I get started with describing different data types in Ruby. I cover Strings and Numbers in this article.</li>
<li><a href="http://dhruvasagar.net/2010/10/21/getting-started-with-ruby-part3/">Getting started with Ruby &#8211; Part 3</a> : In this article I continue from my previous article describing data types in Ruby and cover Ranges and Arrays.</li>
<li><a href="http://dhruvasagar.net/2010/10/23/getting-started-with-ruby-part4/">Getting started with Ruby &#8211; Part 4</a> : In this article I continue further and cover Hashes, one of the most commonly used data types in Ruby. I also introduce blocks and about using blocks with functions.</li>
<li><a href="http://dhruvasagar.net/2010/10/26/getting-started-with-ruby-part5/">Getting started with Ruby &#8211; Part 5</a> : In this article I move onto describing various looping constructs in Ruby, and also various flow control modifiers in Ruby. After that I introduce Variables, Symbols, Constants, Methods and Global Variables in Ruby.</li>
<li><a href="http://dhruvasagar.net/2010/11/03/getting-started-with-ruby-part6/">Getting started with Ruby &#8211; Part 6</a> : After having covered basics of various constructs in Ruby, I considered it better to take a problem statement and solve it in Ruby to bring most of what we&#8217;ve discussed into use. I chose Google Codejam 2009&#8242;s qualification round problem &#8211; <a href="http://code.google.com/codejam/contest/dashboard?c=90101">Alien Language</a></li>
</ul>
<p>I tried to be clear, precise and specific. My intention was write more code than to waste time in trying to explain and so I&#8217;ve used code comments to help people understand what each part of the code is meant to do. I have plans on going further and go onto describing classes &#038; modules, sometime soon, even though I expected more people to read and give their feedback, but owing to a lot of work and hence a lack of time, I haven&#8217;t been able to do much justice to the same, but hopefully I will be able to do so very soon. I hope it helps people appreciate Ruby and motivate them to learn and understand it.</p>
<div class="shr-publisher-20768"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://dhruvasagar.net/2010/12/24/getting-started-with-ruby-toc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MongoMapper Plugin &#8211; Versionable</title>
		<link>http://dhruvasagar.net/2010/12/02/mongomapper_plugin_versionable/</link>
		<comments>http://dhruvasagar.net/2010/12/02/mongomapper_plugin_versionable/#comments</comments>
		<pubDate>Thu, 02 Dec 2010 02:22:13 +0000</pubDate>
		<dc:creator>Dhruva Sagar</dc:creator>
				<category><![CDATA[MongoMapper]]></category>
		<category><![CDATA[mongomapper]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[versionable]]></category>

		<guid isPermaLink="false">http://dhruvasagar.net/?p=19920</guid>
		<description><![CDATA[There was a requirement at one of our projects here at Artha42 Innovations Private Limited that our Mongo Mapper documents have inherent versioning so as to be able to track the changes being made and also if required revert the changes to a previous state. Since we&#8217;re using MongoMapper, I set out to create a [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F12%2F02%2Fmongomapper_plugin_versionable%2F' data-shr_title='MongoMapper+Plugin+-+Versionable'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F12%2F02%2Fmongomapper_plugin_versionable%2F' data-shr_title='MongoMapper+Plugin+-+Versionable'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p>There was a requirement at one of our projects here at <a href="http://www.artha42.com/" title="Artha42 Innovations Private Limited">Artha42 Innovations Private Limited</a> that our Mongo Mapper documents have inherent versioning so as to be able to track the changes being made and also if required revert the changes to a previous state. Since we&#8217;re using MongoMapper, I set out to create a simple plugin for the same to add the required functionality.</p>
<p>Initially, from the initial research I did, I found <a href="http://shapado.com/" title="Shapado">Shapado</a> had an implementation of exactly what I wanted, however when I studied and understood their implementation in detail, I realized that it doesn&#8217;t really work that well in most scenarios, especially the ones we were tackling in our project. Shapado&#8217;s versionable allows you to define a set of keys for a document, whose changes would trigger creation of new versions. However in our project we needed to track changes at the complete document level, i.e. even for changes in the associations. Also the versions were embedded within the document itself, which implied that the document would grow in size very rapidly, even though our changes in each iteration could be minor, since each version would literally be a snapshot of the complete document itself.</p>
<p>So we made quite a few design changes in the same, externalized the versions, changed the way one would enable versioning since in our case we wouldn&#8217;t be keeping track of changes only in specific keys. Even here, we found that there were certain bottlenecks in the case if there were a whole lot of versions, for which we introduced the :limit feature which allows us to define how many of versions would need to be loaded instead of loading all of them at once.</p>
<p>The result of it was <a href="https://github.com/artha42/mm-versionable" title="MongoMapper Versionable Plugin">mm-versionable</a>.</p>
<p>If you have any comments, suggestions, requests, queries or feedback, please do let us know in the comments section below.</p>
<div class="shr-publisher-19920"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://dhruvasagar.net/2010/12/02/mongomapper_plugin_versionable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calculating Hashes</title>
		<link>http://dhruvasagar.net/2010/11/25/calculating-hashes/</link>
		<comments>http://dhruvasagar.net/2010/11/25/calculating-hashes/#comments</comments>
		<pubDate>Thu, 25 Nov 2010 04:38:29 +0000</pubDate>
		<dc:creator>Dhruva Sagar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[md5]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sha1]]></category>
		<category><![CDATA[sha256]]></category>
		<category><![CDATA[sha384]]></category>
		<category><![CDATA[sha512]]></category>

		<guid isPermaLink="false">http://dhruvasagar.net/?p=18889</guid>
		<description><![CDATA[In my old blog, 2 of the popular posts were about calculating MD5, SHA-1, SHA-256, SHA-384, SHA-512 hashes in Java and C#, I am still getting a little traffic from diggs of those posts :). So today I decided to post it again, and since I am now a fan of Ruby, i&#8217;ll also added [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F11%2F25%2Fcalculating-hashes%2F' data-shr_title='Calculating+Hashes'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F11%2F25%2Fcalculating-hashes%2F' data-shr_title='Calculating+Hashes'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p>In my old blog, 2 of the popular posts were about calculating MD5, SHA-1, SHA-256, SHA-384, SHA-512 hashes in Java and C#, I am still getting a little traffic from diggs of those posts :). So today I decided to post it again, and since I am now a fan of Ruby, i&#8217;ll also added that in.</p>
<p><span id="more-18889"></span></p>
<p>Java :</p>
<pre class="brush: java; title: ; notranslate">
public String getHash(String message, String algorithm) {
	try {
		byte[] buffer = message.getBytes();
		MessageDigest md = MessageDigest.getInstance(algorithm);
		md.update(buffer);
		byte[] digest = md.digest();
		String hexValue = null;
		for(int i = 0 ; i &amp;lt; digest.length ; i++) {
			int b = digest[i] &amp;amp; 0xff;
			if (Integer.toHexString(b).length() == 1) hex = hex + &quot;0&quot;;
			hex  = hex + Integer.toHexString(b);
		}
		return hex;
	} catch(NoSuchAlgorithmException e) {
		e.printStackTrace();
	}
	return null;
}
</pre>
<p>C# :</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Text;
using System.Security.Cryptography;

public String getHash(String message, String algo) {
	byte[] sourceBytes = Encoding.Default.GetBytes(message);
	byte[] hashBytes = null;
	Console.WriteLine(algo);
	switch(algo.Trim().ToUpper()) {
		case &quot;MD5&quot;:
			hashBytes = MD5CryptoServiceProvider.Create().ComputeHash(sourceBytes);
			break;
		case &quot;SHA1&quot;:
			hashBytes = SHA1Managed.Create().ComputeHash(sourceBytes);
			break;
		case &quot;SHA256&quot;:
			hashBytes = SHA256Managed.Create().ComputeHash(sourceBytes);
			break;
		case &quot;SHA384&quot;:
			hashBytes = SHA384Managed.Create().ComputeHash(sourceBytes);
			break;
		case &quot;SHA512&quot;:
			hashBytes = SHA512Managed.Create().ComputeHash(sourceBytes);
			break;
		default:
			break;
	}
	StringBuilder sb = new StringBuilder();
	for(int i = 0 ; hashBytes != null &amp;amp;&amp;amp; i &amp;lt; hashBytes.Length ; i++) {
		sb.AppendFormat(&quot;{0:x2}&quot;, hashBytes[i]);
	}
	return sb.ToString();
}
</pre>
<p>Ruby:</p>
<pre class="brush: ruby; title: ; notranslate">
require 'digest'

def getHash(message, algo='md5')
  case algo.strip.upcase
  when &quot;MD5&quot; then Digest::MD5.hexdigest(message)
  when &quot;SHA1&quot; then Digest::SHA1.hexdigest(message)
  when &quot;SHA256&quot; then Digest::SHA256.hexdigest(message)
  when &quot;SHA384&quot; then Digest::SHA384.hexdigest(message)
  when &quot;SHA512&quot; then Digest::SHA512.hexdigest(message)
  end
end
</pre>
<p>As you can see, the Ruby version is the most concise &#038; simple.</p>
<div class="shr-publisher-18889"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://dhruvasagar.net/2010/11/25/calculating-hashes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kotak Mahindra Bank &#8211; Epic Fail</title>
		<link>http://dhruvasagar.net/2010/11/04/kotak-mahindra-bank-epic-fail/</link>
		<comments>http://dhruvasagar.net/2010/11/04/kotak-mahindra-bank-epic-fail/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 04:42:25 +0000</pubDate>
		<dc:creator>Dhruva Sagar</dc:creator>
				<category><![CDATA[Fail]]></category>
		<category><![CDATA[Bank]]></category>
		<category><![CDATA[Epic]]></category>
		<category><![CDATA[Kotak]]></category>
		<category><![CDATA[Mahindra]]></category>

		<guid isPermaLink="false">http://dhruvasagar.net/?p=17116</guid>
		<description><![CDATA[I will now narrate 2 separate events that have happened with me, within the 2 months of my having a savings account with Kotak Mahindra Bank, which to me are both EPIC Failures! 27th October morning, as I leave my house for office, I realize I am running low on cash, so I decide to [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F11%2F04%2Fkotak-mahindra-bank-epic-fail%2F' data-shr_title='Kotak+Mahindra+Bank+-+Epic+Fail'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F11%2F04%2Fkotak-mahindra-bank-epic-fail%2F' data-shr_title='Kotak+Mahindra+Bank+-+Epic+Fail'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I will now narrate 2 separate events that have happened with me, within the 2 months of my having a savings account with Kotak Mahindra Bank, which to me are both <strong>EPIC Failures</strong>!</p>
<p><span id="more-17116"></span></p>
<ol>
<li>27th October morning, as I leave my house for office, I realize I am running low on cash, so I decide to go to the Corporation Bank ATM which is the nearest ATM from my residence. I have had an HDFC Bank account for over 2 years and have been using my HDFC Debit Card in this Corporation Bank ATM for nearly 6 months since it came up.So I get to the ATM, decide to use my new Kotak Mahindra Bank &#8211; Debit card for the first time to withdraw some cash. I go through the complete process in my attempt to withdraw the amount, only to be disappointed in the end by my transaction being declined with a weird error &#8211; Issuer Unavailable. Although a little confused, but running short of patience and time, I decide to well go ahead and use my HDFC Debit Card instead, only to be disappointed again to see the same error.
<p>Although a little irritated, I wasted no time and continued to my office. As I reach my office and settle down, I see in my mail box a notification email from the Kotak Mahindra Bank with the subject <strong>Transaction Success Alert &#8211; ATM Cash Withdrawal</strong>. I quickly opened the email and to add to my agony of the utter waste of time at the ATM this morning, Kotak Mahindra had actually debited the amount that I had tried to withdraw from my bank account!</p>
<p>Not a good start to the day, I thought, I quickly opened their website (www.kotak.com), got their customer care number and called them up. After a few key presses I was finally able to get to a customer representative on the other end. I narrated the entire happening once again, requested them to lodge a complain, which they did. However, as if the day hadn&#8217;t already had a rough beginning, to my utter disgust &amp; surprise, the customer representative told me that the process of reversal of the transaction would take <strong>upto 12 working days</strong>! I am now <strong>NEVER</strong> going to use my Kotak Mahindra Bank Debit Card in an ATM. <strong>NEVER EVER!</strong></li>
<li>As if the previous days happenings weren&#8217;t enough to convince me how much of a <strong>FAILURE</strong>,<strong> Kotak Mahindra Bank</strong> is, on 28th October I had yet another rather interesting event and this time, it does not involve an ATM or a fake transaction.
<p>I am a music lover, and enjoy playing the guitar. I love the finger picking style of guitar playing and am often exploring the web trying to get my hands on nice soothing arrangements which are easy enough for me to try. I happened to come across <a href="http://www.kellyvalleau.com/">Kelly Valleau&#8217;s</a> video <a href="http://www.youtube.com/watch?v=5sLH1MeXnUY">Scientist &#8211; Coldplay</a> and I was excited to see that he offered the tabs / scores to his arrangements on his website. I quickly went over to his website, checked out a few more of his videos and was quite excited.</p>
<p>After a little while, I decided that subscribing to his site would be totally worth it. I clicked on the subscribe button on his site, which took me to paypal, I logged in with my paypal account and I decided to use my Kotak Mahindra Bank Debit Card this time. I added my Debit Card details into Paypal and tried to go through with the transaction, but not to my surprise, the card was declined. I tried a couple of more times, with some hope, but gave up soon and after quite a few curse words to Kotak Mahindra Bank and a few laughs, I just let it go and continued exploring youtube.</p>
<p>After a while, when I check my phone, I see that I received an SMS Alert from Kotak Mahindra Bank. And I swear to GOD this is what it read :</p>
<blockquote><p>Your transaction at PAYPAL, SG using Kotak Debit Card XXXX on 28th-OCT-2010 XX:XX:XX IST could not be processed due to Card not swiped properly. The transaction reference No is XXXXXXXXXXX. Swipe the card properly and try again. .</p></blockquote>
<p>Since I had tried a couple of times, I had a couple of those SMS&#8217;s.</li>
</ol>
<p>Now that my friends are both <strong>EPIC Failures</strong>! As of now I am stuck with Kotak Mahindra Bank, and since I have used it barely in the past 2 months since I hold an account, I am going to give them a little more time (for more <strong>EPIC Fails</strong>).</p>
<div class="shr-publisher-17116"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://dhruvasagar.net/2010/11/04/kotak-mahindra-bank-epic-fail/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Getting started with Ruby &#8211; Part 6 &#8211; Example Problem</title>
		<link>http://dhruvasagar.net/2010/11/03/getting-started-with-ruby-part6/</link>
		<comments>http://dhruvasagar.net/2010/11/03/getting-started-with-ruby-part6/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 07:54:12 +0000</pubDate>
		<dc:creator>Dhruva Sagar</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[alien]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://dhruvasagar.net/?p=16959</guid>
		<description><![CDATA[After having covered most of the basics of Ruby, lets get started on an actual problem, just like I had promised in my previous article &#8211; Getting started with Ruby – Part 5 I am taking a relatively simple problem that came in Google Codejam 2009 qualification round. Here is the direct link to the [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F11%2F03%2Fgetting-started-with-ruby-part6%2F' data-shr_title='Getting+started+with+Ruby+-+Part+6+-+Example+Problem'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F11%2F03%2Fgetting-started-with-ruby-part6%2F' data-shr_title='Getting+started+with+Ruby+-+Part+6+-+Example+Problem'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p>After having covered most of the basics of Ruby, lets get started on an actual problem, just like I had promised in my previous article &#8211; <a href="http://dhruvasagar.net/2010/10/26/getting-started-with-ruby-part5/" title="Getting started with Ruby - Part 5">Getting started with Ruby – Part 5</a> </p>
<p><span id="more-16959"></span></p>
<p>I am taking a relatively simple problem that came in <a href="http://code.google.com/codejam/" title="Google Codejam">Google Codejam</a> 2009 qualification round. Here is the direct link to the problem &#8211; <a href="http://code.google.com/codejam/contest/dashboard?c=90101" title="Alien Language">Alient Language</a></p>
<blockquote><p>
After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also, there are exactly D words in this language.</p>
<p>Once the dictionary of all the words in the alien language was built, the next breakthrough was to discover that the aliens have been transmitting messages to Earth for the past decade. Unfortunately, these signals are weakened due to the distance between our two planets and some of the words may be misinterpreted. In order to help them decipher these messages, the scientists have asked you to devise an algorithm that will determine the number of possible interpretations for a given pattern.</p>
<p>A pattern consists of exactly L tokens. Each token is either a single lowercase letter (the scientists are very sure that this is the letter) or a group of unique lowercase letters surrounded by parenthesis ( and ). For example: (ab)d(dc) means the first letter is either a or b, the second letter is definitely d and the last letter is either d or c. Therefore, the pattern (ab)d(dc) can stand for either one of these 4 possibilities: add, adc, bdd, bdc.</p>
<p><strong>Input</strong></p>
<p>The first line of input contains 3 integers, L, D and N separated by a space. D lines follow, each containing one word of length L. These are the words that are known to exist in the alien language. N test cases then follow, each on its own line and each consisting of a pattern as described above. You may assume that all known words provided are unique.</p>
<p><strong>Output</strong></p>
<p>For each test case, output</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
Case #X: K
</pre>
<p>where X is the test case number, starting from 1, and K indicates how many words in the alien language match the pattern.</p>
<p>Limits</p>
<p>Small dataset</p>
<p>1 ≤ L ≤ 10<br />
1 ≤ D ≤ 25<br />
1 ≤ N ≤ 10<br />
Large dataset</p>
<p>1 ≤ L ≤ 15<br />
1 ≤ D ≤ 5000<br />
1 ≤ N ≤ 500</p>
<p>Sample</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
Input				Output

3 5 4				Case #1: 2
abc					Case #2: 1
abc					Case #3: 3
bca					Case #4: 0
dac
dbc
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc
</pre>
</blockquote>
<p>If you look at the problem, it&#8217;s pretty simple to comprehend. If you have done some reasonable amount of programming, and you have come across Regular Expressions, you should be easily able to identify the pattern here that we need to apply. I am sure there might be other solutions, but here I demonstrate my solution. I am not going to dive into regular expressions here, I would recommend you to do your own research on it if you don&#8217;t know them well. So here is our solution for the problem :</p>
<pre class="brush: ruby; title: ; notranslate">
L, D, N = gets.split.map{|i| i.to_i} #gets is a function that returns the input from the console as a string. Here we then call the String#split function which splits the string delimited by a space by default and will return an array of the values. However it will be an array of strings, and since we need integers, we can simply call the String#map OR String#collect function which will loop through the array and execute the block on each and return a new array with it. In this case since we call to String#to_i method, the map returns an array of 3 integers, which gets assigned to L, D, N and since we know the length of the array is 3, each L, D, N gets assigned to each element within the array.

words = []

#To fetch D words each of length L.
D.times do |i|
  words[i] = gets #Here we will get the words that exist in the alien language
end

#To fetch N test cases.
N.times do |i|
  r = Regexp.new(gets.gsub('(','[').gsub(')',']').to_s) #Here we will get the test cases and convert them to a Regular Expression. The String#gsub method is used to substitute a pattern with a given input within the string and it returns a new string object with the substituted value.
  count = 0
  words.each do |w|
    count+=1 if r.match(w) #Alternatively you could also use the syntax w =~ r, which practically does the same thing.
  end
  puts &quot;Case ##{i+1}: #{count}\n&quot;
end
</pre>
<pre class="brush: bash; title: ; notranslate">
#In order to execute the above code, save the code within a file say alien.rb. You can execute the program by calling :
ruby alien.rb
#Then you can input the sample test content manually. Although a better approach would be to save your sample input within an input file, say sample_input.txt and use redirection to pass it to your program, example :
ruby alien.rb &lt; sample_input.txt
#The output in our program is output onto the console. If you wish to save that in a file, we can again use redirection to save the output in a file, example:
ruby alien.rb &lt; sample_input.txt &gt; output.txt
</pre>
<p>The above solution will help you realize how simple &#038; intuitive it is to code in Ruby :). I hope you liked this article. I will be covering Modules &#038; Classes from next part onwards so do <a href="http://feeds2.feedburner.com/GeekRantZone">Subscribe</a> to my blog to stay up-to date with my articles. If you have any queries, suggestions or feedback please let me know in the comments below.</p>
<div class="shr-publisher-16959"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://dhruvasagar.net/2010/11/03/getting-started-with-ruby-part6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting started with Ruby &#8211; Part 5 &#8211; Ruby Loops</title>
		<link>http://dhruvasagar.net/2010/10/26/getting-started-with-ruby-part5/</link>
		<comments>http://dhruvasagar.net/2010/10/26/getting-started-with-ruby-part5/#comments</comments>
		<pubDate>Tue, 26 Oct 2010 15:38:53 +0000</pubDate>
		<dc:creator>Dhruva Sagar</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[all?]]></category>
		<category><![CDATA[any?]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[break]]></category>
		<category><![CDATA[collect]]></category>
		<category><![CDATA[constant]]></category>
		<category><![CDATA[constants]]></category>
		<category><![CDATA[detect]]></category>
		<category><![CDATA[each]]></category>
		<category><![CDATA[each_cons]]></category>
		<category><![CDATA[each_slice]]></category>
		<category><![CDATA[each_with_index]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[find_all]]></category>
		<category><![CDATA[first]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[global variables]]></category>
		<category><![CDATA[inject]]></category>
		<category><![CDATA[last]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[max]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[methods]]></category>
		<category><![CDATA[min]]></category>
		<category><![CDATA[next]]></category>
		<category><![CDATA[redo]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[step]]></category>
		<category><![CDATA[symbol]]></category>
		<category><![CDATA[symbols]]></category>
		<category><![CDATA[times]]></category>
		<category><![CDATA[until]]></category>
		<category><![CDATA[upto]]></category>
		<category><![CDATA[variable]]></category>
		<category><![CDATA[variables]]></category>
		<category><![CDATA[while]]></category>

		<guid isPermaLink="false">http://dhruvasagar.net/?p=15737</guid>
		<description><![CDATA[Hi in my previous article Getting started with Ruby &#8211; Part 4 I covered Hashes &#38; blocks. Today I will take you through the various forms of loops in Ruby. While &#8211; executes code as long as the given condition is true. We can separate the code from the condition using either the keyword do, [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F10%2F26%2Fgetting-started-with-ruby-part5%2F' data-shr_title='Getting+started+with+Ruby+-+Part+5+-+Ruby+Loops'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F10%2F26%2Fgetting-started-with-ruby-part5%2F' data-shr_title='Getting+started+with+Ruby+-+Part+5+-+Ruby+Loops'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Hi in my previous article <a title="Getting started with Ruby - Part 4" href="http://dhruvasagar.net/2010/10/23/getting-started-with-ruby-part4/" target="_self">Getting started with Ruby &#8211; Part 4</a> I covered Hashes &amp; blocks. Today I will take you through the various forms of loops in Ruby.</p>
<p><span id="more-15737"></span></p>
<ul>
<li>While &#8211; executes <em>code</em> as long as the given condition is true. We can separate the code from the condition using either the keyword <strong>do</strong>, a newline or a semi-colon (;)
<pre class="brush: ruby; title: ; notranslate">
	i = 0
	while i &lt; 5 do
		puts &quot;Iteration #{i}&quot;
		i += 1
	end

	#Output :
	#Iteration 0
	#Iteration 1
	#Iteration 2
	#Iteration 3
	#Iteration 4

	#Alternatively, it is also possible to use the while loop as a modifier towards the end of a statement or a code block
	i = 0
	begin
		puts &quot;Iteration #{i}&quot;
		i += 1
	end while i &lt; 5

	#Output :
	#Iteration 0
	#Iteration 1
	#Iteration 2
	#Iteration 3
	#Iteration 4
	</pre>
</li>
<li>Until &#8211; is very similar to the while loop with respect to the syntax &amp; usage, the only difference between the two is the fact that the while loop executes the code block as long as the condition is true, the until loop does exactly the opposite, i.e. it executes the code block as long as the condition is false.
<pre class="brush: ruby; title: ; notranslate">
	until i &gt; 4 do
		puts &quot;Iteration #{i}&quot;
		i += 1
	end

	#Output :
	#Iteration 0
	#Iteration 1
	#Iteration 2
	#Iteration 3
	#Iteration 4

	#Again we also have the alternate syntax where we can use until as a modified at the end of a statement or a code block
	begin
		puts &quot;Iteration #{i}&quot;
		i += 1
	end until i &gt; 4

	#Output :
	#Iteration 0
	#Iteration 1
	#Iteration 2
	#Iteration 3
	#Iteration 4
	</pre>
</li>
<li>For &#8211; executes a block of code once for each element in the expression / object passed to it. Again as in the case of while &amp; until above, the block of code may be separated from the expression by either using the keyword do, a newline or a semi-colon (;)
<pre class="brush: ruby; title: ; notranslate">
	array = 1,2,4,5
	for i in array
		puts &quot;Array element #{i}&quot;
	end

	#Output :
	#Array element 1
	#Array element 2
	#Array element 4
	#Array element 5

	#OR

	for i in 0...10 do #Remember '...' creates a range which excludes the last element from the range.
		puts &quot;Iteration #{i}&quot;
	end

	#Output :
	#Iteration 0
	#Iteration 1
	#Iteration 2
	#Iteration 3
	#Iteration 4
	#Iteration 5
	#Iteration 6
	#Iteration 7
	#Iteration 8
	#Iteration 9
	</pre>
</li>
<li> These are some of the other loops which are available through fixnums
<pre class="brush: ruby; title: ; notranslate">
	10.times {|i| p i}

	#Output :
	#0
	#1
	#2
	#3
	#4
	#5
	#6
	#7
	#8
	#9

	1.upto(10) {|i| p i}

	#Output :
	#1
	#2
	#3
	#4
	#5
	#6
	#7
	#8
	#9
	#10

	1.step(10, 2) {|i| p i}

	#Output :
	#0
	#2
	#4
	#6
	#8
	#10
</pre>
</li>
<li> Collections provide various different &#038; interesting forms of looping mechanisms.
<pre class="brush: ruby; title: ; notranslate">
	array.each do |i|
		puts &quot;Array element #{i}&quot;
	end

	#Output :
	#Array element 1
	#Array element 2
	#Array element 4
	#Array element 5

	array.each_with_index do |value, index|
		puts &quot;array[#{index}] = #{value}&quot;
	end

	#Output :
	#array[0] = 1
	#array[1] = 2
	#array[2] = 4
	#array[3] = 5

	array.collect {|i| i.to_s} #=&gt; ['1', '2', '4', '5']
	array.map #same as collect

	array.find {|i| i == 1} #=&gt; 1
	array.find {|i| i == 3} #=&gt; nil
	array.find('Doesn\'t Exist!') {|i| i == 3} #=&gt; &quot;Doesn't Exist!&quot;
	array.detect #same as find

	array.select {|i| i == 1} #=&gt; [1]
	array.find_all #same as select

	array.all? {|i| i.is_a?(Integer)} #=&gt; true
	#Loops through all the elements and executes the block for each, it returns true if the block returned true for all elements, otherwise it returns false.

	array.any?{|i| i == 1} #=&gt; true
	#Loops through all the elements and executes the block for each, it returns true if any of the elements return true for the block, if all of them are false, it would return false.

	array.each_cons(3) {|i| p i}
	#Output :
	#[1, 2, 4]
	#[2, 4, 5]
	#each_cons(n) takes 'n' consecutive elements from the array and goes from the start of the array till the end with a sub array start with each element at that index of size 'n'.

	array.each_slice(3) {|i| p i}
	#Output :
	#[1, 2, 4]
	#[5]
	#each_slice(n) divides the array into slices of 'n' elements each starting from the index 0.

	array.inject(0) {|s, i| s+=i} #=&gt; 12
	#This is a very nifty method, in the method call as argument it takes as input (in this case 0) as the initial value for a variable, within the block you get that variable as the first argument (in this case s) and the method loops through all the values within the object and executes the block for it.

	#Some more useful methods for Collections
	array.first #=&gt; 1
	array.last #=&gt; 5

	array.max #=&gt; 5
	array.min #=&gt; 1

	array.sort #=&gt; [1, 2, 4, 5]
	array.sort {|i, j| j &lt;=&gt; i} #=&gt; [5, 4, 2, 1]
	#This returns a new array with the same values as the caller array but in sorted order
	array #=&gt; [1, 2, 4, 5]

	array.sort! {|i, j| j &lt;=&gt; i}  #=&gt; [5, 4, 2, 1]
	#as the ! indicates, the sort method modifies the caller object itself and updates it with the sorted array.
	array #=&gt; [5, 4, 2, 1]
	</pre>
</li>
</ul>
<p>Loop flow control modifiers:</p>
<ul>
<li>Break &#8211; terminates the immediate loop currently executing
<pre class="brush: ruby; title: ; notranslate">
	array = 1, 2, 3, 4
	array.each do |i|
		puts i
		break if i == 2 #In Ruby if a condition spans a single line you can simply follow the code block with the if condition and it will be evaluated just as you can read it.
	end

	#Output :
	#1
	#2
	</pre>
</li>
<li>Next &#8211; terminates the current iteration and beings the next.
<pre class="brush: ruby; title: ; notranslate">
	array = 1, 2, 3, 4
	array.each {|i| next if i &lt; 3; puts i}

	#Output :
	#3
	#4
	</pre>
</li>
<li>Redo &#8211; restarts the current iteration.
<pre class="brush: ruby; title: ; notranslate">
	j = 0
	array = 1, 2, 3, 4
	array.each do |i|
		puts i
		break if (j += 1) == 10
		redo if i &gt; 2
	end

	#Output :
	#1
	#2
	#3
	#3
	#3
	#3
	#3
	#3
	#3
	#3

	#As you can see here, as soon as i &gt; 2 (i == 3) the current iteration is restarted, since the value of i was 3, it remains 3. I have used the variable j to ensure that after 10 iterations the loop terminates with the help of the break modifier, otherwise the loop will keep running infinitely.
	</pre>
</li>
</ul>
<p>That having done, let me now get started with the other basic</p>
<h3>Variables</h3>
<p>Any plain lowercase word is qualified to be an identifier or a variable name. Variable names may contain letters, underscores or digits except that a variable name cannot begin with a digit.</p>
<pre class="brush: ruby; title: ; notranslate">
x = 0
y1 = 0
test_variable_1 = 'string'

x = y = z = 0

x, y, z = 1, 2, 3 #=&gt; [1, 2, 3]
x #=&gt; 1
y #=&gt; 2
z #=&gt; 3

a, (b, c), d = 1, 2, 3, 4
#=&gt; a = 1, b = 2, c = nil, d = 3

a, (b, c), d = 1, [2, 3], 4
#=&gt; a = 1, b = 2, c = 3, d = 4
</pre>
<h3>Symbols</h3>
<p>Symbols are very similar to variables, except they start with a colon (:), they too may contain letters, underscores or digits except that they can&#8217;t begin with a digit (i.e. after the colon). Symbols are lightweight strings, they take up less memory, are faster and are immutable. These are generally used where you don&#8217;t need to print them out and also are rightly used for keys in hashes.</p>
<pre class="brush: ruby; title: ; notranslate">
hash = {}
hash[:key] = 'value'
#As stated above it is always recommended to use Symbols where ever possible since they take up lesser memory, are always faster to work with and most importantly are immutable.
</pre>
<h3>Constants</h3>
<p>Constants in Ruby are words again very much like variables, except that they are capitalized. Constants as the name suggests are used for storing constant values which will not change over time. The most prominent usage of constants comes in defining classes, whose names are always constants, we will see classes later in much more detail. If you try to reassign an already assigned constant, the ruby interpreter will throw a warning and let you know, however the value will still change</p>
<h3>Methods</h3>
<p>Classes from a generic OOPs concepts is nothing but an encapsulation of the properties &amp; functions to work on those properties. Methods are those functions. Methods in ruby are referred to as messages that are send to the caller object, methods may optionally take arguments as input.</p>
<p>In Ruby all methods return something, by default the last statement within the method&#8217;s definition determines what is returned. Although we have the return keyword in ruby, we do not always need to use it since we know the fact that the last line of the method body is always returned, hence when returning a value we can simply just have that value as the last line of the method definition and it will be returned.</p>
<h3>Global Variables</h3>
<p>Variables that begin with a dollar ($) sign are global. They are accessible from anywhere within your program, they never go out of scope, since they are in global scope.</p>
<p>That covers a lot for one post :). For now I think this much is enough for us to get started with coding something more concrete than what we have up until now. So in my next article I will take up a smallish problem to work on and we&#8217;ll see the solution for it. After that we&#8217;ll move onto classes &amp; modules. Hope these articles benefit more people than I think. If you have any queries, suggestions or feedback, please let me know in the comments below.</p>
<div class="shr-publisher-15737"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://dhruvasagar.net/2010/10/26/getting-started-with-ruby-part5/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting started with Ruby &#8211; Part 4 &#8211; Hashes &amp; Blocks</title>
		<link>http://dhruvasagar.net/2010/10/23/getting-started-with-ruby-part4/</link>
		<comments>http://dhruvasagar.net/2010/10/23/getting-started-with-ruby-part4/#comments</comments>
		<pubDate>Sat, 23 Oct 2010 09:05:02 +0000</pubDate>
		<dc:creator>Dhruva Sagar</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[blocks]]></category>
		<category><![CDATA[Data Types]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[hashes]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://dhruvasagar.net/?p=15412</guid>
		<description><![CDATA[In my previous article Getting started with Ruby &#8211; Part 3 I started with Collections and covered basics of Ranges &#38; Arrays. Like I mentioned towards the end of the article (in case you couldn&#8217;t manage to reach the end) I will start with one of the most commonly used Collection objects in Ruby, namely [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F10%2F23%2Fgetting-started-with-ruby-part4%2F' data-shr_title='Getting+started+with+Ruby+-+Part+4+-+Hashes+%26+Blocks'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F10%2F23%2Fgetting-started-with-ruby-part4%2F' data-shr_title='Getting+started+with+Ruby+-+Part+4+-+Hashes+%26+Blocks'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p>In my previous article <a title="Getting started with Ruby" href="http://dhruvasagar.net/2010/10/21/getting-started-with-ruby-part3/" target="_self">Getting started with Ruby &#8211; Part 3</a> I started with Collections and covered basics of Ranges &amp; Arrays. Like I mentioned towards the end of the article (in case you couldn&#8217;t manage to reach the end) I will start with one of the most commonly used Collection objects in Ruby, namely Hashes.</p>
<p>Hashes are essentially data structures to store key &#8211; value pairs, when you wish to store an element in the Hash you do so by assigning a unique key to it within the hash and while retrieving you do so with the help of that unique key. The beauty of hashes is the fact that the &#8216;key&#8217; in question here could be just about any object in Ruby, most commonly however in practice strings or symbols (i&#8217;ll get to what these are soon) are used as keys.</p>
<p><span id="more-15412"></span></p>
<p>Well i&#8217;ve said enough, time for some code :</p>
<pre class="brush: ruby; title: ; notranslate">
hash = {} #=&gt; {}
hash = Hash.new #=&gt; {}
hash = {'key1' =&gt; 'value1',
        'key2' =&gt; Object.new,
        :key3 =&gt; Hash.new,
        1 =&gt; 2}

hash['key1'] #=&gt; 'value1'
hash['key2'] #=&gt; #&lt;Object:0x00000001e98bc0&gt;
hash['key3'] #=&gt; nil
hash[:key3] #=&gt; {}
hash[1] #=&gt; 2

hash = Hash.new('Doesn\'t Exist!')
hash['dummy-key'] #=&gt; &quot;Doesn't Exist!&quot;
#The constructor for a Hash takes the default value as input that should be returned when referencing a non-existent key, when nothing is input to the constructor the default value is nil.

hash['dummy-key'] = 'Dummy Value' #=&gt; 'Dummy Value'
hash #=&gt; {&quot;dummy-key&quot;=&gt;&quot;Dummy Value&quot;}
#In order to define a new key-value pair for a hash, you simply use the assignment operation for the key, if the key doesn't already exist within the array, it creates a new key value pair based on what you assigned, however if they key had already existed in the hash, then the value of that key would get overwritten by the new assignment.

hash.store('key', 'value') #=&gt; 'value'
hash #=&gt; {'dummy-key'=&gt;'Dummy Value', 'key' =&gt; 'value'}
#As you can see, you can alternatively use the store method to achieve the same, however in practice one rarely use it.

hash.has_key?('key') #=&gt; true
hash.has_value?('value') #=&gt; true

hash.empty? #=&gt; false

hash.delete('key') #=&gt; 'value'
hash #=&gt; {'dummy-key' =&gt; 'Dummy Value'}
hash.delete('test') #=&gt; &quot;Doesn't Exist!&quot;

hash.clear #=&gt; {}
hash.empty? #=&gt; true

hash.keys #=&gt; ['dummy-key']
#The keys method for a hash returns an array of all the keys available by the hash object

hash.values #=&gt; ['Dummy Value']
#The values method for a hash returns an array of all the values available in the hash object.
</pre>
<p>That just about covers the basics of Hashes, which in turn covers the basics of various built-in data type classes in Ruby.</p>
<p>As per my previous article <a title="Getting started with Ruby" href="http://dhruvasagar.net/2010/10/21/getting-started-with-ruby-part3/" target="_self">Getting started with Ruby &#8211; Part 3</a> I have to now get started with loops in Ruby. But before I get into that, I feel it is important that I introduce you to blocks. Blocks in Ruby are essentially nameless functions, in Ruby blocks are used extensively to achieve a lot of cool stuff.</p>
<pre class="brush: ruby; title: ; notranslate">
def block_test
  yield if block_given?
end

block_test { puts &quot;test&quot; }
#Here we call the block_test method and pass in a block. A block as I explained before is a nameless function, and hence we can do everything within a block, that we can do within a function. A block can be defined in 2 ways.

{ puts &quot;test1&quot;; puts &quot;test2&quot; }
#OR
do
  puts &quot;test1&quot;
  puts &quot;test2&quot;
end

#As you can see, the first way to define a block is using the curly braces {}, which is used to define a block in a single line of code. If you wish to still include multiple lines of code within the {} block, you may do so by using the ';' which you can use as a delimiter to have multiple Ruby statements on a single line. The other way to define blocks is using the 'do...end' which allows you to write your code in separate lines and comes in more useful when dealing with more complex code.
#In Ruby, every function can optionally receive a single block, which can be executed from within the function with the help of the &quot;yield&quot; function. Since it is optional, Ruby also provides a special method &quot;block_given?&quot; which returns true | false depending on weather a block was passed to the function during its call or not.

def block_with_args_test
  yield(&quot;string argument1&quot;, &quot;string argument2&quot;) if block_given?
end

block_with_args_test {|arg1, arg2| puts arg1; puts arg2}

#Output :
#string argument1
#string argument2

#Here you see how you can define blocks that accept arguments. When you use the yield method, you can pass in values as arguments and they are then passed onto the block during invocation.
</pre>
<p>For now I think that  good enough to go forward, later on we should come across more complex &amp; advanced usages of blocks &amp; closures.</p>
<p>An important point about functions, it is not mandatory to use brackets &#8216;(&#8216; &amp; &#8216;)&#8217;  while calling a function for passing the arguments unless you&#8217;re calling a function within a function call as an argument or when you are passing a block to it</p>
<pre class="brush: ruby; title: ; notranslate">
def test(arg1, arg2)
  puts arg1
  puts arg2
  yield if block_given?
end

test 1, 2
#Output :
#1
#2

test(1,2)
#Output :
#1
#2

test(1,2) {puts 'test'}
#Output :
#1
#2
#test

test 1, 2 {puts 'test'}
syntax error, unexpected '}', expecting $end
test 1, 2, {puts 'test'}
syntax error, unexpected '}', expecting $end
</pre>
<p>Considering that the article is already quite long and loaded with a whole lot to digest, I will now take up loops in my next part. I hope the article is easy to understand. If you have any questions or suggestions, please feel free to put them in the comments below and I will make sure I address it asap.</p>
<div class="shr-publisher-15412"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://dhruvasagar.net/2010/10/23/getting-started-with-ruby-part4/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Getting started with Ruby &#8211; Part 3 &#8211; Collections, Ranges &amp; Arrays</title>
		<link>http://dhruvasagar.net/2010/10/21/getting-started-with-ruby-part3/</link>
		<comments>http://dhruvasagar.net/2010/10/21/getting-started-with-ruby-part3/#comments</comments>
		<pubDate>Thu, 21 Oct 2010 12:23:40 +0000</pubDate>
		<dc:creator>Dhruva Sagar</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[Data Types]]></category>
		<category><![CDATA[range]]></category>
		<category><![CDATA[ranges]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://dhruvasagar.net/?p=15008</guid>
		<description><![CDATA[In my previous article Getting started with Ruby &#8211; Part 2 I introduced you all to Ruby &#38; its philosophy and I started out with data type classes in Ruby. I was able to give you an overview about Strings &#38; Numbers in ruby. Today I will start with Collections. The Range &#8211; As the [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F10%2F21%2Fgetting-started-with-ruby-part3%2F' data-shr_title='Getting+started+with+Ruby+-+Part+3+-+Collections%2C+Ranges+%26+Arrays'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F10%2F21%2Fgetting-started-with-ruby-part3%2F' data-shr_title='Getting+started+with+Ruby+-+Part+3+-+Collections%2C+Ranges+%26+Arrays'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p>In my previous article <a title="Getting started with Ruby - Part 2" href="http://dhruvasagar.net/2010/10/19/getting-started-with-ruby-part2/" target="_self">Getting started with Ruby &#8211; Part 2</a> I introduced you all to Ruby &amp; its philosophy and I started out with data type classes in Ruby. I was able to give you an overview about Strings &amp; Numbers in ruby. Today I will start with <strong>Collections</strong>.<br />
<span id="more-15008"></span></p>
<ul>
<li><strong>The Range</strong> &#8211; As the name suggests, ranges hold a  sequential collection of numbers. Defining a range in Ruby is extremely simple, instead of explaining, i&#8217;ll show it to you. Don&#8217;t be afraid to start an IRB shell to try the code out.
<pre class="brush: ruby; title: ; notranslate">
full_range = 1..10
loop_range = 0...10
test_range = 1..10

puts full_range == loop_range #=&gt; false
puts full_range == test_range #=&gt; true
puts loop_range.eql?(loop_range) #=&gt; false

puts full_range.include?(1) #=&gt; true
puts test_range.include?(4) #=&gt; true
puts full_range.include?(10) #=&gt; true
puts full_range.include?(12) #=&gt; false

puts loop_range.include?(0) #=&gt; true
puts loop_range.include?(4) #=&gt; true
puts loop_range.include?(10) #=&gt; false
</pre>
<p>From the examples above, it should be clear what the syntax to define a range is, although you might be confused regarding the difference in line 1 &amp; line 2, I can assure you it&#8217;s not a typo. Ruby provides 2 ways to create ranges, one using two periods (..) and one using triple periods (&#8230;), the difference being full_range = 1..10 is a range which starts from value 1 and ends at value 10, whereas loop_range = 0&#8230;10 is a range that starts from value 0 and ends at value 9.</p>
<p>Here I have also introduced you to 3 methods of the range ==, eql? &amp; include?, I am sure that the methods names are quite self explanatory. Although I would like to bring your attention towards a convention that is followed in ruby for method names. Method names in Ruby may end in =, ? or !. As a convention we end a method name in ? to indicate that the method will be returning a boolean value, i.e. true or false, we end a method name in = to indicate that it is a setter method used for assignment operations, and we end a method name in ! to indicate that the execution of the method will alter the state / value of the caller object. We will get into details of methods later again when we would be digging into classes.</li>
<li><strong>Array</strong> &#8211; These are simple indexed or ordered collection of elements or objects. If you have worked in C/C++/Java, Arrays are similar in concept except for the fact that the various elements within the Array need not be of the same type, they could be of any type. Let me give you some examples to make it more clear :
<pre class="brush: ruby; title: ; notranslate">
empty_array = [] #=&gt; []
another_empty_array = Array.new #=&gt; []
number_array = (1..10).to_a #=&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#to_a is a method that the Range class implements which converts the range to an array and returns the array

random_array = [1, 'hello', (100/3).to_s, Array.new, (1..10).to_a]
#=&gt; [1, &quot;hello&quot;, &quot;33&quot;, [], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
#As you can see, the array is dynamic in nature and the individual element can be just about any object.

preinitialized_array = Array.new(10) #=&gt; [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
preinitialized_array_with_values = Array.new(10) {0} #=&gt; [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

short_hand_array = %w(this is an array) #=&gt; [&quot;this&quot;, &quot;is&quot;, &quot;an&quot;, &quot;array&quot;]
short_hand_with_interpolation = %W(this is an array of length #{1*7})
#=&gt; [&quot;this&quot;, &quot;is&quot;, &quot;an&quot;, &quot;array&quot;, &quot;of&quot;, &quot;length&quot;, &quot;7&quot;]
#%w works similar to single quoted string creation except that it creates an array out of the words, whereas %W works similar to double quoted string creation and allows for interpolation by embedding results within and creates an array from the words.

short_hand_with_string = 'this is an array'.split #=&gt; [&quot;this&quot;, &quot;is&quot;, &quot;an&quot;, &quot;array&quot;]
short_hand_with_string = 'this is an array'.split(' ') #=&gt; [&quot;this&quot;, &quot;is&quot;, &quot;an&quot;, &quot;array&quot;]

short_hand_with_string[0] #=&gt; 'this'
short_hand_with_string[-1] #=&gt; 'array'
short_hand_with_string[-2] #=&gt; 'an'
#you can access the array from reverse by using negative numbers, awesome isn't it ? :)

short_hand_with_string[0..2] #=&gt; [&quot;this&quot;, &quot;is&quot;, &quot;an&quot;]
#you can use a range to reference multiple elements within an array and it will return an array

short_hand_with_string.values_at(0, 1, 2) #=&gt; [&quot;this&quot;, &quot;is&quot;, &quot;an&quot;]

short_hand_with_string.at(-1) #=&gt; 'array'
short_hand_with_string.at(100) #=&gt; nil
short_hand_with_string.fetch(100, 'Not Found!') #=&gt; 'Not Found!'
</pre>
<p>The above are various ways in which one can create &amp; access Arrays in Ruby, I think most of them are pretty self explanatory, there may be some minor constructs here that you may not understand, but I will be going through them more in detail later on, for now lets concentrate on just Arrays.</p>
<p>Let us now take a look at various ways to manipulate arrays :</p>
<pre class="brush: ruby; title: ; notranslate">
array = (1..10).to_a #=&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array[0] = 11 #=&gt; [11, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array[12] = 12 #=&gt; [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil, nil, 12]
array.push(13, 14) #=&gt; [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil, nil, 12, 13, 14]
#pushes / appends / inserts multiple comma separate values to the end of the array

array.insert(-1, 16) #=&gt; [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil, nil, 12, 13, 14, 16]
array.insert(1, 100, 101) #=&gt; [11, 100, 101, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil, nil, 12, 13, 14, 16]
#insert method takes as input multiple comma separate values, however the first argument is the index of the position where you wish to insert th multiple values. So the values after the first argument are the values tha will be inserted into the array started from the index identified by the first argument.

array&lt;&lt; 20 #=&gt; [11, 100, 101, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil, nil, 12, 13, 14, 16, 20]
array&lt;&lt; 21&lt;&lt; 22&lt;&lt; 23
#=&gt; [11, 100, 101, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil, nil, 12, 13, 14, 16, 20, 21, 22, 23]
array.pop #=&gt; 23
array #=&gt; [11, 100, 101, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil, nil, 12, 13, 14, 16, 20, 21, 22]

array.pop(2) #=&gt; [21, 22]
array #=&gt; [11, 100, 101, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil, nil, 12, 13, 14, 16, 20]
#As you can see above pop method can optionally take as input an integer representing the number of elements that should be popped from the array.

array.shift #=&gt; 11
array #=&gt; [100, 101, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil, nil, 12, 13, 14, 16, 20, 21, 22]
array.shift(2) #=&gt; [100, 101]
array #=&gt; [2, 3, 4, 5, 6, 7, 8, 9, 10, nil, nil, 12, 13, 14, 16, 20]

array.index(14) #=&gt; 13
#This returns the index of the input element in the array, it returns nil if the element doesn't exist

array.include?(5) #=&gt; true

array.delete(14) #=&gt; 14
array #=&gt; [2, 3, 4, 5, 6, 7, 8, 9, 10, nil, nil, 12, 13, 16, 20]
#delete method takes as input an element which is then deleted from the caller array. In case the element does not exist within the array, it returns with nil.

array.delete(1000) { 'Doesn\'t Exist!' } #=&gt; Doesn't Exist
#The delete method also accepts a block which it uses in case the input element does not exist

array.delete_at(3) #=&gt; 5
array #=&gt; [2, 3, 4, 6, 7, 8, 9, 10, nil, nil, 12, 13, 16, 20]
#delete_at method takes the index number as input instead of the element like the delete method and deletes the element at that index within the caller array and returns it.
</pre>
<p>The above should give you a very good idea on how to create &amp; manipulate arrays, the examples should be explanatory, I have also added helpful comments along.</p>
<p>I had forgotten to demonstrate some really powerful features of arrays before hand. Let me do that now :</p>
<pre class="brush: ruby; title: ; notranslate">
array1 = 1,2,3,4
array2 = 4,5,6,7
#As you can see, while assigning, its not necessary to have the square brackets []

array1 + array2 #=&gt; [1, 2, 3, 4, 4, 5, 6, 7]
#union

array1 - array2 #=&gt; [1, 2, 3]
array2 - array1 #=&gt; [5, 6, 7]
#difference

array1 &amp; array2 #=&gt; [4]
#intersection
</pre>
<p>Since the article is already very long, I will cut it short here and take up the last collection data type <strong>Hash</strong> in my next article. After that I will take you through various mechanisms for looping over collections in Ruby, which will apply to all collection data types.</p>
<p>In Ruby there are always several ways to do one thing, the reason for that is because there are always more than one intuitive way to do the same thing. Different developers use different ways to do what they wish to as per what suits them best. I am sure you will get there too soon enough.</p>
<p>Please do feel free to launch the IRB and try out various things that I have shown here, experiment &amp; play around with it and I am sure you will have a nice time :). After I finish with covering the looping mechanisms for collections I will also write about how you can use IRB &amp; Ruby&#8217;s reflective nature to inspect objects.</p>
<p>I hope you find the series of articles easy to understand and that it helps you getting started with Ruby. In case you have any questions or suggestions, please do let me know by adding it in the comments below.</li>
</ul>
<div class="shr-publisher-15008"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://dhruvasagar.net/2010/10/21/getting-started-with-ruby-part3/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Getting started with Ruby &#8211; Part 2 &#8211; Introduction &amp; Philosophy</title>
		<link>http://dhruvasagar.net/2010/10/19/getting-started-with-ruby-part2/</link>
		<comments>http://dhruvasagar.net/2010/10/19/getting-started-with-ruby-part2/#comments</comments>
		<pubDate>Tue, 19 Oct 2010 19:50:00 +0000</pubDate>
		<dc:creator>Dhruva Sagar</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[Data Types]]></category>
		<category><![CDATA[Numbers]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://dhruvasagar.net/?p=14922</guid>
		<description><![CDATA[Hi again, this is part 2 of my series of articles to help beginners getting started with the Ruby programming language. At this point of time I would like to quote the definition of Ruby from the Ruby-Lang official website. A dynamic, open source programming language with a focus on simplicity and productivity. It has [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F10%2F19%2Fgetting-started-with-ruby-part2%2F' data-shr_title='Getting+started+with+Ruby+-+Part+2+-+Introduction+%26+Philosophy'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fdhruvasagar.net%2F2010%2F10%2F19%2Fgetting-started-with-ruby-part2%2F' data-shr_title='Getting+started+with+Ruby+-+Part+2+-+Introduction+%26+Philosophy'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Hi again, this is part 2 of my series of articles to help beginners getting started with the Ruby programming language. At this point of time I would like to quote the definition of Ruby from the <a title="Ruby Programming Language" href="http://www.ruby-lang.org/en/" target="_blank">Ruby-Lang official website</a>.</p>
<blockquote><p>A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.</p></blockquote>
<p>I know I had already quoted Ruby&#8217;s definition from wikipedia in my previous article &#8216;<a title="Getting started with Ruby - Part 1" href="http://dhruvasagar.net/2010/10/19/getting-started-with-ruby-part1/" target="_self">Getting started with Ruby &#8211; Part 1</a>&#8216;, but that was more of a technical definition. Here I wanted to draw your attention to <a title="Yukihiro Matsumoto" href="http://en.wikipedia.org/wiki/Yukihiro_Matsumoto" target="_blank">Yukihiro Matsumoto</a>&#8216;s, popularly known as Matz&#8217;s philosophy behind creating Ruby, which is making programming as simple &amp; natural as humanely possible. We will be witnessing that very soon.</p>
<p>NOTE : Before diving into programming, I would like to bring to your notice the fact that most of the information that I will be sharing here in my articles, would be my interpretation of publicly available information, and based on my experiences, I will try to be as expressive as possible, however I can make mistakes and would appreciate your help in pointing out the same if I do. <strong>All my examples have been ran and tested on Ruby 1.9.2-p0</strong>.</p>
<p><span id="more-14922"></span></p>
<p><strong><span style="font-size: 150%;">Ruby</span></strong> is a <strong>Pure Object Oriented Programming Language</strong>, by pure, it is implied that <strong>everything</strong> in Ruby is an object. Unlike programming languages like C++/Java/C# which are OOPLs but not pure since they have primitive data types which are not objects, in Ruby, everything is an object. However, that being said, we obviously do have types in Ruby, but these are not primitive data types, they are classes which originate from the Object class. In Ruby every class originates from the Object class. The following are the various data types in Ruby :</p>
<ul>
<li><strong>String</strong> &#8211; as the name suggests, these are a sequence of characters. Any sequence of characters enclosed between single or double quotes creates a string. Let us look at some examples :
<pre class="brush: ruby; title: ; notranslate">
puts 'Hello' #=&gt; Hello
puts 'What\'s Up?' #=&gt; What's Up?
puts &quot;A\tTab.&quot; #=&gt; A	Tab.
puts &quot;#{4*4}&quot; #=&gt; 16
</pre>
<p>First example is pretty straight forward, in the second example I demonstrate what are known as escape sequences. Since the single quote (&#8216;) is used to indicate the start &amp; end of a string, if you wanted to use one within a string, you need to use the escape sequence using the backslash (\) to indicate the Ruby interpreter to replace it with that character and ignore it for the purpose of determining the start &amp; end of the string. The third example is also demonstrates the use of certain special escape sequences, which when used in strings defined using double quotes (&#8220;) are parsed and replaced with appropriate value. In the example \t is replaced by the Ruby interpreter with a tab character. The fourth example demonstrates how you can evaluate expressions and embed their values within strings, again this is possible only for strings defined using double quotes (&#8220;).</p>
<p>Single quoted strings are ordinary strings and have a very limited set of escape sequences, whereas Double quoted strings offer a lot more escape sequences like \t, \n, \r, \s, etc and also they allow expression interpolation, that is embedding results of expressions or variables within strings by enclosing them within #{&#8230;}</p>
<p>Now I will take you through some extremely interesting examples :</p>
<pre class="brush: ruby; title: ; notranslate">
puts 'Hello ' * 3 #=&gt; Hello Hello Hello
puts %q{Whats Up?} #=&gt; Whats Up?
puts %Q{A\tTab.} #=&gt; A	Tab.
puts %Q;#{4*4}; #=&gt; 16
puts %Q(#{5*5}) #=&gt; 25
</pre>
<p>The first example might surprise you, however if you think about it logically, it does exactly what you would expect it to do. Since string literals, like everything else in Ruby are objects, multiplying it with a number makes that many copies of it.</p>
<p>The following 3 examples demonstrate a short hand notation in Ruby to create strings, as you might be able to tell, %q is the equivalent to using single quotes and %Q is equivalent to using double quotes. The short hand notation allows you to use any special character as a delimiter which it uses for identifying the start and end of the expression that it will evaluate to create the string. Hence %Q.#{4*4}. == %Q{#{4*4}} == %Q@#{4*4}@ == %Q$#{4*4}$ == %Q(#{4*4}), etc etc. This is true for all short hand notations.</li>
<li><strong>Numbers</strong> &#8211; Ruby has two class Fixnum &amp; Bignum to represent numbers. As i&#8217;ve mentioned before, everything in Ruby is an object, and so are numbers! Fixnum are numbers within the range (-2^30) and (2^30 &#8211; 1), if the value of the number goes beyond this range, ruby transparently assigns it to an instance of Bignum so you as a programmer do not need to worry about that.<br />
examples :</p>
<pre class="brush: ruby; title: ; notranslate">
puts 23 #=&gt; 23
puts 12_000_000_000 #=&gt; 12000000000
puts 3.14 #=&gt; 3.14
puts 1.0e5 #=&gt; 100000
</pre>
<p>As you can see, Ruby allows underscores (_) within numbers and so you can make use of that to make your numbers more readable. Numbers with decimal place are known as floats, last two are examples of floats.</p>
<pre class="brush: ruby; title: ; notranslate">
puts 1 + 2 #=&gt; 3
puts 1.+(2) #=&gt; 3
puts 2**3 #=&gt; 8
</pre>
<p>The above snippet demonstrates how to perform arithmetic operations within Ruby. Here I would like to point out one important point. Since numbers in Ruby are objects, all arithmetic operations that we perform are actually methods that exist with the objects, although it is not very evident most of the time. The second example demonstrates how + is actually a method that we invoke using the object with value 1 and pass to it the object with value 2, which returns an object of value 3, the sum of 1 &amp; 2.<br />
All standard arithmetic operations exist in Ruby along with a special (**) operation, which is exponent operation. In the above example 2**3 can be read as 2^3 and the result of that is 8.</li>
</ul>
<p>I will cut short the article here since the article has grown quite long already. I will continue with other data types in the next article, after which I will take you through variables, symbols, methods, classes and more. I hope you like the article and it is easy to understand. If you have any questions or suggestions, please do let me know in the comments below.</p>
<div class="shr-publisher-14922"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://dhruvasagar.net/2010/10/19/getting-started-with-ruby-part2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

