<?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>Khelll&#039;s Blog &#187; Grails</title>
	<atom:link href="http://khelll.com/blog/category/grails/feed/" rel="self" type="application/rss+xml" />
	<link>http://khelll.com/blog</link>
	<description>Cool Web Development...</description>
	<lastBuildDate>Thu, 13 Oct 2011 03:48:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Delegation in Ruby</title>
		<link>http://khelll.com/blog/ruby/delegation-in-ruby/</link>
		<comments>http://khelll.com/blog/ruby/delegation-in-ruby/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 02:25:57 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[delegation]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=331</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/delegation-in-ruby/' addthis:title='Delegation in Ruby '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>This is my first article in http://railsmagazine.com, it was published in issue 1, so basically I&#8217;m just republishing it here again. &#8220;Separate changeable parts from others that remain the same&#8221; and &#8220;composition is preferred to inheritance&#8221; are 2 common design &#8230; <a href="http://khelll.com/blog/ruby/delegation-in-ruby/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/delegation-in-ruby/' addthis:title='Delegation in Ruby' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/delegation-in-ruby/' addthis:title='Delegation in Ruby '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><p><em>This is my first article in <a href="http://www.railsmagazine.com/">http://railsmagazine.com</a>, it was published in <a href="http://railsmagazine.com/issues/1">issue 1</a>, so basically I&#8217;m just republishing it here again.</em></p>
<p>&#8220;Separate changeable parts from others that remain the same&#8221; and &#8220;composition is preferred to inheritance&#8221; are 2 common design principles when you start designing in OOP world. However and while the first seems to be logical, a one might wonder why it&#8217;s preferable to use composition over inheritance, and that&#8217;s a logical question, lets answer it via an example:</p>
<p>Let&#8217;s suppose that we have a Robot that has a heat sensor, a one would write a very simple UML:</p>
<p><img src="http://www.khelll.com/public/images/delegation_in_ruby/robot.png" /></p>
<p>This design has several drawbacks:</p>
<p>1.There is a strong probability to have another type of robots that don&#8217;t have heat sensors(breaks the first design principle: separate changeable code from static one).<br />
2.Whenever I want to modify anything related to the heat sensor, I have to change the robot class(breaks the first design principle).<br />
3.Exposure of heat sensor methods to in Robot class.</p>
<p>Let&#8217;s enhance this class a bit:</p>
<p><img src="http://www.khelll.com/public/images/delegation_in_ruby/volcano_robot.png" /></p>
<p>Well, now this is an inheritance based design and it solves the first problem, but it&#8217;s still incapable to solve the other 2 problems related to the heat sensor. Let&#8217;s do another enhancement:</p>
<p><img src="http://www.khelll.com/public/images/delegation_in_ruby/heat_sensor.png"/></p>
<p>Now this is a typical design, based on composition rather than inheritance, where we could solve the above 3 problems, and moreover we gained a new thing:  we can now abstract the HeatSensor for future uses.</p>
<h2>Now what&#8217;s delegation? </h2>
<p>Delegation is the process of delegating functionality to the contained parts.<br />
If you look carefully at the previous figure, you will notice that the VolcanoRobot is still having the 3 methods that are related to the sensor, well those are a wrapper methods, they do nothing but to call the sensor corresponding ones, and that&#8217;s exactly what delegation is, just delegate functionality to the contained parts(delegates).<br />
Delegation comes along with composition to provide a flexible neat solutions like the one we had above, and also it serves the principle &#8220;separate changeable code from static one&#8221; ,but that also comes with a tax: a need of wrapper methods, and extra time needed in processing because of the call of these wrapper methods.</p>
<h2>Ruby and delegation</h2>
<p>Now let&#8217;s have a code example:<br />
We have a multi purpose Robot that has an arm and a heat sensor, the robot does several jobs, like packaging boxes, stacking them and measuring the heat.<br />
we will use composition and delegation as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Robot
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@heat_sensor</span> = HeatSensor.<span style="color:#9900CC;">new</span>
    <span style="color:#0066ff; font-weight:bold;">@arm</span> = RobotArm.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> measure_heat<span style="color:#006600; font-weight:bold;">&#40;</span>scale=<span style="color:#996600;">&quot;c&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@heat_sensor</span>.<span style="color:#9900CC;">measure</span><span style="color:#006600; font-weight:bold;">&#40;</span>scale<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> stack<span style="color:#006600; font-weight:bold;">&#40;</span>boxes_number=<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@arm</span>.<span style="color:#9900CC;">stack</span><span style="color:#006600; font-weight:bold;">&#40;</span>boxes_number<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> package
    <span style="color:#0066ff; font-weight:bold;">@arm</span>.<span style="color:#9900CC;">package</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> HeatSensor
  <span style="color:#008000; font-style:italic;">#Celsius or Fahrenheit scale</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> measure<span style="color:#006600; font-weight:bold;">&#40;</span>scale=<span style="color:#996600;">&quot;c&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    t = <span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    t = scale==<span style="color:#996600;">&quot;c&quot;</span> ? t : t <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Heat is #{t}° #{scale.upcase}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> RobotArm
  <span style="color:#9966CC; font-weight:bold;">def</span> stack<span style="color:#006600; font-weight:bold;">&#40;</span>boxes_number=<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Stacking #{boxes_number} box(es)&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> package
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Packaging&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
robo = Robot.<span style="color:#9900CC;">new</span> <span style="color:#008000; font-style:italic;">#=&gt;#&lt;Robot:0xb75131e8 @arm=#&lt;RobotArm:0xb75131ac&gt;, @heat_sensor=#&lt;HeatSensor:0xb75131c0&gt;&gt;</span>
robo.<span style="color:#9900CC;">stack</span> <span style="color:#006666;">2</span> <span style="color:#008000; font-style:italic;">#=&gt;Stacking 2 box(es)</span>
robo.<span style="color:#9900CC;">package</span> <span style="color:#008000; font-style:italic;">#=&gt;Packaging</span>
robo.<span style="color:#9900CC;">measure_heat</span> <span style="color:#008000; font-style:italic;">#=&gt; Heat is 59° C</span></pre></div></div>

