<?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; metaprogramming</title>
	<atom:link href="http://www.khelll.com/blog/category/metaprogramming/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 introspection 2</title>
		<link>http://www.khelll.com/blog/ruby/ruby-introspection-2/</link>
		<comments>http://www.khelll.com/blog/ruby/ruby-introspection-2/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 18:24:04 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[introspection]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=59</guid>
		<description><![CDATA[I wanted to start blogging on ruby reflection api, but i just realized that i have to give a second part of my previous article on ruby introspection  .
So here we go:

s = ''
#  s.is_a? String, this is the same!
String === s
&#160;
# Determines whether the object has a public or protected method with [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to start blogging on ruby reflection api, but i just realized that i have to give a second part of my previous article on <a href="http://www.khelll.com/blog/ruby/ruby-introspection">ruby introspection </a> .<br />
So here we go:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">s = <span style="color:#996600;">''</span>
<span style="color:#008000; font-style:italic;">#  s.is_a? String, this is the same!</span>
<span style="color:#CC0066; font-weight:bold;">String</span> === s
&nbsp;
<span style="color:#008000; font-style:italic;"># Determines whether the object has a public or protected method with the specified name.</span>
s.<span style="color:#9900CC;">respond_to</span>? <span style="color:#ff3333; font-weight:bold;">:slice</span> <span style="color:#008000; font-style:italic;">#=&gt;true</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Or</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">respond_to</span>? <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#008000; font-style:italic;">#=&gt; false</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Passes true as the second argument to check private methods as well.</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">respond_to</span>? <span style="color:#ff3333; font-weight:bold;">:include</span> , <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Check whether some module includes another</span>
<span style="color:#9966CC; font-weight:bold;">module</span> M1 ; <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">module</span> M2 ; <span style="color:#9966CC; font-weight:bold;">include</span> M1 ; <span style="color:#9966CC; font-weight:bold;">end</span>
M2.<span style="color:#9966CC; font-weight:bold;">include</span>? M1 <span style="color:#008000; font-style:italic;">#=&gt; true</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Check whether instance variable is defined or not.</span>
d=<span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">new</span>
d.<span style="color:#9900CC;">instance_variables</span> <span style="color:#008000; font-style:italic;">#=&gt; [&quot;@sg&quot;, &quot;@of&quot;, &quot;@ajd&quot;]</span>
d.<span style="color:#9900CC;">instance_variable_defined</span>? <span style="color:#996600;">&quot;@of&quot;</span> <span style="color:#008000; font-style:italic;">#=&gt;true</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># I have to clarify this, a note for the previous post:</span>
<span style="color:#008000; font-style:italic;"># obj.public_methods(all=true) ,returns the list of public methods accessible to obj. If the all parameter is set to false, inherited methods will be excluded.  </span>
<span style="color:#008000; font-style:italic;"># obj.protected_methods(all=true),returns the list of protected methods accessible to obj. If the all parameter is set to false, inherited methods will be excluded.  </span>
<span style="color:#008000; font-style:italic;"># obj.private_methods(all=true),returns the list of private methods accessible to obj. If the all parameter is set to false, inherited methods will be excluded.</span>
<span style="color:#008000; font-style:italic;"># Ex:</span>
d.<span style="color:#9900CC;">public_methods</span> <span style="color:#008000; font-style:italic;">#=&gt; [&quot;&gt;&gt;&quot;, &quot;between?&quot;, &quot;h!&quot;, &quot;methods&quot;, &quot;send&quot;, &quot;ctime&quot;, &quot;history_write&quot;, &quot;year&quot;, &quot;h&quot;, &quot;taint&quot;, &quot;to_enum&quot;, &quot;instance_variable_defined?&quot;, &quot;history&quot;, &quot;ld&quot;, &quot;_dump&quot;,&quot;q&quot;, &quot;singleton_methods&quot;, &quot;instance_eval&quot;, &quot;os?&quot;, &quot;wday&quot;, &quot;enum_for&quot;, &quot;nil?&quot;, &quot;succ&quot;, &quot;pretty_print_cycle&quot;, &quot;asctime&quot;, &quot;po&quot;, &quot;protected_methods&quot;, &quot;instance_exec&quot;, &quot;start&quot;, &quot;tainted?&quot;, &quot;ns?&quot;, &quot;handling_jruby_bug&quot;, &quot;new_start&quot;, &quot;yday&quot;, &quot;untaint&quot;, &quot;instance_of?&quot;, &quot;equal?&quot;, &quot;taguri&quot;, &quot;pretty_print&quot;, &quot;julian?&quot;, &quot;step&quot;, &quot;amjd&quot;,&quot;hash&quot;, &quot;poc&quot;, &quot;private_methods&quot;, &quot;jd&quot;, &quot;newsg&quot;, &quot;taguri=&quot;, &quot;history_to_vi&quot;, &quot;pretty_print_instance_variables&quot;, &quot;ajd&quot;, &quot;italy&quot;, &quot;kind_of?&quot;, &quot;freeze&quot;, &quot;eql?&quot;, &quot;next&quot;, &quot;to_yaml_style&quot;, &quot;id&quot;, &quot;sg&quot;, &quot;public_methods&quot;, &quot;hvi&quot;, &quot;quiet&quot;, &quot;england&quot;, &quot;is_a?&quot;, &quot;mday&quot;, &quot;tap&quot;, &quot;type&quot;, &quot;ri&quot;, &quot;to_yaml_properties&quot;, &quot;+&quot;, &quot;instance_variables&quot;, &quot;__id__&quot;, &quot;frozen?&quot;, &quot;-&quot;, &quot;julian&quot;, &quot;pretty_inspect&quot;, &quot;to_a&quot;, &quot;cwyear&quot;, &quot;respond_to?&quot;, &quot;upto&quot;, &quot;display&quot;, &quot;history_do&quot;, &quot;day&quot;, &quot;method&quot;, &quot;class&quot;, &quot;verbose&quot;, &quot;gregorian?&quot;, &quot;downto&quot;, &quot;mjd&quot;, &quot;strftime&quot;, &quot;&lt;=&gt;&quot;, &quot;instance_variable_get&quot;, &quot;==&quot;, &quot;__send__&quot;, &quot;leap?&quot;, &quot;===&quot;, &quot;gregorian&quot;, &quot;pretty_print_inspect&quot;, &quot;extend&quot;, &quot;to_s&quot;,&quot;cweek&quot;, &quot;&gt;=&quot;, &quot;v&quot;, &quot;mon&quot;, &quot;&lt;=&quot;, &quot;clone&quot;, &quot;to_yaml&quot;, &quot;=~&quot;, &quot;instance_variable_set&quot;, &quot;&lt;&quot;, &quot;&gt;&quot;, &quot;cwday&quot;, &quot;inspect&quot;, &quot;day_fraction&quot;, &quot;month&quot;, &quot;dup&quot;, &quot;object_id&quot;, &quot;&lt;&lt;&quot;]</span>
&nbsp;
d.<span style="color:#9900CC;">public_methods</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; [&quot;jd&quot;, &quot;month&quot;, &quot;_dump&quot;, &quot;ctime&quot;, &quot;ld&quot;, &quot;cweek&quot;, &quot;succ&quot;, &quot;to_s&quot;, &quot;step&quot;, &quot;day&quot;, &quot;gregorian?&quot;, &quot;hash&quot;, &quot;ajd&quot;, &quot;julian&quot;, &quot;+&quot;, &quot;yday&quot;, &quot;taguri&quot;, &quot;os?&quot;, &quot;strftime&quot;, &quot;italy&quot;, &quot;downto&quot;, &quot;-&quot;, &quot;eql?&quot;, &quot;sg&quot;, &quot;year&quot;, &quot;asctime&quot;, &quot;leap?&quot;, &quot;taguri=&quot;, &quot;inspect&quot;, &quot;amjd&quot;, &quot;cwday&quot;, &quot;to_yaml&quot;, &quot;mon&quot;, &quot;&lt;&lt;&quot;, &quot;gregorian&quot;, &quot;newsg&quot;, &quot;&gt;&gt;&quot;, &quot;start&quot;, &quot;cwyear&quot;, &quot;day_fraction&quot;, &quot;mday&quot;, &quot;ns?&quot;, &quot;mjd&quot;, &quot;england&quot;, &quot;upto&quot;, &quot;wday&quot;, &quot;&lt;=&gt;&quot;, &quot;julian?&quot;, &quot;new_start&quot;, &quot;===&quot;, &quot;next&quot;]</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Check if a method is defined</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">method_defined</span>? <span style="color:#ff3333; font-weight:bold;">:slice</span>! <span style="color:#008000; font-style:italic;"># =&gt; true </span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Check if public method is defined?</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">public_method_defined</span>? <span style="color:#ff3333; font-weight:bold;">:upcase</span>     <span style="color:#008000; font-style:italic;"># =&gt; true</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Check if protected method is defined?</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">protected_method_defined</span>? <span style="color:#ff3333; font-weight:bold;">:downcase</span>  <span style="color:#008000; font-style:italic;"># =&gt; false </span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Check if private method is defined?</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">private_method_defined</span>? <span style="color:#ff3333; font-weight:bold;">:initialize</span> <span style="color:#008000; font-style:italic;"># =&gt; true </span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Check local variables</span>
<span style="color:#CC0066; font-weight:bold;">local_variables</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Check class variables</span>
<span style="color:#9966CC; font-weight:bold;">class</span> One ; @@var1 = <span style="color:#006666;">1</span> ; <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Two <span style="color:#006600; font-weight:bold;">&lt;</span> One ; @@var2 = <span style="color:#006666;">2</span> ; <span style="color:#9966CC; font-weight:bold;">end</span>
One.<span style="color:#9900CC;">class_variables</span>   <span style="color:#008000; font-style:italic;">#=&gt; [&quot;@@var1&quot;]</span>
Two.<span style="color:#9900CC;">class_variables</span>   <span style="color:#008000; font-style:italic;">#=&gt; [&quot;@@var2&quot;, &quot;@@var1&quot;]</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Check constants</span>
<span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">constants</span> <span style="color:#008000; font-style:italic;">#=&gt; [&quot;PI&quot;, &quot;E&quot;]</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Check global variables</span>
<span style="color:#CC0066; font-weight:bold;">global_variables</span> <span style="color:#008000; font-style:italic;">#=&gt; [&quot;$fileutils_rb_have_lchmod&quot;, &quot;$binding&quot;, &quot;$-w&quot;, &quot;$:&quot;, &quot;$.&quot;, &quot;$KCODE&quot;, &quot;$-F&quot;, &quot;$*&quot;, &quot;$stderr&quot;, &quot;$,&quot;, &quot;$`&quot;, &quot;$-p&quot;, &quot;$\&quot;&quot;, &quot;$$&quot;, &quot;$&lt;&quot;, &quot;$@&quot;, &quot;$-v&quot;, &quot;$-i&quot;, &quot;$deferr&quot;, &quot;$\\&quot;, &quot;$=&quot;, &quot;$;&quot;, &quot;$PROGRAM_NAME&quot;, &quot;$stdout&quot;, &quot;$&amp;&quot;, &quot;$fileutils_rb_have_lchown&quot;, &quot;$-d&quot;, &quot;$LOAD_PATH&quot;, &quot;$-a&quot;, &quot;$VERBOSE&quot;, &quot;$FILENAME&quot;, &quot;$defout&quot;, &quot;$-0&quot;, &quot;$+&quot;, &quot;$0&quot;, &quot;$stdin&quot;, &quot;$~&quot;, &quot;$DEBUG&quot;, &quot;$-I&quot;, &quot;$_&quot;, &quot;$-K&quot;, &quot;$&gt;&quot;, &quot;$/&quot;, &quot;$'&quot;, &quot;$-l&quot;, &quot;$LOADED_FEATURES&quot;, &quot;$?&quot;, &quot;$SAFE&quot;, &quot;$!&quot;]</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># How to get the name of the current method?</span>
<span style="color:#008000; font-style:italic;"># Add this snippet of code to your logic somewhere</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> <span style="color:#CC00FF; font-weight:bold;">Kernel</span>
  private
  <span style="color:#008000; font-style:italic;"># Defined in ruby 1.9</span>
  <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>__method__<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> __method__
      <span style="color:#CC0066; font-weight:bold;">caller</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> =~ <span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">`([^']*)'/ and $1
    end
  end
end</span></pre></div></div>

<p>Also i strongly recommend that you look at the <a href="http://www.ruby-doc.org/core/classes/ObjectSpace.html">ObjectSpace</a> module which contains a number of routines that interact with the garbage collection facility and<strong> allow you to traverse all living objects with an iterator</strong>, however this is a little example taken from the official api documentation :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#ObjectSpace.each_object([module]) {|obj| ... } =&gt; fixnum</span>
<span style="color:#008000; font-style:italic;">#Calls the block once for each living, nonimmediate object in this Ruby process. If module is specified, calls the block for only those classes or modules that match (or are a subclass of) module. Returns the number of objects found. Immediate objects (Fixnums, Symbols true, false, and nil) are never returned. In the example below, each_object returns both the numbers we defined and several constants defined in the Math module.</span>
&nbsp;
a = <span style="color:#006666;">102.7</span>
b = <span style="color:#006666;">95</span>       <span style="color:#008000; font-style:italic;"># Won't be returned</span>
c = <span style="color:#006666;">12345678987654321</span>
count = <span style="color:#CC00FF; font-weight:bold;">ObjectSpace</span>.<span style="color:#9900CC;">each_object</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Numeric</span><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>x<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> x <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Total count: #{count}&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#=&gt;12345678987654321</span>
<span style="color:#008000; font-style:italic;">#102.7</span>
<span style="color:#008000; font-style:italic;">#2.71828182845905</span>
<span style="color:#008000; font-style:italic;">#3.14159265358979</span>
<span style="color:#008000; font-style:italic;">#2.22044604925031e-16</span>
<span style="color:#008000; font-style:italic;">#1.7976931348623157e+308</span>
<span style="color:#008000; font-style:italic;">#2.2250738585072e-308</span>
<span style="color:#008000; font-style:italic;">#Total count: 7</span></pre></div></div>

<p>Also if you feel like you are eager to see low level introspection , then check this <a href="http://eigenclass.org/hiki/class+hierarchy+introspection+evil.rb">great post</a>. </p>
<p>In my next article, am going to blog on ruby reflection api , hope to finish it soon <img src='http://www.khelll.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/ruby-introspection-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Ruby introspection</title>
		<link>http://www.khelll.com/blog/ruby/ruby-introspection/</link>
		<comments>http://www.khelll.com/blog/ruby/ruby-introspection/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 12:31:16 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[introspection]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=3</guid>
		<description><![CDATA[Hi, this is my first blog post!, i&#8217;m already done of reading this artilce on groovy&#8217;s lang introspection , and i wanted to submit the equivalent one for ruby, so all you need now is to fire your irb and follow me   :

# Whats is your class?
&#160;
a = 5
b = &#34;Hello&#34;
&#160;
# Whats is [...]]]></description>
			<content:encoded><![CDATA[<p>Hi, this is my first blog post!, i&#8217;m already done of reading <a title="groovy's introspection" href="http://noor.ojuba.org/2008/07/groovy-introspection-know-what-you-have/">this artilce</a> on groovy&#8217;s lang introspection , and i wanted to submit the equivalent one for ruby, so all you need now is to fire your irb and follow me <img src='http://www.khelll.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Whats is your class?</span>
&nbsp;
a = <span style="color:#006666;">5</span>
b = <span style="color:#996600;">&quot;Hello&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Whats is your class?</span>
<span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;Class of a : #{a.class} ,class of b : #{b.class}&quot;</span> <span style="color:#008000; font-style:italic;">#=&gt;&quot;Class of a : Fixnum ,class of b : String&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Whats is your super class?</span>
<span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;Super class of a : #{a.class.superclass} ,super class of b : #{b.class.superclass}&quot;</span> <span style="color:#008000; font-style:italic;">#=&gt;&quot;Super class of a : Integer ,super class of b : Object&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Is a.class = Fixnum ?</span>
a.<span style="color:#9900CC;">instance_of</span>? <span style="color:#CC00FF; font-weight:bold;">Fixnum</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Is a of type Integer (does it have Integer in it's class hierarchy)?</span>
a.<span style="color:#9900CC;">is_a</span>? <span style="color:#CC0066; font-weight:bold;">Integer</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Or this way:</span>
a.<span style="color:#9900CC;">kind_of</span>? <span style="color:#CC0066; font-weight:bold;">Integer</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Introspection, know all the details about classes :</span>
<span style="color:#008000; font-style:italic;"># List all ancestors(modules and classes) of a class</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">ancestors</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> a<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># List all modules included in a class</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">included_modules</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> m<span style="color:#006600; font-weight:bold;">&#125;</span> 
&nbsp;
<span style="color:#008000; font-style:italic;"># Check class hierarchy</span>
<span style="color:#CC0066; font-weight:bold;">String</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;">Comparable</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span>
<span style="color:#CC0066; font-weight:bold;">String</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC0066; font-weight:bold;">Integer</span> <span style="color:#008000; font-style:italic;">#=&gt; nil  , strings r not integers</span>
<span style="color:#CC00FF; font-weight:bold;">Object</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC0066; font-weight:bold;">String</span> <span style="color:#008000; font-style:italic;">#=&gt; false , Not all objects are strings</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># List ancestors of class type</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">ancestors</span>.<span style="color:#CC0066; font-weight:bold;">select</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span> a.<span style="color:#9966CC; font-weight:bold;">class</span>==<span style="color:#9966CC; font-weight:bold;">Class</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> c<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># List all methods available to an object</span>
b.<span style="color:#9900CC;">methods</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> m<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Get public instance methods</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">public_instance_methods</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> m<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Get protected instance methods</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">protected_instance_methods</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> m<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Get private instance methods</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">private_instance_methods</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> m<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Get class singleton methods</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">singleton_methods</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> m<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Get the instance variables of an object</span>
d = <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">new</span>
d.<span style="color:#9900CC;">instance_variables</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> i<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Get public instance methods</span>
d.<span style="color:#9900CC;">public_methods</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> m<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Get protected instance methods</span>
d.<span style="color:#9900CC;">protected_methods</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> m<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Get private instance methods</span>
d.<span style="color:#9900CC;">private_methods</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> m<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Get instance singleton methods</span>
d.<span style="color:#9900CC;">singleton_methods</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> m<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>As for the Dynamic method calling introduced in that article , <a title="ruby dynamic method calling" href="http://www.khelll.com/blog/ruby/ruby-dynamic-method-calling/">check this post</a>  <img src='http://www.khelll.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>*Update : Check <a href="http://www.khelll.com/blog/ruby/ruby-introspection-2/">the second part article</a> of ruby introspection, for more info on this topic.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/ruby-introspection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
