<?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>Khaled alHabache's official blog &#187; dynamic method calling</title>
	<atom:link href="http://www.khelll.com/blog/tag/dynamic-method-calling/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.khelll.com/blog</link>
	<description>What web development means....</description>
	<lastBuildDate>Tue, 16 Mar 2010 23:21:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby dynamic method calling</title>
		<link>http://www.khelll.com/blog/ruby/ruby-dynamic-method-calling/</link>
		<comments>http://www.khelll.com/blog/ruby/ruby-dynamic-method-calling/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 13:36:08 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[dynamic method calling]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=19</guid>
		<description><![CDATA[I&#8217;m pretty sure that you have heard lots about ruby, specially as being a dynamic language, you can create methods on the fly, add instance variables, define constants and invoke existing methods dynamically , and that&#8217;s what this post is all about :
As you know in ruby you can call a public instance method directly [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m pretty sure that you have heard lots about ruby, specially as being a dynamic language, you can create methods on the fly, add instance variables, define constants and invoke existing methods dynamically , and that&#8217;s what this post is all about :</p>
<p>As you know in ruby you can call a public instance method directly ,ex :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">s= <span style="color:#996600;">&quot;hi man&quot;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> s.<span style="color:#9900CC;">length</span> <span style="color:#008000; font-style:italic;">#=&gt; 6</span>
<span style="color:#CC0066; font-weight:bold;">p</span> s.<span style="color:#9966CC; font-weight:bold;">include</span>? <span style="color:#996600;">&quot;hi&quot;</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span></pre></div></div>

<p>One way to invoke a method dynamically in ruby is to send a message to the object :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> s.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:length</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 6</span>
<span style="color:#CC0066; font-weight:bold;">p</span> s.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:include</span>?,<span style="color:#996600;">&quot;hi&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span></pre></div></div>

<p>A second way is instantiate a method object and then call it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">method_object = s.<span style="color:#9900CC;">method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:length</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
<span style="color:#CC0066; font-weight:bold;">p</span> method_object.<span style="color:#9900CC;">call</span> <span style="color:#008000; font-style:italic;">#=&gt; 6</span>
method_object = s.<span style="color:#9900CC;">method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:include</span>?<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> method_object.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'hi'</span><span style="color:#006600; font-weight:bold;">&#41;</span>  <span style="color:#008000; font-style:italic;">#=&gt; true</span></pre></div></div>

<p>And the third way is to use the eval method:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">eval</span> <span style="color:#996600;">&quot;s.length&quot;</span> <span style="color:#008000; font-style:italic;">#=&gt; 6</span>
<span style="color:#CC0066; font-weight:bold;">eval</span> <span style="color:#996600;">&quot;s.include? 'hi'&quot;</span> <span style="color:#008000; font-style:italic;">#=&gt;true</span></pre></div></div>

<p>Well, when to use what?</p>
<p>look at this script, it will be used to benchmark the 3 ways of calling :</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;benchmark&quot;</span> 
test = <span style="color:#996600;">&quot;hi man&quot;</span> 
m = test.<span style="color:#9900CC;">method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:length</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
n = <span style="color:#006666;">100000</span> 
<span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bmbm</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> 
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;call&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> m.<span style="color:#9900CC;">call</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> 
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;send&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> test.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:length</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> 
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;eval&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#CC0066; font-weight:bold;">eval</span> <span style="color:#996600;">&quot;test.length&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> 
<span style="color:#006600; font-weight:bold;">&#125;</span> 
<span style="color:#008000; font-style:italic;">#######################################</span>
<span style="color:#008000; font-style:italic;">#####   The results</span>
<span style="color:#008000; font-style:italic;">#######################################</span>
<span style="color:#008000; font-style:italic;">#Rehearsal ----------------------------------------</span>
<span style="color:#008000; font-style:italic;">#call   0.050000   0.020000   0.070000 (  0.077915)</span>
<span style="color:#008000; font-style:italic;">#send   0.080000   0.000000   0.080000 (  0.086071)</span>
<span style="color:#008000; font-style:italic;">#eval   0.360000   0.040000   0.400000 (  0.405647)</span>
<span style="color:#008000; font-style:italic;">#------------------------------- total: 0.550000sec</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#          user     system      total        real</span>
<span style="color:#008000; font-style:italic;">#call   0.050000   0.020000   0.070000 (  0.072041)</span>
<span style="color:#008000; font-style:italic;">#send   0.070000   0.000000   0.070000 (  0.077674)</span>
<span style="color:#008000; font-style:italic;">#eval   0.370000   0.020000   0.390000 (  0.399442)</span></pre></div></div>

<p>Well as you can see, instantiating a method object is the fastest dynamic way in calling a method, also notice how slow using eval is.</p>
<p>Also when sending a message to an object , or when instantiating a method object , u can call private methods of that object :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foo
  private  
  <span style="color:#9966CC; font-weight:bold;">def</span> hi 
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;hi man&quot;</span> 
  <span style="color:#9966CC; font-weight:bold;">end</span> 
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Normal method calling</span>
f = Foo.<span style="color:#9900CC;">new</span>  <span style="color:#008000; font-style:italic;">#=&gt; &lt;Foo:0x10a0d51&gt;</span>
f.<span style="color:#9900CC;">hi</span>  <span style="color:#008000; font-style:italic;">#=&gt;NoMethodError: private method `hi' called for #&lt;Foo:0x10a0d51&gt; </span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Sending a message</span>
f.<span style="color:#9900CC;">send</span> <span style="color:#ff3333; font-weight:bold;">:hi</span> <span style="color:#008000; font-style:italic;">#  hi man</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Instantiating a method object</span>
f.<span style="color:#9900CC;">method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:hi</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">call</span>  <span style="color:#008000; font-style:italic;"># hi man</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Using eval</span>
<span style="color:#CC0066; font-weight:bold;">eval</span> <span style="color:#996600;">&quot;f.hi&quot;</span>  <span style="color:#008000; font-style:italic;">#=&gt;NoMethodError: private method `hi' called for #&lt;Foo:0x10a0d51&gt; </span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Using instance_eval</span>
f.<span style="color:#9900CC;">instance_eval</span> <span style="color:#006600; font-weight:bold;">&#123;</span>hi<span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#008000; font-style:italic;"># hi man</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/ruby-dynamic-method-calling/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