<p>As you can see, i have 3 wrapper methods(stack,package and measure_heat) in Robot class that are doing nothing but to call the contained objects corresponding methods.<br />
This is really a nasty thing, specially when there are lots of contained objects.<br />
However there are 2 libs that comes to the rescue to in ruby, Forwardable and Delegate. Let&#8217;s check them one by one.</p>
<h2>Forwardable lib</h2>
<p><a href="http://www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/index.html">Forwardable lib</a> is library that supports delegation, it has 2 modules Forwardable and SingleForwardable: </p>
<h3>Forwardable module</h3>
<p>The <a href="http://www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/classes/Forwardable.html">Forwardable</a> module provides delegation of specified methods to a designated object, using the methods <a href="http://www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/classes/Forwardable.html#M000736">def_delegator</a> and <a href="http://www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/classes/Forwardable.html#M000735">def_delegators</a>. </p>
<p>def_delegator(obj, method, alias = method) : Defines a method method which delegates to obj. If alias is provided, it is used as the name for the delegate method.</p>
<p>def_delegators(obj, *methods): Shortcut for defining multiple delegator methods, but with no provision for using a different name.</p>
<p>Let&#8217;s refactor our robot example to make it Forwardable module:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'forwardable'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Robot
  <span style="color:#008000; font-style:italic;"># Extending provides class methods</span>
  extend <span style="color:#CC00FF; font-weight:bold;">Forwardable</span>
  <span style="color:#008000; font-style:italic;"># Use of  def_delegators</span>
  def_delegators :@arm,:package,:stack
  <span style="color:#008000; font-style:italic;"># Use of  def_delegator</span>
  def_delegator :@heat_sensor, <span style="color:#ff3333; font-weight:bold;">:measure</span> ,:measure_heat
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@heat_sensor</span> = HeatSensor.<span style="color:#9900CC;">new</span>
    <span style="color:#0066ff; font-weight:bold;">@arm</span> = RobotArm.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> HeatSensor
  <span style="color:#008000; font-style:italic;">#Celsius or Fahrenheit scale</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> measure<span style="color:#006600; font-weight:bold;">&#40;</span>scale=<span style="color:#996600;">&quot;c&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    t = <span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    t = scale==<span style="color:#996600;">&quot;c&quot;</span> ? t : t <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Heat is #{t}° #{scale.upcase}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> RobotArm
  <span style="color:#9966CC; font-weight:bold;">def</span> stack<span style="color:#006600; font-weight:bold;">&#40;</span>boxes_number=<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Stacking #{boxes_number} box(es)&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> package
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Packaging&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Well, that&#8217;s a neater solution as you can see.</p>
<h3>
SingleForwardable module</h3>
<p>The <a href="http://www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/classes/SingleForwardable.html">SingleForwardable</a> module provides delegation of specified methods to a designated object, using the methods <a href="http://www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/classes/SingleForwardable.html#M000732>def_delegator</a> and <a href="http://www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/classes/SingleForwardable.html#M000731">def_delegators</a>. This module is similar to Forwardable, but it works on objects themselves, instead of their defining classes.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;forwardable&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;date&quot;</span>
date = <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span> <span style="color:#008000; font-style:italic;">#=&gt; #&lt;Date: 4909665/2,0,2299161&gt;</span>
<span style="color:#008000; font-style:italic;"># Prepare object for delegation</span>
date.<span style="color:#9900CC;">extend</span> <span style="color:#CC00FF; font-weight:bold;">SingleForwardable</span> <span style="color:#008000; font-style:italic;">#=&gt; #&lt;Date: 4909665/2,0,2299161&gt;</span>
<span style="color:#008000; font-style:italic;"># Add delegation for Time.now</span>
date.<span style="color:#9900CC;">def_delegator</span> :<span style="color:#CC00FF; font-weight:bold;">Time</span>, <span style="color:#996600;">&quot;now&quot;</span>,<span style="color:#996600;">&quot;with_time&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> date.<span style="color:#9900CC;">with_time</span> <span style="color:#008000; font-style:italic;">#=&gt;Thu Jan 01 23:03:04 +0200 2009</span></pre></div></div>

