<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Ruby: Recursive send</title>
	<atom:link href="http://csummers.com/2007/01/18/ruby-recursive-send/feed/" rel="self" type="application/rss+xml" />
	<link>http://csummers.com/2007/01/18/ruby-recursive-send/</link>
	<description>%w{life code ruby}.map { &#124;i&#124; "#{i} is awesome" }</description>
	<lastBuildDate>Sun, 30 Jan 2011 20:55:04 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Vish</title>
		<link>http://csummers.com/2007/01/18/ruby-recursive-send/comment-page-1/#comment-137</link>
		<dc:creator>Vish</dc:creator>
		<pubDate>Wed, 31 Jan 2007 17:20:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.csummers.org/index.php/2007/01/18/ruby-recursive-send/#comment-137</guid>
		<description>This looks like an &quot;accumulator&quot; pattern- we associate an injector with the collection that we want to operate on and then the method (rsend) is the operative accumulator:

&lt;pre lang=&quot;ruby&quot;&gt;
class Array
  def inject(n)
     each { &#124;value&#124; n = yield(n, value) }
     n
  end

  def rsend(baseobj)
    inject(baseobj) { &#124;n, value&#124; n.send(value) }
  end
end
&lt;/pre&gt;

Then we&#039;d use it like so:
&lt;pre lang=&quot;ruby&quot;&gt;[:comments, :first, :commented_at].rsend(:post)&lt;/pre&gt;


-Vish K.</description>
		<content:encoded><![CDATA[<p>This looks like an &#8220;accumulator&#8221; pattern- we associate an injector with the collection that we want to operate on and then the method (rsend) is the operative accumulator:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> inject<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span>
     each <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>value<span style="color:#006600; font-weight:bold;">|</span> n = <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>n, value<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
     n
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> rsend<span style="color:#006600; font-weight:bold;">&#40;</span>baseobj<span style="color:#006600; font-weight:bold;">&#41;</span>
    inject<span style="color:#006600; font-weight:bold;">&#40;</span>baseobj<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>n, value<span style="color:#006600; font-weight:bold;">|</span> n.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span> <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></pre></div></div>

<p>Then we&#8217;d use it like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:comments</span>, <span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:commented_at</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:post</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>-Vish K.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

