<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.6" -->
<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/"
	>

<channel>
	<title>Of Mice and Men</title>
	<link>http://www.gnejs.net</link>
	<description>Just another WordPress weblog</description>
	<pubDate>Fri, 08 Aug 2008 11:13:54 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.6</generator>
	<language>en</language>
			<item>
		<title>Improved &#8216;try()&#8217;</title>
		<link>http://www.gnejs.net/2008/03/02/improved-try/</link>
		<comments>http://www.gnejs.net/2008/03/02/improved-try/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 14:44:52 +0000</pubDate>
		<dc:creator>Anders Engström</dc:creator>
		
		<category>Ruby</category>

		<guid isPermaLink="false">http://www.gnejs.net/2008/03/02/improved-try/</guid>
		<description><![CDATA[I recently discovered the nice &#8216;try()&#8217; hack that chris@ozmm.org created.

It has some resemblance to my Array#to_proc - so I though that I would try to create an extended &#8216;try()&#8217;  

It works with &#8220;nested methods&#8221; and supports a default return value.

Here we go:



class Object
  def try(*args)
      options = {:default [...]]]></description>
			<content:encoded><![CDATA[<p>I recently discovered the nice <a href="http://ozmm.org/posts/try.html">&#8216;try()&#8217; hack</a> that chris@ozmm.org created.</p>

<p>It has some resemblance to my <a href="http://www.gnejs.net/2007/04/18/arrayto_proc-nested-properties-for-to_proc-hack/">Array#to_proc</a> - so I though that I would try to create an extended &#8216;try()&#8217; <img src='http://www.gnejs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p>It works with &#8220;nested methods&#8221; and supports a default return value.</p>

<p>Here we go:</p>

<p><code><pre></p>

<pre><code>class Object
  def try(*args)
      options = {:default =&gt; nil}.merge(args.last.is_a?(Hash) ? args.pop : {})
      target = self # Initial target is self.
      while target &amp;&amp; mtd = args.shift
        target = target.send(mtd) if target.respond_to?(mtd)
      end

      return target || options[:default]
  end
end


class Person
  attr_accessor :name
  attr_accessor :address

end
class Address
  attr_accessor :street
end

def print_info(person, default = nil)
  puts &lt;&lt;-END
    ==============================================================
    Name: #{person.try(:name, :default =&gt; default)}
    Street: #{person.try(:address, :street, :default =&gt; default)}
  END
end

@person = Person.new
@person.name = "Frank Black"
@person.address = Address.new
@person.address.street = "xyz"
print_info(@person)

@person.address = nil
print_info(@person)
print_info(@person, "No Info Registered")
</code></pre>

<p></pre></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gnejs.net/2008/03/02/improved-try/feed/</wfw:commentRss>
		</item>
		<item>
		<title>&#8230;</title>
		<link>http://www.gnejs.net/2007/11/05/12/</link>
		<comments>http://www.gnejs.net/2007/11/05/12/#comments</comments>
		<pubDate>Sun, 04 Nov 2007 23:00:49 +0000</pubDate>
		<dc:creator>Anders Engström</dc:creator>
		
		<category>Uncategorized</category>

		<guid isPermaLink="false">http://www.gnejs.net/2007/11/05/12/</guid>
		<description><![CDATA[13949712720901ForOSX
]]></description>
			<content:encoded><![CDATA[<p><a href="http://blogs.sun.com/bblfish/entry/vote_for_java6_on_leopard">13949712720901ForOSX</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gnejs.net/2007/11/05/12/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Array#to_proc - Nested properties for to_proc hack.</title>
		<link>http://www.gnejs.net/2007/04/18/arrayto_proc-nested-properties-for-to_proc-hack/</link>
		<comments>http://www.gnejs.net/2007/04/18/arrayto_proc-nested-properties-for-to_proc-hack/#comments</comments>
		<pubDate>Tue, 17 Apr 2007 23:12:28 +0000</pubDate>
		<dc:creator>Anders Engström</dc:creator>
		
		<category>Ruby</category>

		<guid isPermaLink="false">http://www.gnejs.net/2007/04/18/arrayto_proc-nested-properties-for-to_proc-hack/</guid>
		<description><![CDATA[Ruby is really elegant when it come to extending core libraries with custom behavior.

I needed a way to apply the standard to_proc hack to nested method calls. Normally the to_proc implementation in Symbol allows you to replace

['abc', 'defg'].map{&#124;x&#124; x.size} #=&#62; [3, 4]


with

['abc, 'defg'].map(&#38;:size) #=&#62; [3, 4]


But this only applies to &#8220;first level&#8221; methods on the [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby is really elegant when it come to extending core libraries with custom behavior.</p>

<p>I needed a way to apply the standard <a href="http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html">to_proc hack</a> to nested method calls. Normally the to_proc implementation in Symbol allows you to replace</p>

<pre><code>['abc', 'defg'].map{|x| x.size} #=&gt; [3, 4]
</code></pre>

<p>with</p>

<pre><code>['abc, 'defg'].map(&amp;:size) #=&gt; [3, 4]
</code></pre>

<p>But this only applies to &#8220;first level&#8221; methods on the target reference. The following extension of the Array class allows specifying a &#8216;call chain&#8217; of methods:</p>

<pre><code>class Array
  def to_proc            
    proc{|target|        
      inject(target){|memo, arg| 
        break unless memo
        memo.send(arg)            
      }
    }
  end
end
</code></pre>

<p>So, in order to extract the <em>regnr</em> of an <strong>optional</strong> <em>default_trailer</em>:</p>

<pre><code>class Car &lt; ActiveRecord::Base
    has_one :default_trailer, :class_name =&gt; 'Trailer'
end

class Trailer &lt; ActiveRecord::Base
    belongs_to :car
end
</code></pre>

<p>you can do:</p>

<pre><code>[car1, car2, car3].map(&amp;[:default_trailer, :regnr]) 
    #=&gt; ['GH3456', nil, 'FF1234']                   
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.gnejs.net/2007/04/18/arrayto_proc-nested-properties-for-to_proc-hack/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