<h3>Delegate Lib</h3>
<p><a href="http://www.ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html">Delegate lib</a> is another lib that provides delegation, i&#8217;ll explain 2 ways to use it:</p>
<h4>
DelegateClass method</h4>
<p>Use the top level <a href="http://www.ruby-doc.org/stdlib/libdoc/delegate/rdoc/files/delegate_rb.html#M000406">DelegateClass</a> method which allows you to easily setup delegation through class inheritance. In the following example, I want to make a new class called CurrentDate, which holds the current date and some extra methods, at the same time I&#8217;m delegating to normal date objects:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;delegate&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;date&quot;</span>
<span style="color:#008000; font-style:italic;"># Notice the class definition</span>
<span style="color:#9966CC; font-weight:bold;">class</span> CurrentDate <span style="color:#006600; font-weight:bold;">&lt;</span> DelegateClass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Date</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@date</span> = <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span>
    <span style="color:#008000; font-style:italic;"># Pass the object to be delegated to the superclass. </span>
    <span style="color:#9966CC; font-weight:bold;">super</span><span style="color:#006600; font-weight:bold;">&#40;</span>@date<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> to_s
    <span style="color:#0066ff; font-weight:bold;">@date</span>.<span style="color:#9900CC;">strftime</span> <span style="color:#996600;">&quot;%Y/%m/%d&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> with_time
    <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
cdate = CurrentDate.<span style="color:#9900CC;">new</span>
<span style="color:#008000; font-style:italic;"># Notice how delegation works</span>
<span style="color:#008000; font-style:italic;"># Instead of doing cdate.date.day and defining attr_accessor for the date , i'm doing c.day</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> cdate.<span style="color:#9900CC;">day</span> <span style="color:#008000; font-style:italic;">#=&gt;1</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> cdate.<span style="color:#9900CC;">month</span> <span style="color:#008000; font-style:italic;">#=&gt;1</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> cdate.<span style="color:#9900CC;">year</span> <span style="color:#008000; font-style:italic;">#=&gt;2009</span>
<span style="color:#008000; font-style:italic;"># Testing added methods</span>
<span style="color:#008000; font-style:italic;"># to_s</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> cdate <span style="color:#008000; font-style:italic;">#=&gt; 2009/01/01</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> cdate.<span style="color:#9900CC;">with_time</span> <span style="color:#008000; font-style:italic;">#=&gt; Thu Jan 01 23:22:20 +0200 2009</span></pre></div></div>

<h4>SimpleDelegator class</h4>
<p>Use it to delegate to an object that might be changed:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;delegate&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;date&quot;</span>
today = <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span> <span style="color:#008000; font-style:italic;">#=&gt; #&lt;Date: 4909665/2,0,2299161&gt;</span>
yesterday = today â€“ <span style="color:#006666;">1</span> <span style="color:#008000; font-style:italic;">#=&gt; #&lt;Date: 4909663/2,0,2299161&gt;</span>
date = <span style="color:#CC00FF; font-weight:bold;">SimpleDelegator</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>today<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; #&lt;Date: 4909665/2,0,2299161&gt;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> date <span style="color:#008000; font-style:italic;">#=&gt;2009-01-01</span>
<span style="color:#008000; font-style:italic;"># Use __setobj__ to change the delegate</span>
date.__setobj__<span style="color:#006600; font-weight:bold;">&#40;</span>yesterday<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#008000; font-style:italic;">#=&gt; #&lt;Date: 4909663/2,0,2299161&gt;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> date <span style="color:#008000; font-style:italic;">#=&gt;2008-12-31</span></pre></div></div>

