<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8324598090381948093</id><updated>2012-02-16T22:04:13.843+11:00</updated><category term='ruby'/><category term='IE7'/><category term='responds_to'/><category term='mocks'/><category term='block'/><category term='XSLT'/><category term='duck_typing'/><category term='IE6'/><category term='svn:ignore'/><category term='programming'/><category term='outside in development'/><category term='XML'/><category term='art'/><category term='lambda'/><category term='method_missing'/><category term='class_eval'/><category term='C#'/><category term='creative'/><category term='rspec'/><category term='css'/><category term='render'/><category term='hacks'/><category term='mocking'/><category term='enterprise'/><category term='rails'/><category term='functional'/><category term='Objective-C'/><category term='propset'/><category term='Internet Explorer'/><category term='bdd'/><category term='stubs'/><category term='svn'/><title type='text'>Coding Rants</title><subtitle type='html'>A place to tirade about programming</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://codingrants.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://codingrants.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Cameron Barrie</name><uri>http://www.blogger.com/profile/07717661204895096395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8324598090381948093.post-3863849788692627500</id><published>2009-03-10T11:58:00.000+11:00</published><updated>2009-03-10T12:02:41.052+11:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='method_missing'/><category scheme='http://www.blogger.com/atom/ns#' term='class_eval'/><category scheme='http://www.blogger.com/atom/ns#' term='Objective-C'/><category scheme='http://www.blogger.com/atom/ns#' term='duck_typing'/><category scheme='http://www.blogger.com/atom/ns#' term='responds_to'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Duck Typing</title><content type='html'>&lt;p style="clear: both"&gt;I learnt how to program in Ruby, and as we all know it's a very flexible language. Recently I've been doing a lot of stuff in C# on windows, and a lot more stuff in Objective-C on the iPhone. Something finally clicked for me, when I was using these languages, and that was Duck Typing. &lt;/p&gt;&lt;p style="clear: both"&gt;Ruby, unlike the C style languages, couldn't care less about what type of object it is dealing with. Now before you start, I will repeat, it couldn't care less about what type of object it is dealing with. &lt;/p&gt;&lt;p style="clear: both"&gt;What Ruby cares about is if object responds to the method you're sending it. In Ruby there is even a respond_to? method to assert this. i.e. &lt;/p&gt;&lt;blockquote style="clear: both"&gt;&lt;p&gt;1.respond_to?(:to_s) #=&amp;gt; true&lt;br /&gt;1.respond_to?(:foo_bar) #=&amp;gt; false&lt;/p&gt;&lt;/blockquote&gt;&lt;p style="clear: both"&gt;So you can have two objects, of totally differing types, and instead of asserting type like you might in say C#, you can instead check that the object responds to a method. The great thing about this, is that if it doesn't and you want it to, you can use Ruby's meta programming to dynamically inject that method into that object. For instance&lt;/p&gt;&lt;blockquote style="clear: both"&gt;&lt;p&gt;&lt;pre style="clear: both"&gt;unless 1.respond_to?(:foo_bar)&amp;lt;br /&amp;gt;   1.class.class_eval do&amp;lt;br /&amp;gt;      def foo_bar&amp;lt;br /&amp;gt;         self * 100&amp;lt;br /&amp;gt;      end&amp;lt;br /&amp;gt;   end&amp;lt;br /&amp;gt;end&lt;/pre&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;blockquote style="clear: both"&gt;&lt;p&gt;1.foo_bar #=&amp;gt; 100&lt;br /&gt;2.foo_bar #=&amp;gt; 200&lt;br /&gt;30.foo_bar #=&amp;gt; 30000&lt;br /&gt;&lt;br /&gt;12.2.foo_bar&lt;br /&gt;#=&amp;gt; NoMethodError: undefined method `foo_bar' for 12.2:Float&lt;br /&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p style="clear: both"&gt;So we've used class_eval to dynamically inject the method foo_bar in the class of 1(Fixnum), and so now all instances of Fixnum will respond to that method. We've never even checked it's type, we don't care, all we know is that we want it to respond to the method foo_bar and now it does. Obviously our Float class doesn't have this method hence the reason that 12.2 has raised the undefined method error. Any guesses on how to make this work :D, yeah that's right, class_eval and respond_to? will do it, just like we did with Fixnum.&lt;/p&gt;&lt;p style="clear: both"&gt;So in summary what is the point of Duck Typing? Why not just check the class, and respond with what we as coders know it can and cannot do(or think we know it can do)? &lt;br /&gt;&lt;br /&gt;Well, seriously why do you care what class it is? When what you really wanted to know is, whether it's going to respond to a method? Sometimes class and type is important, and obviously those features are there and should be used when class is important, but sometimes you just need to stop and ask yourself.&lt;/p&gt;&lt;blockquote style="clear: both"&gt;&lt;p&gt;Do I can about type or do I just want to know if I can execute a method on this object?&lt;/p&gt;&lt;/blockquote&gt;&lt;br class='final-break' style='clear: both' /&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;a 
style="padding: 0pt 3px; cursor: pointer;" onclick="javascript:location.href='http://reddit.com/submit?url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title)"&gt;
&lt;img alt="reddit" src="http://reddit.com/static/reddit_firefox.png"/&gt;
&lt;/a&gt;
&lt;script src="http://del.icio.us/feeds/js/networkbadge/cammo1979?name;icon" type="text/javascript"&gt;&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8324598090381948093-3863849788692627500?l=codingrants.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codingrants.blogspot.com/feeds/3863849788692627500/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8324598090381948093&amp;postID=3863849788692627500' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/3863849788692627500'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/3863849788692627500'/><link rel='alternate' type='text/html' href='http://codingrants.blogspot.com/2009/03/duck-typing.html' title='Duck Typing'/><author><name>Cameron Barrie</name><uri>http://www.blogger.com/profile/07717661204895096395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8324598090381948093.post-8763776155828688700</id><published>2008-06-17T13:14:00.002+10:00</published><updated>2008-06-17T13:30:41.970+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='lambda'/><category scheme='http://www.blogger.com/atom/ns#' term='functional'/><category scheme='http://www.blogger.com/atom/ns#' term='block'/><title type='text'>Functional Ruby methods</title><content type='html'>Ruby is a great language and normally I'd roll it in a object oriented fashion, however I've just started looking at Haskell, and thought that lambda could give me something similar to Haskell functions in Ruby. albeit without a ton of Haskell's features.&lt;br /&gt;&lt;br /&gt;So here are two examples, the first is normal OOP Ruby&lt;br /&gt;&lt;p style="color: black"&gt;&lt;br /&gt;b = Time.now&lt;br /&gt;class Integer&lt;br /&gt;&amp;nbsp;&amp;nbsp;def main&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;self * self&lt;br /&gt;&amp;nbsp;&amp;nbsp;end  &lt;br /&gt;end&lt;br /&gt;1000000.times{ |i| i.main }&lt;br /&gt;puts "Time: #{Time.now - b}"&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;On my machine this returns "Time: 1.115471"&lt;br /&gt;&lt;br /&gt;Here is a functional style version. &lt;br /&gt;&lt;p style="color: black"&gt;&lt;br /&gt;b = Time.now&lt;br /&gt;main = lambda { |x| x * x }&lt;br /&gt;1000000.times &amp;main&lt;br /&gt;puts "Time: #{Time.now - b}"&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;First thing to note is that it's only 4 lines of code, not 8. Second thing is that it returns the following to the terminal:&lt;br /&gt;"Time: 0.960359"&lt;br /&gt;&lt;br /&gt;That's 0.155112 faster, just over 15% faster in computing a simple algorithm like this. Of course this is where functional programming is meant to shine, this is simply an experiment to see if I could do this is Ruby, it is hardly a well tested benchmark, and the results are not all together unexpected, but it's cool in any case.&lt;div class="blogger-post-footer"&gt;&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;a 
style="padding: 0pt 3px; cursor: pointer;" onclick="javascript:location.href='http://reddit.com/submit?url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title)"&gt;
&lt;img alt="reddit" src="http://reddit.com/static/reddit_firefox.png"/&gt;
&lt;/a&gt;
&lt;script src="http://del.icio.us/feeds/js/networkbadge/cammo1979?name;icon" type="text/javascript"&gt;&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8324598090381948093-8763776155828688700?l=codingrants.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codingrants.blogspot.com/feeds/8763776155828688700/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8324598090381948093&amp;postID=8763776155828688700' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/8763776155828688700'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/8763776155828688700'/><link rel='alternate' type='text/html' href='http://codingrants.blogspot.com/2008/06/functional-ruby-methods.html' title='Functional Ruby methods'/><author><name>Cameron Barrie</name><uri>http://www.blogger.com/profile/07717661204895096395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8324598090381948093.post-8558917447850549747</id><published>2008-06-05T14:33:00.002+10:00</published><updated>2008-06-05T14:37:05.312+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Internet Explorer'/><category scheme='http://www.blogger.com/atom/ns#' term='hacks'/><category scheme='http://www.blogger.com/atom/ns#' term='IE7'/><category scheme='http://www.blogger.com/atom/ns#' term='IE6'/><category scheme='http://www.blogger.com/atom/ns#' term='css'/><title type='text'>Targetting IE7 with CSS</title><content type='html'>I've needed to target IE6 &amp; IE7 within my css for 1 margin rule, so rather than some convoluted process I used the following simple hack&lt;br /&gt;&lt;br /&gt;div#div_id{&lt;br /&gt;  margin: 10px 10px 5px 10px;&lt;br /&gt;  *margin: 10px 10px 10px 10px; /* Targets IE6 &amp; IE7 */&lt;br /&gt;  _margin: 5px 5px 5px 5px; /* Targets IE6 */&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Yes it is invalid CSS, but I don't really care, it's 1 - 2 lines in a 200 line stylesheet.&lt;div class="blogger-post-footer"&gt;&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;a 
style="padding: 0pt 3px; cursor: pointer;" onclick="javascript:location.href='http://reddit.com/submit?url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title)"&gt;
&lt;img alt="reddit" src="http://reddit.com/static/reddit_firefox.png"/&gt;
&lt;/a&gt;
&lt;script src="http://del.icio.us/feeds/js/networkbadge/cammo1979?name;icon" type="text/javascript"&gt;&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8324598090381948093-8558917447850549747?l=codingrants.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codingrants.blogspot.com/feeds/8558917447850549747/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8324598090381948093&amp;postID=8558917447850549747' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/8558917447850549747'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/8558917447850549747'/><link rel='alternate' type='text/html' href='http://codingrants.blogspot.com/2008/06/targetting-ie7-with-css.html' title='Targetting IE7 with CSS'/><author><name>Cameron Barrie</name><uri>http://www.blogger.com/profile/07717661204895096395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8324598090381948093.post-2843583754480543576</id><published>2008-05-01T11:15:00.003+10:00</published><updated>2008-05-01T11:30:51.823+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='svn:ignore'/><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><category scheme='http://www.blogger.com/atom/ns#' term='svn'/><category scheme='http://www.blogger.com/atom/ns#' term='propset'/><title type='text'>Ignoring files with SVN</title><content type='html'>This is one of those things that I've just never really got my head around. I'm not sure why, I suppose I just never took the time to read the manual. Stupid me!&lt;br /&gt;&lt;br /&gt;After filling up my drives on my svn host, I found that it was pretty much log files that were killing me, so now I needed to work out how to do it. You'd think it would be simple, something like svn ignore log/development.log, but noooo. &lt;br /&gt;&lt;br /&gt;I won't rant to much here about what svn propset and svn propedit do, you can read the manual page &lt;a href="http://svnbook.red-bean.com/en/1.1/ch07s02.html"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Short of it all is follow these instructions.&lt;/h3&gt;&lt;br /&gt;&lt;b&gt;1.&lt;/b&gt; Create a file that will list all the files you want to ignore, I've called mine .svn_ignore and it is in the root of the trunk of my repository&lt;br /&gt;&lt;br /&gt;&lt;b&gt;2.&lt;/b&gt; Edit that file and add a list of the files you want to ignore, mine looks like this(it's for a rails project)&lt;br /&gt;*.sqlite3&lt;br /&gt;schema.rb&lt;br /&gt;*.log&lt;br /&gt;*.pid&lt;br /&gt;&lt;br /&gt;The file is just a newline separated list of the files you want to ignore, as you can see you can use wild cards.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;3.&lt;/b&gt; Now for the fun bit, first make sure that the files you want to ignore aren't checked in yet. &lt;br /&gt;&lt;b&gt;$&gt; svn st&lt;/b&gt;&lt;br /&gt;? log/production.log&lt;br /&gt;? log/test.log&lt;br /&gt;? log/development.log&lt;br /&gt;? db/schema.rb&lt;br /&gt;? db/development.sqlite3&lt;br /&gt;? db/test.sqlite3&lt;br /&gt;&lt;br /&gt;&lt;b&gt;4.&lt;/b&gt; Execute the following command to ignore all of the listed(in your .svn_ignore) files.&lt;br /&gt;&lt;b&gt;$&gt; svn propset svn:ignore -F .svn_ignore ./log/&lt;/b&gt;&lt;br /&gt;property 'svn:ignore' set on 'log'&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$&gt; svn st&lt;/b&gt;&lt;br /&gt;? db/schema.rb&lt;br /&gt;? db/development.sqlite3&lt;br /&gt;? db/test.sqlite3&lt;br /&gt;&lt;br /&gt;&lt;b&gt;5.&lt;/b&gt; Obviously you can now apply this same files to your db directory.&lt;br /&gt;&lt;b&gt;$&gt; svn propset svn:ignore -F .svn_ignore ./db/&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$&gt; svn st&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Happy times.&lt;/h4&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;a 
style="padding: 0pt 3px; cursor: pointer;" onclick="javascript:location.href='http://reddit.com/submit?url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title)"&gt;
&lt;img alt="reddit" src="http://reddit.com/static/reddit_firefox.png"/&gt;
&lt;/a&gt;
&lt;script src="http://del.icio.us/feeds/js/networkbadge/cammo1979?name;icon" type="text/javascript"&gt;&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8324598090381948093-2843583754480543576?l=codingrants.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codingrants.blogspot.com/feeds/2843583754480543576/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8324598090381948093&amp;postID=2843583754480543576' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/2843583754480543576'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/2843583754480543576'/><link rel='alternate' type='text/html' href='http://codingrants.blogspot.com/2008/05/ignoring-files-with-svn.html' title='Ignoring files with SVN'/><author><name>Cameron Barrie</name><uri>http://www.blogger.com/profile/07717661204895096395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8324598090381948093.post-5591372460356509544</id><published>2008-04-24T10:01:00.033+10:00</published><updated>2008-05-14T09:28:37.546+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><category scheme='http://www.blogger.com/atom/ns#' term='enterprise'/><category scheme='http://www.blogger.com/atom/ns#' term='XML'/><category scheme='http://www.blogger.com/atom/ns#' term='responds_to'/><category scheme='http://www.blogger.com/atom/ns#' term='render'/><category scheme='http://www.blogger.com/atom/ns#' term='XSLT'/><title type='text'>Rending your XML as HTML with XSLT in Rails</title><content type='html'>I had a need to convert XML services that my rails app was providing into customizable html widgets tht could then be in-jested by some sort of server side includes(like NGINX's SSI). So I needed to use XSLT. &lt;br /&gt;&lt;br /&gt;When the W3C wrote the XML specification they also developed the &lt;span style="font-style:italic;"&gt;Extensible Stylesheet Language for Transformations&lt;/span&gt;. XSLT provides a powerful, flexible language for transforming XML documents into something else. In this case, we're going to use it to create HTML documents. &lt;br /&gt;&lt;br /&gt;So to quickly look at how we're rendering xml currently. &lt;br /&gt;&lt;pre class="code"&gt;&lt;br /&gt;def show&lt;br /&gt;  @event = Event.find(params[:id])&lt;br /&gt;  render :xml =&gt; @event&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;OK so we are simply rendering only xml back from this request. Now of course we can react to any registered mime-type we want, obviously HTML is one of those. So we do something like....&lt;br /&gt;&lt;pre class="code"&gt;&lt;br /&gt;def show&lt;br /&gt;  @event = Event.find(params[:id])&lt;br /&gt;  responds_to do |format|&lt;br /&gt;    format.html  &lt;br /&gt;    format.xml { render :xml =&gt; @event }&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, I'm going to assume that you've seen this responds to block before and you know that it's just going to render the show.html.erb template in our views, when html is requested. &lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Installing Ruby's XSLT Library&lt;/h3&gt;&lt;br /&gt;I'm on Mac OS-X 10.5.2 and haven't tested this install on anything else, but it's pretty simple really, unfortunately it's not a gem, but hey, it's not that hard&lt;br /&gt;&lt;br /&gt;First you'll need to download the library, you can get it from &lt;a href="http://gregoire.lejeune.free.fr/ruby-xslt_0.9.2.tar.gz"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Unzip that guy and in your terminal navigate to that directory, then execute the following commands(You'll need Make installed so ensure you've XCode installed on OS-X, or some sort of GCC/Make):&lt;br /&gt;&lt;b&gt;ruby extconf.rb&lt;/b&gt;&lt;br /&gt;&lt;b&gt;make&lt;/b&gt;&lt;br /&gt;&lt;b&gt;make test&lt;/b&gt;&lt;br /&gt;&lt;b&gt;make doc&lt;/b&gt;&lt;br /&gt;&lt;b&gt;sudo make install&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Fire up irb and ensure you return true from require 'xml/xslt' if you get a false, the library hasn't installed properly, you can probably find help at http://greg.rubyfr.net/pub/packages/ruby-xslt/files/README.html&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Getting Rails to render:&lt;/h3&gt;&lt;br /&gt;Firstly, include your new friend in environment.rb&lt;br /&gt;require 'xml/xslt'&lt;br /&gt;&lt;br /&gt;In my application controller I've added a private method&lt;br /&gt;&lt;pre class="code"&gt;&lt;br /&gt;def xslt(_xml, _xslt)&lt;br /&gt;  xlt = XML::XSLT.new&lt;br /&gt;  xlt.xml = render_to_string :xml =&gt; _xml&lt;br /&gt;  xlt.xsl = _xslt&lt;br /&gt;  xlt.serve&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now in your controller method(I'll use the example from above)&lt;br /&gt;&lt;pre class="code"&gt;&lt;br /&gt;def show&lt;br /&gt;  @event = Event.find(params[:id])&lt;br /&gt;  respond_to do |format|&lt;br /&gt;    format.html { render :inline =&gt; xslt(@event, "#{RAILS_ROOT}/xslt_template/show.xslt") } &lt;br /&gt;    format.xml { render :xml =&gt; @event }&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h3&gt;Create an XSLT Style&lt;/h3&gt;&lt;br /&gt;You're of course going to need XSLT styles to apply to pages. &lt;br /&gt;I'll start with the XML I'm using. It's just rendered by the show method from above:&lt;br /&gt;&lt;pre class="code"&gt;&lt;br /&gt;&amp;lt;event&amp;gt;&lt;br /&gt;  &amp;lt;created-at type="datetime"&amp;gt;2008-04-23T18:36:28+10:00&amp;lt;/created-at&amp;gt;&lt;br /&gt;  &amp;lt;date type="date"&amp;gt;2008-04-23&amp;lt;/date&amp;gt;&lt;br /&gt;  &amp;lt;id type="integer"&amp;gt;1&amp;lt;/id&amp;gt;&lt;br /&gt;  &amp;lt;location&amp;gt;Camperdown Park&amp;lt;/location&amp;gt;&lt;br /&gt;  &amp;lt;title&amp;gt;Cardboard Tube Fighting&amp;lt;/title&amp;gt;&lt;br /&gt;  &amp;lt;updated-at type="datetime"&amp;gt;2008-04-23T18:36:28+10:00&amp;lt;/updated-at&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And this is the XSLT template I'll use to style this.&lt;br /&gt;&lt;pre class="code"&gt;&lt;br /&gt;&amp;lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&amp;gt;&lt;br /&gt;  &amp;lt;xsl:output method="html" /&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;xsl:template match="/"&amp;gt;&lt;br /&gt;    &amp;lt;xsl:apply-templates select="event/location" /&amp;gt;&lt;br /&gt;    &amp;lt;xsl:apply-templates select="event/title" /&amp;gt;&lt;br /&gt;  &amp;lt;/xsl:template&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;xsl:template match="title"&amp;gt;&lt;br /&gt;    &amp;lt;h4&amp;gt;&lt;br /&gt;      &amp;lt;xsl:value-of select="." /&amp;gt;&lt;br /&gt;    &amp;lt;/h4&amp;gt;&lt;br /&gt;  &amp;lt;/xsl:template&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;  &amp;lt;xsl:template match="location"&amp;gt;&lt;br /&gt;    &amp;lt;h1&amp;gt;&lt;br /&gt;      &amp;lt;xsl:value-of select="." /&amp;gt;&lt;br /&gt;    &amp;lt;/h1&amp;gt;&lt;br /&gt;  &amp;lt;/xsl:template&amp;gt;&lt;br /&gt;&amp;lt;/xsl:stylesheet&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;h3&gt;Smile like a bandit!&lt;/h3&gt;&lt;br /&gt;terminal: script/server &lt;br /&gt;Navigate to the path of your method and voila you've just applied an XSLT style to your XML stream dynamically.&lt;br /&gt;&lt;br /&gt;Next is for me to roll this into a Rail templating language... But that's another post.&lt;div class="blogger-post-footer"&gt;&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;a 
style="padding: 0pt 3px; cursor: pointer;" onclick="javascript:location.href='http://reddit.com/submit?url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title)"&gt;
&lt;img alt="reddit" src="http://reddit.com/static/reddit_firefox.png"/&gt;
&lt;/a&gt;
&lt;script src="http://del.icio.us/feeds/js/networkbadge/cammo1979?name;icon" type="text/javascript"&gt;&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8324598090381948093-5591372460356509544?l=codingrants.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codingrants.blogspot.com/feeds/5591372460356509544/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8324598090381948093&amp;postID=5591372460356509544' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/5591372460356509544'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/5591372460356509544'/><link rel='alternate' type='text/html' href='http://codingrants.blogspot.com/2008/04/rending-your-xml-as-html-with-xslt-in.html' title='Rending your XML as HTML with XSLT in Rails'/><author><name>Cameron Barrie</name><uri>http://www.blogger.com/profile/07717661204895096395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8324598090381948093.post-5489961748769522495</id><published>2008-04-17T11:59:00.005+10:00</published><updated>2008-04-17T12:47:07.951+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='art'/><category scheme='http://www.blogger.com/atom/ns#' term='creative'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Programmers aren't creative</title><content type='html'>I used to be a photographer, and recently I've been selling some of my gear to new photographic hopefuls. Of course the inevitable questions arises about what I do now? &lt;br /&gt;&lt;span style="font-style:italic;"&gt;I'm a programmer I respond. &lt;/span&gt;&lt;br /&gt;The usual response is: &lt;br /&gt;&lt;span style="font-style:italic;"&gt;Oh, that's not a very creative field to go to. I mean you used to be a photographer. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Well I'm going to put this question out there to the world. Is programming not creative? Really stop and think about it.&lt;br /&gt;As someone who has come from a creative field to a "non-creative" field, I can tell you that in my opinion programming is very creative. Programmers are constantly asked to produce a completely new thing, from nothing at all. To create a program. Photographers are generally asked to capture something there in front of them. Now I'm not here to start the whole photography isn't creative debate I feel there is a degree of creativity in photography, but why does programming seem to have this non-creative air about it. &lt;br /&gt;Is it Bill Gates fault? Or all the other a-typical nerd looking boys made infamous in  the '80s for the development of Windows etc. How long will it take for people to put together the iPod/iMac/iPhone with programmers? All those cool funky user interfaces, those problems are solved by programmers... Are they not creative solutions? &lt;br /&gt;&lt;br /&gt;So we come to the question, what is creativity?&lt;br /&gt;&lt;span style="font-style:italic;"&gt;&lt;br /&gt;the ability to transcend traditional ideas, rules, patterns, relationships, or the like, and to create meaningful new ideas, forms, methods, interpretations, etc.; originality, progressiveness, or imagination&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;I don't know about you, but that pretty well sums up about 8/10ths of my job as a programmer. &lt;br /&gt;&lt;br /&gt;Anyone else feel that their industry is unnecessarily brandished with the non-creative iron?&lt;div class="blogger-post-footer"&gt;&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;a 
style="padding: 0pt 3px; cursor: pointer;" onclick="javascript:location.href='http://reddit.com/submit?url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title)"&gt;
&lt;img alt="reddit" src="http://reddit.com/static/reddit_firefox.png"/&gt;
&lt;/a&gt;
&lt;script src="http://del.icio.us/feeds/js/networkbadge/cammo1979?name;icon" type="text/javascript"&gt;&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8324598090381948093-5489961748769522495?l=codingrants.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codingrants.blogspot.com/feeds/5489961748769522495/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8324598090381948093&amp;postID=5489961748769522495' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/5489961748769522495'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/5489961748769522495'/><link rel='alternate' type='text/html' href='http://codingrants.blogspot.com/2008/04/programmers-arent-creative.html' title='Programmers aren&apos;t creative'/><author><name>Cameron Barrie</name><uri>http://www.blogger.com/profile/07717661204895096395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8324598090381948093.post-8312897231458804133</id><published>2008-04-16T21:17:00.019+10:00</published><updated>2008-04-16T21:59:51.388+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='rspec'/><category scheme='http://www.blogger.com/atom/ns#' term='mocks'/><category scheme='http://www.blogger.com/atom/ns#' term='outside in development'/><category scheme='http://www.blogger.com/atom/ns#' term='stubs'/><category scheme='http://www.blogger.com/atom/ns#' term='bdd'/><category scheme='http://www.blogger.com/atom/ns#' term='mocking'/><title type='text'>Mocking and Stubbing in rSpec</title><content type='html'>I program in Ruby. A lot. I've recently found myself writing applications to provide simple web services to flash applications. We've pretty well standardized on using JSON as our preferred format at Snepo, so I've found it useless attempting to build and delivery anything without using some sort of TDD.&lt;br /&gt;&lt;br /&gt;Welcome rSpec to my world, and it adds in a level of communication between myself and the involved parties about what a service should do, that I'm not sure any other framework does. For instance, we can begin specifying behaviors that the controller should return. &lt;br /&gt;I might have a service that finds me all users who've been active in the last 24 hours. So I'd specify, like follows.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;describe "Users Controller", "find_recent action" do&lt;br /&gt; it "should find all users who've been active in the last 24 hours" do&lt;br /&gt;   c, r = request("/users/find_recent/24")&lt;br /&gt;   c.body.should eql(["some json array of elements"])&lt;br /&gt; end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;But! Here is our first problem with outside in development, we don't have a any methods in a User model yet! Now we could go off and build some, but this behavior might change as we move forward and in any case, we should test those methods with their own tests, this is testing the controller class after all. So why not try to get as much of this working how we want now. Well we can.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Hello Mocks and Stubs!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Mocks and Stubs confused me for a long time. The official definition in rSpec says: &lt;span style="font-style: italic;"&gt;Mock objects are imitation objects that give you declarative control over their behaviour in the course of the execution of an example&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;/span&gt;I found this really confusing so I'll try to explain it, in my own words.&lt;br /&gt;A mock allows you to send a message to an object(including a class, a class is an object after all) that does not yet exist( that is the message/method does not yet exist ). The rSpec framework looks out for the method that your test to call this object, and then returns what you specified in your test. If it is never called it considers that the test has failed. That is, a mock is a test in itself, a stub is not. A stub simply substitutes itself for the real object. So to flesh out the above example with a mock.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;describe "Users Controller", "find_recent action" do&lt;br /&gt;  it "should find all users who've been active in the last 24 hours" do&lt;br /&gt;    @users = mock("A list of users")&lt;br /&gt;    User.should_receive(:find_all).with(24).and_return(@users)&lt;br /&gt;    c, r = request("/users/find_recent/24")&lt;br /&gt;    c.body.should eql(@users.to_json)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;What we are doing here is creating a new mock object(@users) so we have something to return from our mock of the static method find_all. That happens in the line below, we tell rspec that it should_receive a message (:find_all) for a class called User, and when it does receive that message to return the mock object that we created above for the controller to keep on using it.&lt;br /&gt;If we just wanted to stub that, i.e. if we didn't care if it got called, but if it does we'd better return something, we'd use: User.stub!(:find_all).and_return(@users).&lt;br /&gt;&lt;br /&gt;There is one small misnomer in that whole mocking stubbing framework in rSpec, and that is @users = mock("A list of users"). This is not a mock, if it's not used rSpec doesn't care, there is no expectation placed on this object, to me this should be something like stub_object, in any case, that's a little gottcha albeit a linguistic one. &lt;br /&gt;Now you see, you can get to it from the outside in this time and you can get all the model method's that you'll &lt;span style="font-weight: bold;"&gt;need &lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;sorted before your write a scrap of code in your models.&lt;br /&gt;Oh and just so you know, your controller method might look something like this( in Merb ).&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;def find_recent&lt;br /&gt;  users = User.find_recent(params[:duration].to_i)&lt;br /&gt;  render_text users.to_json&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;a 
style="padding: 0pt 3px; cursor: pointer;" onclick="javascript:location.href='http://reddit.com/submit?url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title)"&gt;
&lt;img alt="reddit" src="http://reddit.com/static/reddit_firefox.png"/&gt;
&lt;/a&gt;
&lt;script src="http://del.icio.us/feeds/js/networkbadge/cammo1979?name;icon" type="text/javascript"&gt;&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8324598090381948093-8312897231458804133?l=codingrants.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/8312897231458804133'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/8312897231458804133'/><link rel='alternate' type='text/html' href='http://codingrants.blogspot.com/2008/04/mocking-and-stubbing-in-rspec.html' title='Mocking and Stubbing in rSpec'/><author><name>Cameron Barrie</name><uri>http://www.blogger.com/profile/07717661204895096395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8324598090381948093.post-2109731247814482595</id><published>2008-03-04T15:26:00.014+11:00</published><updated>2008-04-17T11:23:43.925+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='method_missing'/><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><title type='text'>Ruby's Method Missing</title><content type='html'>&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;I love programming in Ruby. Sometimes I need to do stuff in other languages, and for all intensive purposes, it's fine. I've no problem firing up Visual Studio and hacking out a bit of C#. But curly braces and brackets around every function aside and I think Ruby offers something that is often overlooked in lots of other languages. Smalltalk introduced the idea some time ago with doesNotUnderstand, but I'll explain it in the context of Ruby. &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;In Ruby everything is an object: &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;"A String".class =&gt; String &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;1.class =&gt; Fixnum. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;Let me repeat everything is an object &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;1.class.class =&gt; Class&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;And from the wonderful Programming Ruby book by Dave Thomas in reference to Ruby's class &lt;/span&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;Object&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;"Object is the parent class of all classes in Ruby . Its methods are therefore available to all objects unless explicitly overridden." -- &lt;/span&gt;&lt;a href="http://www.pragprog.com/titles/ruby"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;GetItHere&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;One of the Object methods(functions are called methods in Ruby) which are automatically inferred to all objects is method_missing. Method missing is where your method request will end up if the method you called cannot be found. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;Here is a really simple trivial example:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;-----------------------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;class Fixnum  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;  def method_missing(method, arg)  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;    puts "Called: #{method}"&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;    return self * arg&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;  end&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;puts 10.mutiply_by(100)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;----------------------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51); font-size: 13px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51); font-size: 13px;"&gt;--------------------------&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;OUTPUT:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Called: multiply_by&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;1000&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51); font-size: 13px;"&gt;-------------------------&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;Let's just explain this quickly. It's trivial and you'd never bother doing this but it does explain the concept. We're essentially re-writing the method_missing method for the Fixnum class. This will not effect the method_missing class in any other class, including most importantly object.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;You can prove this to yourself by changing the 10 to a 10.2, you'll now throw an error explain that multiply_by is not a method of the Float class, since 10.2 is a float not a Fixnum.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;Anyhow we digress, back to the code. method_missing takes at least 2 args, the first one is always the method that was attempting to be called, in the case of our Fixnum method_missing it's called method, and I'm also only assuming one argument from the caller. But is could easily take an array of args even a proc object.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;So we write to stdout using puts to log what method was being called, and then simply take the args and multiply it by self, in this scope that is 10. Hence we get 1000 since 10*100 == 1000&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;But of course this is stupid, you'd never do this. The real power of method_missing comes in when you want to dynamically deal with methods. Creating API's for instance. You can now dynamically catch(rescue is Ruby land) anything that isn't a method. You can log it and return a proper message. All in 5 lines of code. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;---------------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;class User&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt; ....accessors and methods&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;private  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;  def method_missing(method, *args)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;    puts "Unavailable method called on #{self.class}: #{method}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;    puts "With args: #{args.inspect}"&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;    return "Method: #{method} does not exist"&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;  end&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;---------------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;This will allow methods called on the user object that do not exist to simply return you a text string, rather than raising an error. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;But I want to raise an error you say, I want to log it and then raise the error. Fine, no problems, change the &lt;/span&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;return&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt; line to:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;super(method, args)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;You'll now call the original method_missing from the object class after you've executed puts. In essence you get to do stuff before your error is raised.  Cool ehh.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;You'll see method_missing splattered all over Ruby code, and there are some outstanding uses of it out there. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;Here's an example from Rails:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;Check out routing.rb in Rails core for example. method_missing is used to generate all those named routes. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;So in your routes table when you name a route like so:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;map.manage_user "some/nicely_formated_url", :controller =&gt; "blah", :action =&gt; "blah"&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: georgia; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);"&gt;What actually happens is method_missing grabs that, and calls a method &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;add_named_route&lt;/span&gt;, which in turn adds your route with the custom name to the hash called named_routes and also to an array called routes which is later used to determine where URLs are to be routed to.... Wicked!&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;a 
style="padding: 0pt 3px; cursor: pointer;" onclick="javascript:location.href='http://reddit.com/submit?url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title)"&gt;
&lt;img alt="reddit" src="http://reddit.com/static/reddit_firefox.png"/&gt;
&lt;/a&gt;
&lt;script src="http://del.icio.us/feeds/js/networkbadge/cammo1979?name;icon" type="text/javascript"&gt;&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8324598090381948093-2109731247814482595?l=codingrants.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/2109731247814482595'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8324598090381948093/posts/default/2109731247814482595'/><link rel='alternate' type='text/html' href='http://codingrants.blogspot.com/2008/03/rubys-method-missing-super-mega-fully.html' title='Ruby&apos;s Method Missing'/><author><name>Cameron Barrie</name><uri>http://www.blogger.com/profile/07717661204895096395</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry></feed>
