<?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>Railstic &#187; recursive</title>
	<atom:link href="http://railstic.com/tag/recursive/feed/" rel="self" type="application/rss+xml" />
	<link>http://railstic.com</link>
	<description>sharing experiences</description>
	<lastBuildDate>Mon, 29 Aug 2011 00:31:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Recursive methods with block in Ruby</title>
		<link>http://railstic.com/2008/08/recursive-methods-with-block/</link>
		<comments>http://railstic.com/2008/08/recursive-methods-with-block/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 10:46:17 +0000</pubDate>
		<dc:creator>İ. Emre Kutlu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://izzetemrekutlu.com/blog/?p=20</guid>
		<description><![CDATA[def comic&#40;cast&#41; cast.each do &#124;character&#124; unless character.is_a?&#40;Array&#41; yield&#40;character&#41; else comic&#40;character&#41; &#123;&#124;x&#124; yield x&#125; end end end This method takes an array as argument and checks each element if it is an Array. If the element is not an Array then the block is called. If the element is an Array then the same method is [...]]]></description>
			<content:encoded><![CDATA[<p></p>

<div class="wp_codebox"><table><tr id="p204"><td class="code" id="p20code4"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> comic<span style="color:#006600; font-weight:bold;">&#40;</span>cast<span style="color:#006600; font-weight:bold;">&#41;</span>
   cast.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>character<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#9966CC; font-weight:bold;">unless</span> character.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#41;</span>
         <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>character<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
	 comic<span style="color:#006600; font-weight:bold;">&#40;</span>character<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#9966CC; font-weight:bold;">yield</span> x<span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>
This method takes an array as argument and checks each element if it is an <em>Array</em>.<br />
If the element is not an <em>Array</em> then the block is called.<br />
If the element is an <em>Array</em> then the same method is called with that element as an argument and with the same block.<br />
<br />
Now let&#8217;s look how we can call this method.</p>

<div class="wp_codebox"><table><tr id="p205"><td class="code" id="p20code5"><pre class="ruby" style="font-family:monospace;">names = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'lucky luke'</span>, <span style="color:#996600;">'jolly jumper'</span>, <span style="color:#996600;">'rin tin tin'</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'joe'</span>, <span style="color:#996600;">'william'</span>,<span style="color:#996600;">'jack'</span>, <span style="color:#996600;">'averell'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
comic<span style="color:#006600; font-weight:bold;">&#40;</span>names<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> c<span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>
<strong>Output :</strong></p>

<div class="wp_codebox"><table><tr id="p206"><td class="code" id="p20code6"><pre class="bash" style="font-family:monospace;">lucky luke
jolly jumper
rin tin tin
joe
william
jack
averell</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://railstic.com/2008/08/recursive-methods-with-block/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

