<?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>Bicosyes - since evermore... &#187; method_added</title>
	<atom:link href="http://bicosyes.com/tag/method_added/feed/" rel="self" type="application/rss+xml" />
	<link>http://bicosyes.com</link>
	<description></description>
	<lastBuildDate>Sat, 06 Mar 2010 20:03:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to know, in ruby, which methods have been added and by whom?</title>
		<link>http://bicosyes.com/how-to-know-in-ruby-which-methods-have-been-added-and-by-whom/</link>
		<comments>http://bicosyes.com/how-to-know-in-ruby-which-methods-have-been-added-and-by-whom/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 19:50:37 +0000</pubDate>
		<dc:creator>blaxter</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[method_added]]></category>
		<category><![CDATA[monkeypatching]]></category>

		<guid isPermaLink="false">http://bicosyes.com/?p=885</guid>
		<description><![CDATA[If you are not very careful, monkeypatching could be very harmful. One thing to remember is that you should never override a method to add funcionality, for those kind of thinks you must use alias chain method pattern, a safer way of doing that. 
For the rest of the monkeypatching, i.e. add new methods, you [...]]]></description>
			<content:encoded><![CDATA[<p>If you are not very careful, monkeypatching could be very harmful. One thing to remember is that you should <strong>never</strong> override a method to <strong>add</strong> funcionality, for those kind of thinks you must use <em>alias chain method pattern</em>, a safer way of doing that. </p>
<p>For the rest of the <em>monkeypatching</em>, i.e. add new methods, you could debug them really easy with something like this:</p>
<pre class="ruby"><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> method_added<span style="color:#006600; font-weight:bold;">&#40;</span>method_name<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{method_name} added to #{self}, callstack:&quot;</span>
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC0066; font-weight:bold;">caller</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span>|line| <span style="color:#996600;">&quot;<span style="color:#000099;">\t</span>#{line}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre>
<p>You can always add more code to filter by class or by method's name. Let's see an example:</p>
<pre class="ruby">$ more example.<span style="color:#9900CC;">rb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'date'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'time'</span>
&nbsp;
<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> method_added<span style="color:#006600; font-weight:bold;">&#40;</span>method_name<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">if</span> %w<span style="color:#006600; font-weight:bold;">&#40;</span>method_added<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>? method_name.<span style="color:#9900CC;">to_s</span>
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{method_name} added to #{self}, callstack:&quot;</span>
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC0066; font-weight:bold;">caller</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span>|line| <span style="color:#996600;">&quot;<span style="color:#000099;">\t</span>#{line}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</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> <span style="color:#CC00FF; font-weight:bold;">Time</span>
   <span style="color:#9966CC; font-weight:bold;">def</span> to_date
      <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">ordinal</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">year</span>, <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">yday</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> <span style="color:#CC00FF; font-weight:bold;">Date</span>
   <span style="color:#9966CC; font-weight:bold;">def</span> to_time
      <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">parse</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">to_s</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;">raise</span> <span style="color:#996600;">&quot;to_date not working&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span>
   <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>.<span style="color:#9900CC;">to_date</span> == <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span>
<span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;to time not working&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span>
   <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>.<span style="color:#9900CC;">to_date</span>.<span style="color:#9900CC;">to_time</span> == <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span>.<span style="color:#9900CC;">to_time</span></pre>
<p>The output will be:</p>
<pre>$ ruby example.rb
to_date added to Time, callstack:
	example.rb:13
to_time added to Date, callstack:
	example.rb:19</pre>
<p>Nice, isn't it?. Remember to be carefull with your monkeypatching, with great power comes great responsibility, it's just a tool, neither magic nor the panacea.</p>
<img src="http://bicosyes.com/?ak_action=api_record_view&id=885&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://bicosyes.com/how-to-know-in-ruby-which-methods-have-been-added-and-by-whom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

