<?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>Elliott&#039;s Development Blog &#187; Ruby</title>
	<atom:link href="http://www.elliottsprehn.com/blog/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.elliottsprehn.com/blog</link>
	<description>Exploring Life Through Math, Algorithms and Code</description>
	<lastBuildDate>Mon, 30 Aug 2010 17:25:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>I Object!</title>
		<link>http://www.elliottsprehn.com/blog/2007/02/26/i-object/</link>
		<comments>http://www.elliottsprehn.com/blog/2007/02/26/i-object/#comments</comments>
		<pubDate>Mon, 26 Feb 2007 08:15:02 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.elliottsprehn.com/blog/?p=3</guid>
		<description><![CDATA[While doing some casual web surfing I came across a rather interesting blog entry about Ruby&#8217;s types and looping. I started typing a reply, and then I realized it was really long, so I&#8217;m putting it here: One reason I think methods like this are great is that Ruby is intended to be read! Which [...]]]></description>
			<content:encoded><![CDATA[<p>While doing some casual web surfing I came across a rather interesting blog entry about <a href="http://www.ditchnet.org/wp/2007/02/07/3times-erh-links/">Ruby&#8217;s types and looping</a>. I started typing a reply, and then I realized it was really long, so I&#8217;m putting it here:</p>
<p>One reason I think methods like this are great is that Ruby is intended to be <strong>read</strong>! Which actually makes 5.times(&#038;block) make much more sense than a C style 3 part for loop (that&#8217;s where it came from, not Java).</p>
<p><code>5.times do |i|; end</code> can be read as &#8220;5 times do this&#8221; or better &#8220;do this 5 times&#8221;</p>
<p>for on the other hand looks more like: <code>for( i=0; i &amp;lt; 5; i++ ) {}</code> which has no such linear meaning, &#8220;for set i equal to 0, i less than 5, i plus one do this&#8221;, its all out of order. At best you can rearrange it in your head as &#8220;set i to 0, while i is less than 5 do do this, add one to i after each iteration&#8221;</p>
<p>for() is also prone to error with the condition operator, was that supposed to be &lt; or &lt;= ? Plenty of applications have had bugs because of this, and where something loops to isn&#8217;t always clear. If you really don&#8217;t like 5.times, there&#8217;s other methodologies though, in fact you can use a &#8216;for&#8217; loop:</p>
<pre class="brush: ruby">for i in (0...5); end</pre>
<p>Read as &#8220;For i in the range of [0, 5) do this&#8221;</p>
<pre class="brush: ruby">(0...5).each do |i|; end</pre>
<p>Read as &#8220;for each in the range [0, 5) do this&#8221;</p>
<pre class="brush: ruby">0.upto(4) do |i|; end</pre>
<p>Again it reads linearly, from &#8220;from 0 up to 4 do this&#8221;.</p>
<p>I definitely think this makes Ruby more OO; Java suffers quite a lot from the distinction between primitive types and regular types. Want to design a collection in Java and store both objects and primitive types? You can&#8217;t declare:</p>
<pre class="brush: java">
class Collection&lt;T&gt; {
...
     public void add( T elem );
...
}
</pre>
<p>Instead you need to declare methods for each primitive type, and that method. This bloats Java types with many extra methods, or requires object wrappers that are a nasty hit on performance (something that&#8217;s totally unnecessary with a decent compiler). Other languages deal with this much more elegantly; by making numbers objects all you need to declare is the method that accepts the type T.</p>
<p>The Java API is greatly bloated because of this. Look at java.util.Arrays which has 9 methods to join() an array, one for each primitive type, and another for Object because of the Object#toString() and String.valueOf() distinction, even the String.valueOf() method has 9 signatures to deal with this limitation.</p>
<p>In ruby to get a string I can call to_s on *any* object, there is no null that causes exceptions, or special case primitives. I think Ruby unified all these types into a single Object hierarchy quite elegantly. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elliottsprehn.com/blog/2007/02/26/i-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	<img style='margin:0;padding:0;border:0;' width='1px' height='1px' src="http://www.elliottsprehn.com/blog/wp-content/plugins/mystat/mystat.php?act=time_load&id=34529&rnd=379975635" /></channel>
</rss>
