<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Khelll&#039;s Blog &#187; introspection</title>
	<atom:link href="http://khelll.com/blog/tag/introspection/feed/" rel="self" type="application/rss+xml" />
	<link>http://khelll.com/blog</link>
	<description>Cool Web Development...</description>
	<lastBuildDate>Thu, 13 Oct 2011 03:48:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Ruby introspection 2</title>
		<link>http://khelll.com/blog/ruby/ruby-introspection-2/</link>
		<comments>http://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[metaprogramming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[introspection]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=59</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/ruby-introspection-2/' addthis:title='Ruby introspection 2 '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>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 &#8230; <a href="http://khelll.com/blog/ruby/ruby-introspection-2/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/ruby-introspection-2/' addthis:title='Ruby introspection 2' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/ruby-introspection-2/' addthis:title='Ruby introspection 2 '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><p>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:#9966CC; 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:#9966CC; 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://khelll.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/ruby-introspection-2/' addthis:title='Ruby introspection 2' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://khelll.com/blog/ruby/ruby-introspection-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby introspection</title>
		<link>http://khelll.com/blog/ruby/ruby-introspection/</link>
		<comments>http://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[metaprogramming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[introspection]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=3</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/ruby-introspection/' addthis:title='Ruby introspection '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>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 &#8230; <a href="http://khelll.com/blog/ruby/ruby-introspection/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/ruby-introspection/' addthis:title='Ruby introspection' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://khelll.com/blog/ruby/ruby-introspection/' addthis:title='Ruby introspection '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><p>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://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://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>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://khelll.com/blog/ruby/ruby-introspection/' addthis:title='Ruby introspection' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_print"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://khelll.com/blog/ruby/ruby-introspection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

