<?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>SearchMarked.com &#187; Ruby</title>
	<atom:link href="http://www.searchmarked.com/category/ruby/feed" rel="self" type="application/rss+xml" />
	<link>http://www.searchmarked.com</link>
	<description>I search, I bookmark, I write, You learn.</description>
	<lastBuildDate>Wed, 14 Oct 2009 12:56:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to use a &#8220;while&#8221; loop in Ruby by singing a song about beer!</title>
		<link>http://www.searchmarked.com/ruby/how-to-use-a-while-loop-in-ruby.php</link>
		<comments>http://www.searchmarked.com/ruby/how-to-use-a-while-loop-in-ruby.php#comments</comments>
		<pubDate>Wed, 19 Dec 2007 16:57:15 +0000</pubDate>
		<dc:creator>hstagner</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.searchmarked.com/ruby/how-to-use-a-while-loop-in-ruby.php</guid>
		<description><![CDATA[I have been going through the Ruby tutorial at Chris Pine&#8217;s Learn to Program site. While, I have been making do with Ruby on Rails, I have never officially learned Ruby yet. I wanted a beginner tutorial to get a feel for the actual Ruby language itself.
At the end of the Chapter on Flow Control, [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "How to use a &#8220;while&#8221; loop in Ruby by singing a song about beer!", url: "http://www.searchmarked.com/ruby/how-to-use-a-while-loop-in-ruby.php" });</script>


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I have been going through the Ruby tutorial at <a href="http://pine.fm/LearnToProgram/" onclick="javascript:urchinTracker ('/outbound/article/pine.fm');">Chris Pine&#8217;s Learn to Program site</a>. While, I have been making do with Ruby on Rails, I have never officially learned Ruby yet. I wanted a beginner tutorial to get a feel for the actual Ruby language itself.</p>
<p>At the end of the <a href="http://pine.fm/LearnToProgram/?Chapter=06" onclick="javascript:urchinTracker ('/outbound/article/pine.fm');">Chapter on Flow Control</a>, Chris asks the reader to write a program that displays the lyrics to &#8220;99 bottles of beer&#8230;&#8221; I took on this task and added a few other features to enhance the output (like pausing so the reader can read the lines as they are written on the screen). This program demonstrates several concepts like While loops, methods, and the Don&#8217;t Repeat Yourself (DRY) mantra.</p>
<p>If you followed at least the Ruby portion of my previous tutorial on &#8220;<a href="http://www.searchmarked.com/ubuntu/install-ruby-on-rails-in-ubuntu.php" >How to install ruby on rails on Ubuntu Gutsy Gibbon (7.10)</a>&#8221; you should be able to run this program if you copy and paste the code below into a text document ( keep in mind both the &#8220;bottles of beer&#8221; and the &#8220;bottle of beer&#8221; text should be on one line, the &#8220;beer&#8221; portion is only looks like a separate line because of formatting), rename it beer.rb (or whatever you like) and type the following into the Terminal:<br />
<code>ruby beer.rb</code></p>
<p><code><font color="#000000">beer =</font> <font color="#ff00ff">99</font> <font color="#0000ff">#Cuz that's how many beerz you have!</font><br />
<font color="#000000"> pass =</font> <font color="#ff00ff">'Take one down and pass it around...'</font> <font color="#0000ff">#Cuz you has to share dem beerz!</font><br />
<font color="#0000ff"> # Begin da loop!</font><br />
<font color="#800000"> while</font> <font color="#000000">beer &gt;</font> <font color="#ff00ff">2</font> <font color="#0000ff">#Gotta gets da gramma right. Sees it below da loop!</font><br />
<font color="#000000">puts</font> <font color="#ff00ff">' '</font><br />
<font color="#000000"> puts beer.to_s +</font> <font color="#ff00ff">' bottles of beer on the wall, '</font> <font color="#000000">+</font> <font color="#000000">beer.to_s +</font> <font color="#ff00ff">' bottles of beer!'</font><br />
<font color="#000000"> puts pass</font><br />
<font color="#000000"> puts</font> <font color="#ff00ff">' '</font><br />
<font color="#000000"> beer = beer</font> - <font color="#ff00ff">1</font><br />
<font color="#000000"> sleep <font color="#ff00ff">5</font></font> <font color="#0000ff">#Gotta wait till the line is sung!</font><br />
<font color="#000000"> puts beer.to_s +</font> <font color="#ff00ff">' bottles of beer on the wall.'</font><br />
<font color="#000000"> puts</font> <font color="#ff00ff">' '</font><br />
<font color="#000000"> sleep</font> <font color="#ff00ff">2</font> <font color="#0000ff">#Gotta wait till the line is sung!</font><br />
<font color="#800000">end</font><br />
<font color="#0000ff"> #End da loop!</font><br />
<font color="#0000ff"> #Gets da gramma right with dis piece below!</font><br />
<font color="#000000"> beer = beer -</font> <font color="#ff00ff">1</font><br />
<font color="#000000"> puts beer.to_s +</font> <font color="#ff00ff">' bottle of beer on the wall, '</font> <font color="#000000">+ beer.to_s +</font> <font color="#ff00ff">' bottle of beer!'</font><br />
<font color="#000000"> puts pass</font><br />
<font color="#000000"> puts</font> <font color="#ff00ff">' '</font><br />
<font color="#000000"> beer = beer</font> - <font color="#ff00ff">1</font><br />
<font color="#000000"> puts</font> <font color="#ff00ff">'You drank all my beer! You Jerk!!!'</font> <font color="#0000ff">#It's a damn shame!</font></code></p>
<p>Have fun with beer.rb. I am sure that you can make improvements. Here are a few suggestions:</p>
<ol>
<li>Make it so after every 10 or 20 beers, the speech begins to slur</li>
<li>Add a &#8220;hic!&#8221; between lines every 10 or 20 lines</li>
<li>Make it so a scripted bar brawl happens after the last beer is gone</li>
</ol>
<p>If you add these improvements, I would love to hear about them in the comments below this post. Perhaps you could even share your code snippets.</p>
<p>This tutorial has been great so far. I am probably going to <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FLearn-Program-Pragmatic-Programmers-Chris%2Fdp%2F0976694042%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1198082505%26sr%3D8-1&amp;tag=searchmarkedcom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325" onclick="javascript:urchinTracker ('/outbound/article/www.amazon.com');">buy Chris Pine&#8217;s Learn to Program book</a><img src="http://www.assoc-amazon.com/e/ir?t=searchmarkedcom-20&amp;l=ur2&amp;o=1" style="border: medium none  ! important; margin: 0px ! important" border="0" height="1" width="1" /> so I can go through the expanded version of this tutorial. Happy Coding!</p>
<p>Technorati Tags: <a href="http://technorati.com/tag/Ruby" rel="tag" onclick="javascript:urchinTracker ('/outbound/article/technorati.com');">Ruby</a>, <a href="http://technorati.com/tag/Programming" rel="tag" onclick="javascript:urchinTracker ('/outbound/article/technorati.com');"> Programming</a>, <a href="http://technorati.com/tag/Beer" rel="tag" onclick="javascript:urchinTracker ('/outbound/article/technorati.com');"> Beer</a></p>
<p><a href="http://sharethis.com/item?&wp=2.8.6&amp;publisher=2b863fb9-6985-4119-9ea7-b8a518a3714e&amp;title=How+to+use+a+%26%238220%3Bwhile%26%238221%3B+loop+in+Ruby+by+singing+a+song+about+beer%21&amp;url=http%3A%2F%2Fwww.searchmarked.com%2Fruby%2Fhow-to-use-a-while-loop-in-ruby.php" onclick="javascript:urchinTracker ('/outbound/article/sharethis.com');">ShareThis</a></p>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.searchmarked.com/ruby/how-to-use-a-while-loop-in-ruby.php/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