<p>As you can see, we made 2 objects and then delegated to them consequently.</p>
<h2>
What about Rails?</h2>
<p>Rails adds new functionality called  &#8220;delegate&#8221;:<br />
Which provides a <a href="http://api.rubyonrails.com/classes/Module.html#M000048">delegate</a> class method to easily expose contained objectsâ€™ methods as your own. Pass one or more methods (specified as symbols or strings) and the name of the target object as the final :to option (also a symbol or string). At least one method and the :to option are required. </p>
<p>go to your console and create a dummy project ,then cd to that project, and fire the rails console:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ rails dummy 
â€¦...
$ <span style="color: #7a0874; font-weight: bold;">cd</span> dummy
<span style="color: #007800;">$ruby</span> script<span style="color: #000000; font-weight: bold;">/</span>console
Loading development environment <span style="color: #7a0874; font-weight: bold;">&#40;</span>Rails 2.2.2<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;</span> Person = Struct.new<span style="color: #7a0874; font-weight: bold;">&#40;</span>:name, :address<span style="color: #7a0874; font-weight: bold;">&#41;</span>
=<span style="color: #000000; font-weight: bold;">&gt;</span> Person
<span style="color: #000000; font-weight: bold;">&gt;&gt;</span> class Invoice <span style="color: #000000; font-weight: bold;">&lt;</span> Struct.new<span style="color: #7a0874; font-weight: bold;">&#40;</span>:client<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;</span>   delegate :name, :address, :to =<span style="color: #000000; font-weight: bold;">&gt;</span> :client
<span style="color: #000000; font-weight: bold;">&gt;&gt;</span> end
=<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>:name, :address<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;</span> john_doe = Person.new<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;John Doe&quot;</span>, <span style="color: #ff0000;">&quot;Vimmersvej 13&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
=<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #666666; font-style: italic;">#&lt;struct Person name=&quot;John Doe&quot;, address=&quot;Vimmersvej 13&quot;&gt;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;</span> invoice = Invoice.new<span style="color: #7a0874; font-weight: bold;">&#40;</span>john_doe<span style="color: #7a0874; font-weight: bold;">&#41;</span>
=<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #666666; font-style: italic;">#&lt;struct Invoice client=#&lt;struct Person name=&quot;John Doe&quot;, address=&quot;Vimmersvej 13&quot;&gt;&gt;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;</span> invoice.name
=<span style="color: #000000; font-weight: bold;">&gt;</span> John Doe
<span style="color: #000000; font-weight: bold;">&gt;&gt;</span> invoice.address
=<span style="color: #000000; font-weight: bold;">&gt;</span>Vimmersvej <span style="color: #000000;">13</span></pre></div></div>

<p>I strongly urge you to check the whole provided examples in rails API documetation,to check also how to use this effectively with ActiveRecord.</p>
<p>Before I finish this article I want to share you the code of delegate method form rails API documentation, I&#8217;ll add some comments on the code to explain you what is going on:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#9966CC; font-weight:bold;">Module</span>
  <span style="color:#008000; font-style:italic;"># Delegate method </span>
  <span style="color:#008000; font-style:italic;"># It expects an array of arguments that contains the methods to be delegated </span>
  <span style="color:#008000; font-style:italic;"># and a hash of options</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> delegate<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>methods<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># Pop up the options hash from arguments array</span>
    options = methods.<span style="color:#9900CC;">pop</span>
    <span style="color:#008000; font-style:italic;"># Check the availability of the options hash and more specifically the :to option</span>
    <span style="color:#008000; font-style:italic;"># Raises an error if one of them is not there</span>
    <span style="color:#9966CC; font-weight:bold;">unless</span> options.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> to = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:to</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span>, <span style="color:#996600;">&quot;Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to =&gt; :greeter).&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Make sure the :to option follows syntax rules for method names </span>
    <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:prefix</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:to</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span> =~ <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#91;</span>^a<span style="color:#006600; font-weight:bold;">-</span>z_<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">/</span>
      <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span>, <span style="color:#996600;">&quot;Can only automatically set the delegation prefix when delegating to a method.&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Set the real prefix value </span>
    prefix = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:prefix</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> <span style="color:#996600;">&quot;#{options[:prefix] == true ? to : options[:prefix]}_&quot;</span>
