<?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>chrisadams.me.uk &#187; idioms</title>
	<atom:link href="http://chrisadams.me.uk/tag/idioms/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrisadams.me.uk</link>
	<description>Hoping to think more clearly, through thinking out loud</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:29:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Frustrating and Cryptic Ruby Idioms (#1 of a series)</title>
		<link>http://chrisadams.me.uk/2009/05/03/frustrating-and-cryptic-ruby-idioms-1-of-a-series/</link>
		<comments>http://chrisadams.me.uk/2009/05/03/frustrating-and-cryptic-ruby-idioms-1-of-a-series/#comments</comments>
		<pubDate>Sun, 03 May 2009 07:33:58 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Journal]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[idioms]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chrisadams.me.uk/?p=150</guid>
		<description><![CDATA[I keep coming across these FACRI's (Frustrating and Cryptic Ruby Idioms) in my work, so I'm jotting them here in the hope that I'll remember them better in future. Ruby idioms Ruby is a wonderful, if somewhat slow and memory hungry language, with an incredibly flexible and expressive syntax. However this flexibility leads to the [...]]]></description>
			<content:encoded><![CDATA[<p>I keep coming across these FACRI's (Frustrating and Cryptic Ruby Idioms) in my work, so I'm jotting them here in the hope that I'll remember them better in future.</p>
<h4>Ruby idioms</h4>
<p>Ruby is a wonderful, if somewhat slow and memory hungry language, with an incredibly flexible and expressive syntax. However this flexibility leads to the creation of idioms that initially look totally opaque, if you don't know what to look for.</p>
<h5>Case in point: the object{:&amp;method} idiom</h5>
<p>If you want to take an array called names , want to create a new array by running a text manipulation on every member of that array, a terse, but readable way to do this would be:</p>
<pre><code>result = names.map { |name| name.upcase }
</code></pre>
<p>The intent is pretty clear here, and what happens programatically is also very readable. Another to do this though is write it like:</p>
<pre><code>result = names.map {&amp;:upcase}
</code></pre>
<p>Something called type coercion is occurring here; you normally pass the <code>map</code> method a <code>Proc</code> object to execute, with a placeholder name for each iteration, and the code to run and return. However because you're not passing a Proc object here, Ruby tries to convert it on the fly into a Proc object using a method called <code>to_proc</code>:</p>
<pre><code>def to_proc
  proc { |obj, *args| obj.send(self, *args) }
end
</code></pre>
<p>So in this case, it's passing in <code>names</code>, and the method in the <code>*args</code> is <code>upcase</code>. I wasn't familiar with the <code>send</code> method here either, so the <a href="http://www.ruby-doc.org/core/classes/Object.html#M000335" title="Class: Object.send">documentation for it from ruby core</a> may help here:</p>
<pre><code>class Klass
     def hello(*args)
       "Hello " + args.join(' ')
     end
   end
k = Klass.new
k.send :hello, "gentle", "readers"   #=&gt; "Hello gentle readers"
</code></pre>
<h5>An expensive idiom, by rockstars, for rockstars.</h5>
<p>Th end result of all these examples is a saving of about 12 characters, at the expense of readability, and a huge performance hit as each member in names of passed around and type coerced like there's no tomorrow.</p>
<p>If you're a coding savant, the elegance of this will probably make you weep tears of syntactic joy, and the clever brevity of this isn't lost on me.</p>
<p>However, coming to this, without too much knowledge of the Ruby extensions project, or <a href="http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html" title="PragDave: Symbol#to_proc">someone to talk you through what's happening</a> is likely to be a frustrating experience.</p>
<p>Hope fully this will save time for someone else in future.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://chrisadams.me.uk/wordpress/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://chrisadams.me.uk/2009/05/03/frustrating-and-cryptic-ruby-idioms-1-of-a-series/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
