<?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>RubyLearning Blog &#187; Elise Huard</title>
	<atom:link href="http://rubylearning.com/blog/author/elisehuard/feed/" rel="self" type="application/rss+xml" />
	<link>http://rubylearning.com/blog</link>
	<description>Helping Ruby Programmers become Awesome</description>
	<lastBuildDate>Tue, 24 Apr 2012 22:15:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Ruby Forensics</title>
		<link>http://rubylearning.com/blog/2010/10/04/ruby-forensics/</link>
		<comments>http://rubylearning.com/blog/2010/10/04/ruby-forensics/#comments</comments>
		<pubDate>Sun, 03 Oct 2010 23:30:16 +0000</pubDate>
		<dc:creator>Elise Huard</dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby Masters]]></category>
		<category><![CDATA[Elise Huard]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Ruby Beginners]]></category>
		<category><![CDATA[Ruby for Noobs]]></category>
		<category><![CDATA[Ruby Forensics]]></category>

		<guid isPermaLink="false">http://rubylearning.com/blog/?p=4910</guid>
		<description><![CDATA[Ruby Forensics This guest post is contributed by Elise Huard, who is based in Brussels, Belgium and is the owner of Jabberwocky, a solutions company mostly focused on Rails. She has worked with a few other technologies before falling in love with Rails and Ruby about 3 years ago and going freelance to work with [...]<p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>
]]></description>
			<content:encoded><![CDATA[<p></p>
<div class="topsy_widget_data topsy_theme_brick-red" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frubylearning.com%252Fblog%252F2010%252F10%252F04%252Fruby-forensics%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2Fb9Ohlv%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%20Forensics%22%20%7D);"></div>
<div>
<h3>Ruby Forensics</h3>
<p class="update">This guest post is contributed by <strong><a href="http://twitter.com/elise_huard">Elise Huard</a></strong>, who is based in Brussels, Belgium and is the owner of <a href="http://jabberwocky.eu/">Jabberwocky</a>, a solutions company mostly focused on Rails. She has worked with a few other technologies before falling in love with Rails and Ruby about 3 years ago and going freelance to work with Ruby full time. She contributes to open source projects as much as she can, and has given talks at a few Ruby and Rails conferences. She’s a jack of all trades, loves reading, tinkering, food, travel, learning, and people out of the ordinary.</p>
<p class="block"><img style=' float: right; padding: 4px; margin: 0 0 2px 7px;'  class="alignright" src="http://rubylearning.com/images/pic1-125.jpg" alt="Elise Huard" width="125" height="125" /> <span class="drop_cap">S</span>ay you want to use a library, but no or very little documentation is available, and you don&#8217;t feel like diving into the code right away.</p>
<p>Well, you picked the right language. Ruby is blessed with what is called <a href="http://en.wikipedia.org/wiki/Type_introspection">introspection</a>: if you ask a Ruby program/class/module politely, it will tell you almost anything about itself. This post will tell you some tricks I use on a daily basis.</p>
<pre>module Layer
  FILLINGS = [:chocolate, :meringue, :jam, :cream, :strawberry]
  def fill(filling)
    puts "fill with #{filling}"
  end
end

class Cake
  include Layer

  attr_accessor :calories

  def ice
    @calories = @calories + 200
  end

  def eat
    puts "nom"
  end

  def self.bake
    return new
  end
end

class CheeseCake &lt; Cake; end</pre>
<p>Say you have a class, but you&#8217;d like to know what method it defines.</p>
<pre>Cake.public_instance_methods</pre>
<p>Won&#8217;t be that useful, because Ruby objects (with some exceptions like <code>BasicObject</code>) have got a whole lot of methods out of the box. Rather, use:</p>
<pre>Cake.public_instance_methods - Object.public_instance_methods
=&gt; [:calories, :calories=, :ice, :eat, :fill]</pre>
<p>If you want one particular method, and you have an idea of which name it should have:</p>
<pre>(Cake.public_instance_methods - Object.public_instance_methods).grep(/eat/)</pre>
<p>Will show if your instinct was right or not.</p>
<p>The same can be done for class methods, of course:</p>
<pre>(Cake.methods - Object.methods)
=&gt; [:bake]</pre>
<p>(<code>public_class_methods</code> also works.)</p>
<p><em>Note:</em> this won&#8217;t show you the dynamic methods, like find_by_X for ActiveRecord. The class doesn&#8217;t know it has these kind of methods in itself. They&#8217;re executed on the fly when the program hits <code>method_missing</code>. You&#8217;d have to look at the classes&#8217; <code>method_missing</code> method to find out.</p>
<p>Then there&#8217;s the case where you&#8217;d like to know, exactly, where a method was defined. Ruby gives us the &#8216;<code>method</code>&#8216; method, which takes a symbol as an argument.</p>
<pre>Cake.new.method(:fill)
 =&gt; #&lt;Method: Cake(Layer)#fill&gt;</pre>
<p>Which tells us it was defined in the Layer module, included in the Cake class.</p>
<pre>Cake.new.method(:eat)
 =&gt; #&lt;Method: Cake#eat&gt;</pre>
<p>Sometimes, you want to know which other classes your class was descended from &#8211; including mixed in modules. The <code>ancestors</code> method will show you:</p>
<pre>CheeseCake.ancestors
 =&gt; [CheeseCake, Cake, Layer, Object, Kernel, BasicObject]</pre>
<p>Should you want to know about any constants, there&#8217;s a method for that too:</p>
<pre>Cake.constants
 =&gt; [:FILLINGS]</pre>
<p>All these methods have a good chance of giving you the information you want. When used in irb, together with a little experimentation, they can really help you find the code you&#8217;re looking for.</p>
<p><em>I hope you found this article valuable. Feel free to ask questions and give feedback in the comments section of this post. Thanks!</em></p>
<p><b><em>Do read</em> these awesome Guest Posts:</b></p>
<ul>
<li><a href="http://rubylearning.com/blog/2010/10/01/an-introduction-to-eventmachine-and-how-to-avoid-callback-spaghetti/">An introduction to eventmachine, and how to avoid callback spaghetti</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/30/the-testing-mindset/">The Testing Mindset</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/29/an-introduction-to-desktop-apps-with-ruby/">An Introduction to Desktop Apps with Ruby</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/28/the-ruby-movement/">The Ruby movement</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/27/almost-everything-is-an-object-and-everything-is-almost-an-object/">Almost everything is an object (and everything is almost an object!)</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/24/so-youre-new-to-ruby/">So… you’re new to Ruby!</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/23/incorporating-web-apis-to-spark-computer-programming-exercises/">Incorporating Web APIs to spark computer programming exercises</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/22/14-ways-to-have-fun-coding-ruby/">14 Ways To Have Fun Coding Ruby</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/21/writing-modular-web-applications-with-rack/">Writing modular web applications with Rack</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/20/how-to-learn-ruby-or-any-programming-language/">How to Learn Ruby (or any programming language)</a></li>
</ul>
<p><a href="http://www.thechicagoschoolonline.net/master-forensic-psychology.asp">Forensic Psychology Degree</a></p>
</div>
<p>Technorati Tags: <a href="http://technorati.com/tag/Ruby" rel="tag">Ruby</a>, <a href="http://technorati.com/tag/Programming" rel="tag"> Programming</a>, <a href="http://technorati.com/tag/Elise+Huard" rel="tag"> Elise Huard</a>, <a href="http://technorati.com/tag/Ruby+for+Noobs" rel="tag"> Ruby for Noobs</a>, <a href="http://technorati.com/tag/Ruby+beginners" rel="tag"> Ruby beginners</a>, <a href="http://technorati.com/tag/Ruby+Forensics" rel="tag"> Ruby Forensics</a></p>
Posted by <b>Elise Huard</b><p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>

]]></content:encoded>
			<wfw:commentRss>http://rubylearning.com/blog/2010/10/04/ruby-forensics/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
	</channel>
</rss>

