<?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; reflection</title>
	<atom:link href="http://www.khelll.com/blog/tag/reflection/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 reflection</title>
		<link>http://www.khelll.com/blog/ruby/ruby-reflection/</link>
		<comments>http://www.khelll.com/blog/ruby/ruby-reflection/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 22:23:28 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=118</guid>
		<description><![CDATA[If you are here, then most probably you want to know more about ruby reflection interface. Well that&#8217;s true, but I always find myself in need to explain few things before I get started with my posts, and this time I find myself in need of explaining few things related to ruby&#8217;s OOP.
I&#8217;m pretty sure [...]]]></description>
			<content:encoded><![CDATA[<p>If you are here, then most probably you want to know more about ruby reflection interface. Well that&#8217;s true, but I always find myself in need to explain few things before I get started with my posts, and this time I find myself in need of explaining few things related to ruby&#8217;s OOP.</p>
<p>I&#8217;m pretty sure that you always heard that &#8220;in ruby: everything is an object.&#8221; , but have you ever thought of that carefully?<br />
The first thing that comes to a one&#8217;s mind is something like :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">5.<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#008000; font-style:italic;">#=&gt; Fixnum</span>
<span style="color:#996600;">&quot;hello&quot;</span>.<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#008000; font-style:italic;">#=&gt; String</span></pre></div></div>

<p>But have you thought of your class as an object? Well that seems odd, but that&#8217;s how ruby works:</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;end  <span style="color:#008000; font-style:italic;">#=&gt; nil</span>
Foo.<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#008000; font-style:italic;">#=&gt; Class</span></pre></div></div>

<p>What does the above snippet of code mean exactly?<br />
It means 2 things : Foo is a constant and that constant holds(refers to) an object of Class type. </p>
<p>Let me prove that 2 you:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Foo = <span style="color:#9966CC; font-weight:bold;">Class</span>.<span style="color:#9900CC;">new</span>
<span style="color:#006600; font-weight:bold;">&#40;</span>irb<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">8</span> warning: already initialized constant Foo
<span style="color:#006600; font-weight:bold;">=&gt;</span> Foo</pre></div></div>

<p>As you can see, I got a warning because I tried to initialize the constant Foo again.</p>
<p>So ,when you define some class &#8216;Foo&#8217; in ruby, all you are doing is:<br />
1-instantiating an object of type Class.<br />
2-initializing a constant Foo that refers to that created object .</p>
<p>So bear in mind that when I say &#8220;object&#8221; ,then I <strong>do</strong> mean <strong>any object</strong>; an object of  Class type, or any object of any type.</p>
<p>Now, let&#8217;s move to another point. What about the &#8216;Singleton Class&#8217;, did you have the chance to work with it before?<br />
Simply it&#8217;s the class that holds all singleton methods of an object, whether it&#8217;s a class object , or any other object. </p>
<p>Let&#8217;s start by defining 2 class methods (a <strong>class method</strong> is nothing but: a singleton method of an object of Class type) :</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
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">hi</span> ; <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;hi&quot;</span> ;end
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">bye</span> ; <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;bye&quot;</span> ; <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
Foo.<span style="color:#9900CC;">singleton_methods</span> <span style="color:#008000; font-style:italic;">#=&gt; [&quot;hi&quot;,&quot;bye&quot;]</span></pre></div></div>

<p>You could also have written that in this way:</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
  <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:#9966CC; font-weight:bold;">def</span> hi ; <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;hi&quot;</span> ; <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> bye ; <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;bye&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>
Foo.<span style="color:#9900CC;">singleton_methods</span> <span style="color:#008000; font-style:italic;">#=&gt; [&quot;hi&quot;,&quot;bye&quot;]</span></pre></div></div>

<p>The above inner class mentioned with &#8220;class << self" is what we call : a singleton class.<br />
Let's define a method that returns back the singleton class for us :</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:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#9966CC; font-weight:bold;">def</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>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now try this :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Foo.<span style="color:#9900CC;">singleton_methods</span> <span style="color:#008000; font-style:italic;">#=&gt; [&quot;bye&quot;, &quot;hi&quot;]</span>
Foo.<span style="color:#9900CC;">singleton_class</span>.<span style="color:#9900CC;">instance_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;bye&quot;, &quot;hi&quot;]</span></pre></div></div>

<p>As you can see, the singleton class is the class that holds all singleton methods.<br />
We will use the singleton class later in this series, so keep the concept in mind.</p>
<p>Let&#8217;s now get back to our topic on ruby&#8217;s reflection api , i will introduce 3 methods in this post: <strong>eval</strong> , <strong>instance_eval</strong> and <strong>class_eval</strong>.</p>
<h3>eval</h3>
<p>&#8216;eval&#8217; is a method that evaluates a ruby string :</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;3+4&quot;</span> <span style="color:#008000; font-style:italic;">#=&gt; 7</span>
<span style="color:#CC0066; font-weight:bold;">eval</span> <span style="color:#996600;">&quot;def multiply(x,y) ; x*y; end&quot;</span>
multiply<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">4</span>,<span style="color:#006666;">7</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 28</span></pre></div></div>

<p>eval can also work in the scope of <a href="http://www.ruby-doc.org/core/classes/Binding.html">bindings</a> ; a binding is an object that encapsulates the execution context at some particular place in the code and retain this context for future use. Look at this example of using bindings with eval :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Demo
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@secret</span> = n
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> getBinding
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#CC0066; font-weight:bold;">binding</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#008000; font-style:italic;"># a method defined in Kernel module</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
k1 = Demo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">99</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#get the value of the instance variable @secrete stored in the binding of object k1</span>
<span style="color:#CC0066; font-weight:bold;">eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@secret&quot;</span>, k1.<span style="color:#9900CC;">getBinding</span><span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#008000; font-style:italic;">#=&gt; 99</span></pre></div></div>

<p>Also can work with proc objects :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> greeting<span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">lambda</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>greetings<span style="color:#006600; font-weight:bold;">|</span> greetings.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>g<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;#{g} #{name}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
greeter = greeting<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;dude&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0xb752b810@(irb):2&gt;</span>
greeter.<span style="color:#9900CC;">call</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;hi&quot;</span>,<span style="color:#996600;">&quot;hello&quot;</span>,<span style="color:#996600;">&quot;hola&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#008000; font-style:italic;">#=&gt; [&quot;hi dude&quot;, &quot;hello dude&quot;, &quot;hola dude&quot;]</span>
<span style="color:#CC0066; font-weight:bold;">eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;name='khelll'&quot;</span>,greeter<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;khelll&quot;</span>
greeter.<span style="color:#9900CC;">call</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;hi&quot;</span>,<span style="color:#996600;">&quot;hello&quot;</span>,<span style="color:#996600;">&quot;hola&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#008000; font-style:italic;">#=&gt; [&quot;hi khelll&quot;, &quot;hello khelll&quot;, &quot;hola khelll&quot;]</span></pre></div></div>

<h3>instance_eval</h3>
<p>This method works in the context of the 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> Klass
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@secret</span> = <span style="color:#006666;">99</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
k = Klass.<span style="color:#9900CC;">new</span>
k.<span style="color:#9900CC;">instance_eval</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#0066ff; font-weight:bold;">@secret</span> <span style="color:#006600; font-weight:bold;">&#125;</span>   <span style="color:#008000; font-style:italic;">#=&gt; 99 , notice the @</span></pre></div></div>

<p>And could be used to define singleton methods :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">Fixnum</span>.<span style="color:#9900CC;">instance_eval</span> <span style="color:#996600;">&quot;def zero; 0 ;end&quot;</span>                                                 
<span style="color:#CC00FF; font-weight:bold;">Fixnum</span>.<span style="color:#9900CC;">zero</span> <span style="color:#008000; font-style:italic;">#=&gt; 0</span></pre></div></div>

<p>you can pass it a block instead of the string :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">Fixnum</span>.<span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#9966CC; font-weight:bold;">def</span> ten ;<span style="color:#006666;">10</span>;end <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC00FF; font-weight:bold;">Fixnum</span>.<span style="color:#9900CC;">ten</span> <span style="color:#008000; font-style:italic;">#=&gt; 10</span></pre></div></div>

<h3>class_eval</h3>
<p>Evaluates a string or a block in the context of the receiver</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Foo.<span style="color:#9900CC;">class_eval</span><span style="color:#006600; font-weight:bold;">&#123;</span>@@x=<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>
Foo.<span style="color:#9900CC;">class_eval</span><span style="color:#006600; font-weight:bold;">&#123;</span>@@x<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; 1</span></pre></div></div>

<p>And it defines instance methods when called on some object</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">Fixnum</span>.<span style="color:#9900CC;">class_eval</span> <span style="color:#996600;">&quot;def number ; self ;end&quot;</span>
5.<span style="color:#9900CC;">number</span> <span style="color:#008000; font-style:italic;">#=&gt; 5</span></pre></div></div>

<p>And as instance_eval, a block instead of the string could be passed</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">Fixnum</span>.<span style="color:#9900CC;">class_eval</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#9966CC; font-weight:bold;">def</span> number;self;end<span style="color:#006600; font-weight:bold;">&#125;</span>
7.<span style="color:#9900CC;">number</span> <span style="color:#008000; font-style:italic;">#=&gt; 7</span></pre></div></div>

<p>You can use class_eval to dynamically use private methods, for example to use the private method &#8216;include&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> M; <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9966CC; font-weight:bold;">include</span> M <span style="color:#008000; font-style:italic;">#=&gt; NoMethodError: private method `include' called for String:Class</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><span style="color:#9966CC; font-weight:bold;">include</span> M<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; you could do it with String.send(:include,M)</span></pre></div></div>

<p>Now let&#8217;s make use of our knowledge,let&#8217;s try to redefine the <a href="http://www.ruby-doc.org/core/classes/Module.html#M001701">attr_accessor</a> method in our way, I will make a similar method called attr_access :</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>
      class_eval <span style="color:#006600; font-weight:bold;">%</span>Q<span style="color:#006600; font-weight:bold;">&#123;</span>
	    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{attr} </span>
	      @<span style="color:#008000; font-style:italic;">#{attr}</span>
	    <span style="color:#9966CC; font-weight:bold;">end</span>
	    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{attr}=(value)</span>
	      @<span style="color:#008000; font-style:italic;">#{attr} = value</span>
	    <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#006600; font-weight:bold;">&#125;</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>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Foo 
 attr_access <span style="color:#ff3333; font-weight:bold;">:a</span>,:b 
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Foo.<span style="color:#9900CC;">instance_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;b=&quot;, &quot;a=&quot;, &quot;b&quot;, &quot;a&quot;]</span></pre></div></div>

<p>in a similar way we can define class attribute accessors :</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>
      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>
        class_eval <span style="color:#006600; font-weight:bold;">%</span>Q<span style="color:#006600; font-weight:bold;">&#123;</span>
  	    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#008000; font-style:italic;">#{attr}</span>
  	      @@<span style="color:#008000; font-style:italic;">#{attr}</span>
  	    <span style="color:#9966CC; font-weight:bold;">end</span>
  	    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#008000; font-style:italic;">#{attr}=(value)</span>
  	      @@<span style="color:#008000; font-style:italic;">#{attr} = value</span>
  	    <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#006600; font-weight:bold;">&#125;</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>Or with we can use the singleton class :</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> 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> 
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <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>
    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:#006600; font-weight:bold;">%</span>Q<span style="color:#006600; font-weight:bold;">&#123;</span>
        <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{attr}</span>
  	   @@<span style="color:#008000; font-style:italic;">#{attr}</span>
  	 <span style="color:#9966CC; font-weight:bold;">end</span>
  	 <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{attr}=(value)</span>
  	    @@<span style="color:#008000; font-style:italic;">#{attr} = value</span>
  	 <span style="color:#9966CC; font-weight:bold;">end</span>
       <span style="color:#006600; font-weight:bold;">&#125;</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>And in both cases we can do :</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 ; cattr_access <span style="color:#ff3333; font-weight:bold;">:cx</span>,:cy <span style="color:#9966CC; font-weight:bold;">end</span>
Foo.<span style="color:#9900CC;">singleton_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;cy&quot;, &quot;cy=&quot;, &quot;cx&quot;, &quot;cx=&quot;]</span></pre></div></div>

<p>Give it a try and try to define attr_reader and attr_writer for both object and class variables.</p>
<p>I think it&#8217;s enough for this post, the next post will contain more methods to look at. See you then.</p>
<p><strong>Update 1</strong>: fixing some typos.<br />
<strong>Update 2</strong>: second part is <a href="http://www.khelll.com/blog/ruby/ruby-reflection-2/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/ruby-reflection/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
