<?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/tag/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 reflection 2</title>
		<link>http://www.khelll.com/blog/ruby/ruby-reflection-2/</link>
		<comments>http://www.khelll.com/blog/ruby/ruby-reflection-2/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 07:00:04 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=135</guid>
		<description><![CDATA[This is the second post related to ruby&#8217;s reflection API, the previous post was an extensive intro to this topic. While the current one will be lighter somehow, it would require you to focus a bit more on the content.
Here we go:
Setting, getting and removing instance variables :

# Define a simple class M
class M ; [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second post related to ruby&#8217;s reflection API, the <a href="http://www.khelll.com/blog/ruby/ruby-reflection/">previous post</a> was an extensive intro to this topic. While the current one will be lighter somehow, it would require you to focus a bit more on the content.<br />
Here we go:</p>
<h3>Setting, getting and removing instance variables :</h3>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Define a simple class M</span>
<span style="color:#9966CC; font-weight:bold;">class</span> M ; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#008000; font-style:italic;">#=&gt; nil</span>
&nbsp;
m = M.<span style="color:#9900CC;">new</span> <span style="color:#008000; font-style:italic;">#=&gt; #&lt;M:0xb764b7e0&gt;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Set an attribute i with value 5, the attribute name should be a symbol prefixed with @</span>
m.<span style="color:#9900CC;">instance_variable_set</span> :@i , <span style="color:#006666;">5</span> <span style="color:#008000; font-style:italic;">#=&gt; 5 </span>
&nbsp;
<span style="color:#008000; font-style:italic;"># This will never work cause no attribute reader is defined for 'i'</span>
m.<span style="color:#9900CC;">i</span> <span style="color:#008000; font-style:italic;">#=&gt;NoMethodError: undefined method `i' for #&lt;M:0xb764b7e0 @i=5&gt; from (irb):4</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Get it the right way!</span>
m.<span style="color:#9900CC;">instance_variable_get</span> :@i <span style="color:#008000; font-style:italic;">#=&gt; 5</span></pre></div></div>

<p>The code above could be written in a simpler way, let&#8217;s define another attribute &#8216;v&#8217; for example :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Use class_eval , and pass it a block of code :</span>
M.<span style="color:#9900CC;">class_eval</span> <span style="color:#006600; font-weight:bold;">&#123;</span> attr_accessor <span style="color:#ff3333; font-weight:bold;">:v</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; nil</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Set and get the attribute v</span>
m.<span style="color:#9900CC;">v</span> = <span style="color:#006666;">8</span> <span style="color:#008000; font-style:italic;">#=&gt; 8</span>
m.<span style="color:#9900CC;">v</span> <span style="color:#008000; font-style:italic;">#=&gt; 8</span></pre></div></div>

<p>Now, let&#8217;s undefine the instance variable &#8216;i&#8217;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">m.<span style="color:#9900CC;">remove_instance_variable</span> :@i  <span style="color:#008000; font-style:italic;">#=&gt;NoMethodError: private method `remove_instance_variable' called for #&lt;M:0xb767d4c0 @v=8  from (irb):29</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Opps, a private method! let's bypass that with instance_eval</span>
m.<span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#123;</span>remove_instance_variable :@i<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; 5      ,</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Could be accomplished with :send</span>
m.<span style="color:#9900CC;">send</span> <span style="color:#ff3333; font-weight:bold;">:remove_instance_variable</span> , :@i <span style="color:#008000; font-style:italic;">#=&gt;5</span>
m.<span style="color:#9900CC;">i</span> <span style="color:#008000; font-style:italic;">#=&gt; nil</span></pre></div></div>

<h3>Setting, getting and removing class variables :</h3>
<p>As instance_variable_get and instance_variable_set, there are another variations applied to class variables, but unfortunately they are private in ruby 1.8, so let&#8217;s use class_eval to bypass that:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">M.<span style="color:#9900CC;">class_eval</span> <span style="color:#006600; font-weight:bold;">&#123;</span>class_variable_set :@@ci ,<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; 1</span>
M.<span style="color:#9900CC;">class_eval</span> <span style="color:#006600; font-weight:bold;">&#123;</span>class_variable_get :@@ci<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; 1</span>
M.<span style="color:#9900CC;">class_eval</span> <span style="color:#006600; font-weight:bold;">&#123;</span>remove_class_variable :@@ci <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; 1</span></pre></div></div>

<h3>Setting, getting and removing constants :</h3>
<p>How about constants?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">M.<span style="color:#9900CC;">const_set</span> <span style="color:#ff3333; font-weight:bold;">:CON</span> , <span style="color:#996600;">&quot;I'm a constant&quot;</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;I'm a constant&quot;</span>
M.<span style="color:#9900CC;">const_get</span> <span style="color:#ff3333; font-weight:bold;">:CON</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;I'm a constant&quot;</span>
M.<span style="color:#9900CC;">const_defined</span>? <span style="color:#ff3333; font-weight:bold;">:CON</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span>
&nbsp;
&nbsp;
<span style="color:#008000; font-style:italic;"># Let's remove it with &quot;send&quot; method</span>
M.<span style="color:#9900CC;">send</span> <span style="color:#ff3333; font-weight:bold;">:remove_const</span> , <span style="color:#ff3333; font-weight:bold;">:CON</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;I'm a constant&quot;</span>
M.<span style="color:#9900CC;">const_defined</span>? <span style="color:#ff3333; font-weight:bold;">:CON</span> <span style="color:#008000; font-style:italic;">#=&gt; false</span></pre></div></div>

<h3>define_method</h3>
<p>Now let&#8217;s move to a new topic:<br />
Define a method dynamically using <a href="http://www.ruby-doc.org/core/classes/Module.html#M001677">define_method</a>:<br />
As the API tells :</p>
<blockquote><p>Defines an instance method in the receiver. The method parameter can be a Proc or Method object. If a block is specified, it is used as the method body. This block is evaluated using instance_eval, a point that is tricky to demonstrate because define_method is private.</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># define_method is a private method, that's why we use the 'class_eval' method</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">class_eval</span> <span style="color:#006600; font-weight:bold;">&#123;</span>define_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:len</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span>length<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;">#=&gt; #&lt;Proc:0xb7635e68@(irb):17&gt;</span>
<span style="color:#996600;">&quot;how much long am I?&quot;</span>.<span style="color:#9900CC;">len</span> <span style="color:#008000; font-style:italic;">#=&gt; 19</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># You can use the 'send' method instead of 'class_eval', notice how we are sending a Proc object to define_method</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">send</span> <span style="color:#ff3333; font-weight:bold;">:define_method</span>,:len2,<span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span>length<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#996600;">&quot;how much long am I?&quot;</span>.<span style="color:#9900CC;">len2</span> <span style="color:#008000; font-style:italic;">#=&gt; 19</span></pre></div></div>

<p>When using a block, the block params will be the method params:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">class_eval</span><span style="color:#006600; font-weight:bold;">&#123;</span>define_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:part</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>s,e<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span>s,e<span style="color:#006600; font-weight:bold;">&#93;</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;">#=&gt; #&lt;Proc:0xb7699364@(irb):14&gt;</span>
<span style="color:#996600;">&quot;hello world!&quot;</span>.<span style="color:#9900CC;">part</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;ello&quot;</span></pre></div></div>

<p>Let&#8217;s use it to redefine the &#8216;attr_access&#8217; in another way rather than the way that was defined in the <a href="http://www.khelll.com/blog/ruby/ruby-reflection/">previous post</a></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;">Class</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> attr_access<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>attrs<span style="color:#006600; font-weight:bold;">&#41;</span>
    attrs.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>attr<span style="color:#006600; font-weight:bold;">|</span>
      define_method :<span style="color:#996600;">&quot;#{attr}=&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>value<span style="color:#006600; font-weight:bold;">|</span>
        instance_variable_set<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@#{attr}&quot;</span>,value<span style="color:#006600; font-weight:bold;">&#41;</span> 
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      define_method :<span style="color:#996600;">&quot;#{attr}&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        instance_variable_get <span style="color:#996600;">&quot;@#{attr}&quot;</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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>One limitation to &#8216;define_method&#8217; is that: it always creates instance methods, thus if we want to use it to define class methods, then we need to invoke it on the singleton class, let&#8217;s define the &#8216;cattr_access&#8217; method:</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;">Class</span>
   <span style="color:#9966CC; font-weight:bold;">def</span> cattr_access<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>attrs<span style="color:#006600; font-weight:bold;">&#41;</span>
    singleton_class = <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>; <span style="color:#0000FF; font-weight:bold;">self</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
    attrs.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>attr<span style="color:#006600; font-weight:bold;">|</span>
      singleton_class.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
        define_method :<span style="color:#996600;">&quot;#{attr}=&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>value<span style="color:#006600; font-weight:bold;">|</span>
          class_variable_set<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@@#{attr}&quot;</span>,value<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
        define_method :<span style="color:#996600;">&quot;#{attr}&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          class_variable_get <span style="color:#996600;">&quot;@@#{attr}&quot;</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>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Notice how i defined the singleton_class as a local variable, it could be defined using a method, just like what we did in the previous post, however, notice: how complex it became to use the define_method to define a class method, i wouldn&#8217;t encourage at all such a complexity.</p>
<h3>Undefining methods</h3>
<p>How about undefining methods? That can be accomplished in 2 ways: either by using the &#8216;undef&#8217; statement, or using the private &#8216;undef_method&#8217; method:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">class_eval</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#9966CC; font-weight:bold;">undef</span> <span style="color:#ff3333; font-weight:bold;">:len</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; nil</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">send</span> <span style="color:#ff3333; font-weight:bold;">:undef_method</span> ,:len2 <span style="color:#008000; font-style:italic;">#=&gt; String</span></pre></div></div>

<h3>Alias chaining</h3>
<p>One last thing that deserves mentioning here: is the &#8216;alias_method&#8217;, it&#8217;s being used to have alias chaining:<br />
1- Copy the original method and give it an alias to be used later.<br />
2- Create a new method with the same name of the original one, do whatever changes you need, and use the alias to invoke the original method somewhere inside the new one.<br />
Before i show you the example i have to mention the ruby statement &#8216;alias&#8217; which can be used as &#8216;alias_method&#8217; :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># I want to modify the 'puts' method to make it more expressive, at least for me!</span>
<span style="color:#008000; font-style:italic;">#' puts' is defined in Kernel module.</span>
<span style="color:#008000; font-style:italic;"># Copy the method &quot;puts&quot;, and assign it a new name: &quot;original_puts&quot;</span>
<span style="color:#9966CC; font-weight:bold;">alias</span> <span style="color:#ff3333; font-weight:bold;">:original_puts</span> <span style="color:#ff3333; font-weight:bold;">:puts</span> <span style="color:#008000; font-style:italic;">#=&gt; same as doing:   Kernel.send :alias_method ,:original_puts ,:puts</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Now define the method again, and use the original method functionality to help expressing the new functionality. </span>
<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC0066; font-weight:bold;">puts</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
   args.<span style="color:#9900CC;">each_with_index</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>arg,index<span style="color:#006600; font-weight:bold;">|</span>
	original_puts <span style="color:#996600;">&quot;value of parameter #{index+1} =&gt;  #{arg}&quot;</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>
<span style="color:#008000; font-style:italic;">#value of parameter 1 =&gt;  1</span>
<span style="color:#008000; font-style:italic;">#value of parameter 2 =&gt;  2</span>
<span style="color:#008000; font-style:italic;">#value of parameter 3 =&gt;  3</span></pre></div></div>

<p>I hope you liked this post, please don&#8217;t hesitate to notify me about hidden mistakes or suggest new stuff.<br />
I&#8217;ll blog on using ruby&#8217;s hooks(callbacks) in the next blog post, stay in touch&#8230;..</p>
]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/ruby-reflection-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>
