<?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>The Ruby Way</title>
	<atom:link href="http://therubyway.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://therubyway.org</link>
	<description>Unofficial Blog About &#34;The Ruby Way&#34; by Hal Fulton</description>
	<lastBuildDate>Mon, 05 Oct 2009 10:52:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>What is The Ruby Way?</title>
		<link>http://therubyway.org/what-is-the-ruby-way/</link>
		<comments>http://therubyway.org/what-is-the-ruby-way/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 03:21:25 +0000</pubDate>
		<dc:creator>Peter Cooper</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://therubyway.org/?p=3</guid>
		<description><![CDATA[Learn about the book <em>The Ruby Way</em> and how to buy it.]]></description>
			<content:encoded><![CDATA[<div class="announcement_post"><p><a href="http://www.amazon.com/gp/product/0672328844?ie=UTF8&amp;tag=rubins-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1430223634"><img style="float: left; margin-left: -20px; margin-right: 12px; margin-bottom:12" src="http://ecx.images-amazon.com/images/P/0672328844.01._PA12,6,8,25_SS210_PT-05_SCLZZZZZZZZ_.jpg" alt="" /></a><strong>The Ruby Way</strong> (<a href="http://www.amazon.com/gp/product/0672328844?ie=UTF8&amp;tag=rubins-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0672328844">buy it at Amazon.com</a>) is a <a href="http://www.ruby-lang.org/en/">Ruby</a> programming book published by Addison-Wesley that&#8217;s often considered the &#8220;bible&#8221; of Ruby books. It covers a dizzying amount of Ruby topics in a &#8220;how to&#8221; format &#8211; including low level data types, metaprogramming, I/O, graphics libraries, Web development tools, OOP, IDEs, and more (<a href="http://rubyhacker.com/toc.html">see the full table of contents</a>). It&#8217;s an essential book for any Rubyist&#8217;s library.</p>
<h3>Where To Buy</h3>
<p>Amazon typically offers the best price for <em>The Ruby Way.</em> Check it out on <a href="http://www.amazon.com/gp/product/0672328844?ie=UTF8&amp;tag=rubins-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0672328844">Amazon.com (US)</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=rubins-20&amp;l=as2&amp;o=1&amp;a=1430223634" border="0" alt="" width="1" height="1" /> or <a href="http://www.amazon.co.uk/gp/product/0672328844?ie=UTF8&amp;tag=rubyins-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=0672328844">Amazon.co.uk (UK).</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.co.uk/e/ir?t=rubyins-21&amp;l=as2&amp;o=2&amp;a=1430223634" border="0" alt="" width="1" height="1" /> </p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://therubyway.org/what-is-the-ruby-way/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A Ruby Idiom: Defining Methods Based on Conditions at Runtime</title>
		<link>http://therubyway.org/a-ruby-idiom-defining-methods-based-on-conditions-at-runtime/</link>
		<comments>http://therubyway.org/a-ruby-idiom-defining-methods-based-on-conditions-at-runtime/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 10:52:48 +0000</pubDate>
		<dc:creator>Peter Cooper</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://therubyway.org/a-ruby-idiom-defining-methods-based-on-conditions-at-runtime/</guid>
		<description><![CDATA[On pages 36 and 37 of The Ruby Way (2nd Edition) Hal talks about &#8220;Coding at Runtime.&#8221; He specifically mentions how while ifdef directives can be used in C to change how things are defined depending on conditions set prior to the compile, you can just do the same at runtime in Ruby.
For example, let&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>On pages 36 and 37 of <i>The Ruby Way (2nd Edition)</i> Hal talks about &#8220;Coding at Runtime.&#8221; He specifically mentions how while <code>ifdef</code> directives can be used in C to change how things are defined depending on conditions set prior to the compile, you can just do the same <i>at runtime</i> in Ruby.</p>
<p>For example, let&#8217;s say your app is designed to run on multiple platforms, but a certain action has to be executed in a totally different way for a couple of platforms (a common situation where Windows is involved). You could define your logic like this:</p>
<pre>
if platform == Windows
&nbsp;&nbsp;action1
elsif platform == Linux
&nbsp;&nbsp;action2
else
&nbsp;&nbsp;default_action
end
</pre>
<p>To this sort of arrangement Hal says: &#8220;Of course, there is a small runtime penalty for coding in this way since the flag may be tested many times in the course of execution.&#8221; He then presents a technique where a method itself is defined at run-time based upon the condition:</p>
<pre>
if platform == Windows
&nbsp;&nbsp;def my_action
&nbsp;&nbsp;&nbsp;&nbsp;action 1
&nbsp;&nbsp;end
elsif platform == Linux
&nbsp;&nbsp;def my_action
&nbsp;&nbsp;&nbsp;&nbsp;action2
&nbsp;&nbsp;end
else
&nbsp;&nbsp;def my_action
&nbsp;&nbsp;&nbsp;&nbsp;default_action
&nbsp;&nbsp;end
end
</pre>
<p>To this, Hal says: &#8220;In this way, the same result is achieved, but the flag is only evaluated once; when the user&#8217;s code calls my_action, it will already have been defined appropriately.&#8221;</p>
<p>Personally I&#8217;m not entirely convinced by Hal&#8217;s specific example in <em>The Ruby Way</em>, but it&#8217;s a handy reminder of how dynamic Ruby is. I&#8217;m sure there are some great examples of this technique in the wild.</p>
]]></content:encoded>
			<wfw:commentRss>http://therubyway.org/a-ruby-idiom-defining-methods-based-on-conditions-at-runtime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Basic Ruby Idioms: Checking For An Empty String</title>
		<link>http://therubyway.org/basic-ruby-idioms-checking-for-an-empty-string/</link>
		<comments>http://therubyway.org/basic-ruby-idioms-checking-for-an-empty-string/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 17:35:41 +0000</pubDate>
		<dc:creator>Peter Cooper</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://therubyway.org/basic-ruby-idioms-checking-for-an-empty-string/</guid>
		<description><![CDATA[Page 15 of The Ruby Way (2nd edition) presents a basic Ruby idiom for checking whether a string is empty or not.
I believe there are three ways to do it (if you can think of any others, leave a comment!). Two reasonably obvious, but one that&#8217;s quite idiomatic (the one that The Ruby Way mentions):

str.empty? [...]]]></description>
			<content:encoded><![CDATA[<p>Page 15 of <i>The Ruby Way</i> (2nd edition) presents a basic Ruby idiom for checking whether a string is empty or not.</p>
<p>I believe there are three ways to do it (if you can think of any others, leave a comment!). Two reasonably obvious, but one that&#8217;s quite idiomatic (the one that <em>The Ruby Way</em> mentions):</p>
<ol>
<li><code>str.empty?</code> &#8211; With this we simply ask a String object if it&#8217;s &#8220;empty.&#8221;</li>
<li><code>str.length &gt; 0</code> &#8211; With this we check whether the string has a length of more than zero &#8211; i.e. it&#8217;s not empty.</li>
<li><code>str[0]</code> &#8211; With this we check the <i>first character</i> of the string. If no first character is present, both Ruby 1.8 and 1.9 return <code>nil</code>! This is particularly idiomatic and not something I&#8217;d considered before.</li>
</ol>
<p>Of course, the <i>shortest</i> or most idiomatic route is not usually the <i>best</i> way to go when programming, but if you&#8217;re playing code golf, doing some obfuscation, or otherwise want to access the first character of the string anyway, option #3 is undeniably sneaky.</p>
<blockquote>
<p>Note: Remember that while a string can only be empty or non-empty, a <i>variable</i> that you think is referring to a String object may well be <i>nil</i>! So often you need to check for <i>nil</i> as well as string length. You can do this with a simple <code>if str</code> or <code>if str.nil?</code></p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://therubyway.org/basic-ruby-idioms-checking-for-an-empty-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kernel.abort: Quit and Print an Error to STDERR</title>
		<link>http://therubyway.org/kernel-abort-quit-and-print-an-error-to-stderr/</link>
		<comments>http://therubyway.org/kernel-abort-quit-and-print-an-error-to-stderr/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 17:19:05 +0000</pubDate>
		<dc:creator>Peter Cooper</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://therubyway.org/kernel-abort-quit-and-print-an-error-to-stderr/</guid>
		<description><![CDATA[One of the great things about going through other people&#8217;s Ruby books is that you discover small tidbits about the language that you&#8217;d never noticed before. While skimming through The Ruby Way again to come up with ideas for this blog, I noticed a basic program on page 14 (2nd edition) that starts like so:

print [...]]]></description>
			<content:encoded><![CDATA[<p>One of the great things about going through other people&#8217;s Ruby books is that you discover small tidbits about the language that you&#8217;d never noticed before. While skimming through <i>The Ruby Way</i> again to come up with ideas for this blog, I noticed a basic program on page 14 (2nd edition) that starts like so:</p>
<pre>
print "Please enter a temperature and scale (C or F): "
str = gets
exit if str.nil? or str.empty?
str.chomp!
temp, scale = str.split(" ")

<strong>abort "#{temp} is not a valid number." if temp !~ /-?\d+/</strong>
</pre>
<p>The <i>abort</i> line threw me for a loop; I&#8217;d not seen it in use before. I jumped into <i>irb</i> to give it a try and, yes, it works and, yes, it&#8217;s documented:</p>
<p><img src="http://therubyway.org/wp-content/uploads/2009/10/abort.png" width="480" height="117" alt="abort.png" /></p>
<p>Writing to STDERR can be pretty handy, especially if you&#8217;re a UNIX nerd threading together apps at the command line. So.. a nice find, even on such an early page of the book.</p>
]]></content:encoded>
			<wfw:commentRss>http://therubyway.org/kernel-abort-quit-and-print-an-error-to-stderr/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