&nbsp;
   <span style="color:#008000; font-style:italic;"># Here comes the magic of ruby :) </span>
   <span style="color:#008000; font-style:italic;"># Reflection techniques are used here:</span>
   <span style="color:#008000; font-style:italic;"># module_eval is used to add new methods on the fly which:</span>
   <span style="color:#008000; font-style:italic;"># expose the contained methods' objects</span>
    methods.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>method<span style="color:#006600; font-weight:bold;">|</span>
      module_eval<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;def #{prefix}#{method}(*args, &amp;block)<span style="color:#000099;">\n</span>#{to}.__send__(#{method.inspect}, *args, &amp;block)<span style="color:#000099;">\n</span>end<span style="color:#000099;">\n</span>&quot;</span>, <span style="color:#996600;">&quot;(__DELEGATION__)&quot;</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</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></div></div>

<p>That&#8217;s it for this article, we have covered 5 points:</p>
<p>1-Composition vs inheritance.<br />
2-What delegation is, and why it&#8217;s used.<br />
3-Ruby Forwardable lib.<br />
4-Ruby Delegate lib.<br />
5-Rails delegate method.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/delegation-in-ruby/' addthis:title='Delegation in Ruby' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://khelll.com/blog/ruby/delegation-in-ruby/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Grails and Arabic localization</title>
		<link>http://khelll.com/blog/grails/grails-and-arabic-localization/</link>
		<comments>http://khelll.com/blog/grails/grails-and-arabic-localization/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 16:08:29 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[arabic]]></category>
		<category><![CDATA[localization]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=35</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/grails/grails-and-arabic-localization/' addthis:title='Grails and Arabic localization '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>A little problem that i faced with grails , was setting the locale dynamically, i followed this nice article , and here is how i could manage it : I made a new file called messages_ar.properties that contains the arabic &#8230; <a href="http://khelll.com/blog/grails/grails-and-arabic-localization/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/grails/grails-and-arabic-localization/' addthis:title='Grails and Arabic localization' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/grails/grails-and-arabic-localization/' addthis:title='Grails and Arabic localization '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><p>A little problem that i faced with grails , was setting the locale dynamically, i followed this <a href="http://www.grassr.com/wordpress/?p=12">nice article</a> , and here is how i could manage it :</p>
<ol>
<li>I made a new file called messages_ar.properties that contains the arabic localization under the i18n folder.</li>
<li>I made a simple beforeinterceptor:

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> beforeInterceptor <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">def</span> key <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER&quot;</span>
        <span style="color: #000000; font-weight: bold;">def</span> localeResolver <span style="color: #66cc66;">=</span> request.<span style="color: #006600;">getAttribute</span><span style="color: #66cc66;">&#40;</span>key<span style="color: #66cc66;">&#41;</span>
        localeResolver.<span style="color: #006600;">setLocale</span><span style="color: #66cc66;">&#40;</span>request, response, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Locale</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ar&quot;</span>,<span style="color: #ff0000;">&quot;JO&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span></pre></div></div>

</li>
</ol>
<p>Of course you can use <a href="http://grails.org/Filters">grails/Filters</a> to save yourself repeating the same code in every controller.</p>
<p>However what i want to mention is the locale object, which in case of Arabic localization, was seeking  the country argument ( &#8220;JO&#8221; in our case) :</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Locale</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ar&quot;</span>,<span style="color: #ff0000;">&quot;JO&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>While for other localizations like German one it doesn&#8217;t seek this second argument, so this snippet of code will work for them <img src='http://khelll.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Locale</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;de&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Also you can change this country argument value to match this rule :</p>
<blockquote><p>
The country argument is a valid ISO Country Code. These codes are the upper-case, two-letter codes as defined by ISO-3166. You can find a full list of these codes at a number of sites, such as:</p>
<p>http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html</p></blockquote>
<p>I hope that was helpful <img src='http://khelll.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  , enjoy!</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/grails/grails-and-arabic-localization/' addthis:title='Grails and Arabic localization' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://khelll.com/blog/grails/grails-and-arabic-localization/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

