<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-4227402699056124995</id><updated>2008-01-06T17:56:37.607-08:00</updated><title type='text'>Ruby Fu</title><link rel='alternate' type='text/html' href='http://www.rubyfu.com/'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default'/><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.rubyfu.com/atom.xml'/><author><name>Brendan Baldwin</name></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4227402699056124995.post-5886888090841887932</id><published>2007-06-19T15:36:00.000-07:00</published><updated>2007-06-19T16:11:34.955-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Associations'/><category scheme='http://www.blogger.com/atom/ns#' term='BelongsToAssociation'/><category scheme='http://www.blogger.com/atom/ns#' term='belongs_to'/><category scheme='http://www.blogger.com/atom/ns#' term='ActiveRecord'/><category scheme='http://www.blogger.com/atom/ns#' term='AssociationProxy'/><category scheme='http://www.blogger.com/atom/ns#' term='transparent proxies'/><category scheme='http://www.blogger.com/atom/ns#' term='target'/><title type='text'>Transparent Proxies, Part 2</title><content type='html'>Here's a quick case study where we got bit by ActiveRecord's use of a Transparent Proxy, namely ActiveRecord::Associations::BelongsToAssociation.&lt;br /&gt;&lt;br /&gt;If you don't plan on reading this article, at least walk away with this:&lt;br /&gt;"Don't EVER use the word 'target' for any attribute, association, or method on any of your ActiveRecord models!!!"&lt;br /&gt;&lt;br /&gt;Why?  Here's a quick example.&lt;br /&gt;&lt;br /&gt;Consider this migration:&lt;br /&gt;&lt;pre class="textmate-source"&gt;&lt;span class="source source_ruby source_ruby_rails"&gt;&lt;span class="meta meta_class meta_class_ruby"&gt;&lt;span class="keyword keyword_control keyword_control_class keyword_control_class_ruby"&gt;class&lt;/span&gt; &lt;span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby"&gt;CreateTables&lt;span class="entity entity_other entity_other_inherited-class entity_other_inherited-class_ruby"&gt; &lt;span class="punctuation punctuation_separator punctuation_separator_inheritance punctuation_separator_inheritance_ruby"&gt;&amp;lt;&lt;/span&gt; ActiveRecord::Migration&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"&gt;&lt;span class="keyword keyword_control keyword_control_def keyword_control_def_ruby"&gt;def&lt;/span&gt; &lt;span class="entity entity_name entity_name_function entity_name_function_ruby"&gt;self.up&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    create_table &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;missiles&lt;/span&gt; &lt;span class="keyword keyword_control keyword_control_ruby keyword_control_ruby_start-block"&gt;do &lt;/span&gt;|t|&lt;br /&gt;      t.column &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;name&lt;/span&gt;,    &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;string&lt;/span&gt;&lt;br /&gt;      t.column &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;silo_id&lt;/span&gt;, &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;integer&lt;/span&gt;&lt;br /&gt;    &lt;span class="keyword keyword_control keyword_control_ruby"&gt;end&lt;/span&gt;&lt;br /&gt;    create_table &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;silos&lt;/span&gt; &lt;span class="keyword keyword_control keyword_control_ruby keyword_control_ruby_start-block"&gt;do &lt;/span&gt;|t|&lt;br /&gt;      t.column &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;name&lt;/span&gt;,   &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;string&lt;/span&gt;&lt;br /&gt;      t.column &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;target&lt;/span&gt;, &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;string&lt;/span&gt;&lt;br /&gt;    &lt;span class="keyword keyword_control keyword_control_ruby"&gt;end&lt;/span&gt;&lt;br /&gt;  &lt;span class="keyword keyword_control keyword_control_ruby"&gt;end&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"&gt;&lt;span class="keyword keyword_control keyword_control_def keyword_control_def_ruby"&gt;def&lt;/span&gt; &lt;span class="entity entity_name entity_name_function entity_name_function_ruby"&gt;self.down&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    drop_table &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;missiles&lt;/span&gt;&lt;br /&gt;    drop_table &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;silos&lt;/span&gt;&lt;br /&gt;  &lt;span class="keyword keyword_control keyword_control_ruby"&gt;end&lt;/span&gt;&lt;br /&gt;&lt;span class="keyword keyword_control keyword_control_ruby"&gt;end&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Now consider these two models:&lt;br /&gt;&lt;pre class="textmate-source"&gt;&lt;span class="source source_ruby source_ruby_rails"&gt;&lt;span class="meta meta_rails meta_rails_model"&gt;&lt;span class="meta meta_class meta_class_ruby"&gt;&lt;span class="keyword keyword_control keyword_control_class keyword_control_class_ruby"&gt;class&lt;/span&gt; &lt;span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby"&gt;Missile&lt;span class="entity entity_other entity_other_inherited-class entity_other_inherited-class_ruby"&gt; &lt;span class="punctuation punctuation_separator punctuation_separator_inheritance punctuation_separator_inheritance_ruby"&gt;&amp;lt;&lt;/span&gt; ActiveRecord::Base&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class="support support_function support_function_activerecord support_function_activerecord_rails"&gt;belongs_to&lt;/span&gt; &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;silo&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="keyword keyword_control keyword_control_ruby"&gt;end&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="meta meta_rails meta_rails_model"&gt;&lt;span class="meta meta_class meta_class_ruby"&gt;&lt;span class="keyword keyword_control keyword_control_class keyword_control_class_ruby"&gt;class&lt;/span&gt; &lt;span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby"&gt;Silo&lt;span class="entity entity_other entity_other_inherited-class entity_other_inherited-class_ruby"&gt; &lt;span class="punctuation punctuation_separator punctuation_separator_inheritance punctuation_separator_inheritance_ruby"&gt;&amp;lt;&lt;/span&gt; ActiveRecord::Base&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class="support support_function support_function_activerecord support_function_activerecord_rails"&gt;has_many&lt;/span&gt; &lt;span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"&gt;&lt;span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby"&gt;:&lt;/span&gt;missiles&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="keyword keyword_control keyword_control_ruby"&gt;end&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;We have a Missile model which belongs_to a Silo, and a Silo can point to a 'target'.  Lets create our Missile and Silo objects and see what happens...&lt;br /&gt;&lt;pre class="textmate-source"&gt;&lt;span class="text text_plain"&gt;&lt;span class="meta meta_paragraph meta_paragraph_text"&gt;&amp;gt;&amp;gt; the_tube = Silo.create(:name =&amp;gt; 'The Tube', :target =&amp;gt; 'Washington, DC')&lt;br /&gt;=&amp;gt; #&amp;lt;Silo:0x2855d8c&amp;gt;&lt;br /&gt;&amp;gt;&amp;gt; boomer = Missile.create(:name =&amp;gt; 'Boomer', :silo =&amp;gt; the_tube)&lt;br /&gt;=&amp;gt; #&amp;lt;Missile:0x27904c4&amp;gt;&lt;br /&gt;&amp;gt;&amp;gt; silo.target&lt;br /&gt;=&amp;gt; "Washingon, DC"&lt;br /&gt;&lt;br /&gt;# okay, lets fire our missile at the target&lt;br /&gt;# boomer launches and calls home to get its&lt;br /&gt;# targeting information...&lt;br /&gt;&lt;br /&gt;&amp;gt;&amp;gt; boomer.silo.target&lt;br /&gt;=&amp;gt; #&amp;lt;Silo:0x2855d8c&amp;gt;&lt;br /&gt;&lt;br /&gt;# wtf?  its pointed at a silo?&lt;br /&gt;&lt;br /&gt;&amp;gt;&amp;gt; boomer.silo.target == 'Washington, DC'&lt;br /&gt;=&amp;gt; false&lt;br /&gt;&lt;br /&gt;&amp;gt;&amp;gt; boomer.silo.target == the_tube&lt;br /&gt;=&amp;gt; true&lt;br /&gt;&lt;br /&gt;# RUN!!!!&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Did you catch that?  Our Missile, 'Boomer', says that its silo, 'The Tube', is NOT targeting at 'Washington, DC' like we asked it to!  It's targeting itself!!!&lt;br /&gt;&lt;br /&gt;WHY!?!&lt;br /&gt;&lt;br /&gt;Well the reason for this has to do with the *magic* behind ActiveRecord's &lt;tt&gt;BelongsToAssociation&lt;/tt&gt; class, which defines a 'Transparent Proxy' object.  I posted a little about these before: &lt;a href="http://www.rubyfu.com/2007/05/transparent-proxies-part-1.html"&gt;Transparent Proxies, Part 1&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;What's going on is that when we accessed the &lt;tt&gt;silo&lt;/tt&gt; association on our instance variable &lt;tt&gt;boomer&lt;/tt&gt;, what we *actually* get back is an instance of the &lt;tt&gt;ActiveRecord::Associations::BelongsToProxy&lt;/tt&gt; class.  It's such a neat proxy, you probably never realized that you were dealing with it, because when you have one, you can't tell by looking at it that you don't just have an instance of the &lt;tt&gt;Silo&lt;/tt&gt; class we defined.&lt;br /&gt;&lt;br /&gt;So where does 'target' come in to play?  Well, it turns out that the BelongsToProxy isn't completely *Transparent*, it does have some methods defined, one of which happens to be &lt;tt&gt;target&lt;/tt&gt;.  And in the case of Silos and Missiles, this is unfortunately a reference to the object referenced in the &lt;tt&gt;belongs_to&lt;/tt&gt; association!&lt;br /&gt;&lt;br /&gt;Fortunately for Washington, DC, this attack has been averted.  Unfortunately for this web developer, its time to redefine this model's schema.  But with just a little refactoring, the next Missile will find its mark!</content><link rel='alternate' type='text/html' href='http://www.rubyfu.com/2007/06/transparent-proxies-part-2.html' title='Transparent Proxies, Part 2'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4227402699056124995&amp;postID=5886888090841887932' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.rubyfu.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/5886888090841887932'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/5886888090841887932'/><author><name>Brendan Baldwin</name></author></entry><entry><id>tag:blogger.com,1999:blog-4227402699056124995.post-2243008176928719237</id><published>2007-06-04T21:10:00.000-07:00</published><updated>2007-06-05T05:02:29.768-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='regexp'/><category scheme='http://www.blogger.com/atom/ns#' term='regular expressions'/><category scheme='http://www.blogger.com/atom/ns#' term='named_captures.rb'/><title type='text'>Named Captures for Regular Expressions</title><content type='html'>Tonight at the &lt;a href="http://www.chirb.org"&gt;Chicago Area Ruby Meetup (CHIRB)&lt;/a&gt;, we had some lightning-round presentations, and without too much preparation I decided to release and demo some code I had written for dealing with Regular Expressions.&lt;br /&gt;&lt;br /&gt;The library in essence allows you to use the totally under-utilized embedded-comment faculty in Ruby's Regular Expression Extensions to label your captures.&lt;br /&gt;&lt;br /&gt;For those who don't know, a comment in a Ruby Regexp looks like &lt;tt&gt;(?#this)&lt;/tt&gt;, so if you want to label a capture, you can embed comments in the parentheses for that capture, like this: &lt;tt&gt;/((?#number)\d+)-((?#word)\w+)/&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;Of course that doesn't buy you anything on its own, so I wrote a library called 'named_captures.rb' available in my public svn repository &lt;a href="http://usergenic.com/svn/public/ruby/fu/trunk/lib/regexp/named_captures.rb"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;With this library, you can now use that Regexp above like so:&lt;br /&gt;&lt;pre&gt;my_regexp = /((?#number)\d+)-((?#word)\w+)/&lt;br /&gt;my_match = my_regexp.match("i got 128-bit encryption!")&lt;br /&gt;my_match[:number] # &amp;lt;= '128'&lt;br /&gt;my_match[:word]   # &amp;lt;= 'bit'&lt;/pre&gt;&lt;br /&gt;Feel free to explore and send me email if you use this at &lt;a href="mailto:brendan@usergenic.com"&gt;brendan@usergenic.com&lt;/a&gt;.  Comments on this post welcome as well.</content><link rel='alternate' type='text/html' href='http://www.rubyfu.com/2007/06/named-captures-for-regular-expressions.html' title='Named Captures for Regular Expressions'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4227402699056124995&amp;postID=2243008176928719237' title='3 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.rubyfu.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/2243008176928719237'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/2243008176928719237'/><author><name>Brendan Baldwin</name></author></entry><entry><id>tag:blogger.com,1999:blog-4227402699056124995.post-8371372733846426277</id><published>2007-05-30T13:16:00.000-07:00</published><updated>2007-06-05T05:34:48.717-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='with_options'/><category scheme='http://www.blogger.com/atom/ns#' term='block'/><category scheme='http://www.blogger.com/atom/ns#' term='routes'/><title type='text'>Anonymous Block Pitfall</title><content type='html'>You should understand this...&lt;br /&gt;&lt;pre&gt;a=[:x,:y,:z] # =&amp;gt; [:x, :y, :z]&lt;br /&gt;a.map{|a|}   # =&amp;gt; [nil, nil, nil]&lt;br /&gt;a            # =&amp;gt; :z&lt;/pre&gt;&lt;br /&gt;...to know why this &lt;tt&gt;routes.rb&lt;/tt&gt; example using &lt;tt&gt;with_options&lt;/tt&gt;...&lt;br /&gt;&lt;pre&gt;map.with_options :controller =&amp;gt; 'users',&lt;br /&gt;                 :action =&amp;gt; 'show' do |map|&lt;br /&gt;  map.user_groups  '/users/:id/groups',  :section =&amp;gt; 'groups'&lt;br /&gt;  map.user_history '/users/:id/history', :section =&amp;gt; 'history'&lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;...is bad.&lt;br /&gt;&lt;br /&gt;Don't reuse the name of a local variable as a yielded local variable for use inside of a block, because the assignment will persist after the lifespan of the block.  The routes example should look more like this...&lt;br /&gt;&lt;pre&gt;map.with_options :controller =&amp;gt; 'users',&lt;br /&gt;                 :action =&amp;gt; 'show' do |users|&lt;br /&gt;  users.user_groups  '/users/:id/groups',  :section =&amp;gt; 'groups'&lt;br /&gt;  users.user_history '/users/:id/history', :section =&amp;gt; 'history'&lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;That is all.</content><link rel='alternate' type='text/html' href='http://www.rubyfu.com/2007/05/anonymous-block-pitfall.html' title='Anonymous Block Pitfall'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4227402699056124995&amp;postID=8371372733846426277' title='1 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.rubyfu.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/8371372733846426277'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/8371372733846426277'/><author><name>Brendan Baldwin</name></author></entry><entry><id>tag:blogger.com,1999:blog-4227402699056124995.post-4527939442554443371</id><published>2007-05-16T12:19:00.000-07:00</published><updated>2007-06-05T05:37:14.707-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='super'/><category scheme='http://www.blogger.com/atom/ns#' term='inheritance'/><category scheme='http://www.blogger.com/atom/ns#' term='class'/><category scheme='http://www.blogger.com/atom/ns#' term='method'/><title type='text'>super is super</title><content type='html'>The super keyword is pretty sweet.  It remembers the context under which it was used, even when you call an alias of the containing method.  Example:&lt;br /&gt;&lt;pre&gt;class X&lt;br /&gt;  def method&lt;br /&gt;    "got it"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;class Y &amp;lt; X&lt;br /&gt;  def method&lt;br /&gt;    "#{super}!"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;class Z &amp;lt; Y&lt;br /&gt;  alias_method :original_method, :method&lt;br /&gt;  def method&lt;br /&gt;    "you #{original_method}"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;X.new.method # &amp;lt;= "got it"&lt;br /&gt;Y.new.method # &amp;lt;= "got it!"&lt;br /&gt;Z.new.method # &amp;lt;= "you got it!"&lt;/pre&gt;&lt;br /&gt;This behavior gives alias_method real flexibility when used to modify behaviors by composing method chains to pre-defined methods.  I was first surprised by this behavior, but it was a pleasant surprise as it meant I didn't have to worry about 'super' suddenly referring to something else when delegating to aliased methods.</content><link rel='alternate' type='text/html' href='http://www.rubyfu.com/2007/05/super-is-super.html' title='super is super'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4227402699056124995&amp;postID=4527939442554443371' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.rubyfu.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/4527939442554443371'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/4527939442554443371'/><author><name>Brendan Baldwin</name></author></entry><entry><id>tag:blogger.com,1999:blog-4227402699056124995.post-106063482952737835</id><published>2007-05-15T15:32:00.000-07:00</published><updated>2007-06-07T06:09:02.286-07: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='ObjectSpace'/><category scheme='http://www.blogger.com/atom/ns#' term='transparent proxies'/><title type='text'>Transparent Proxies, Part 1</title><content type='html'>This is an old one, but I tightened up the code a little bit.  Take a look at the following:&lt;br /&gt;&lt;pre&gt;module Enumerable&lt;br /&gt;  def all&lt;br /&gt;    MapProxy.new self&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;class Enumerable::MapProxy&lt;br /&gt;  instance_methods.each do |m|&lt;br /&gt;    undef_method m unless m.to_s=~/^__(id|send)__$/&lt;br /&gt;  end&lt;br /&gt;  private&lt;br /&gt;  def initialize proxied&lt;br /&gt;    @proxied=proxied&lt;br /&gt;  end&lt;br /&gt;  def method_missing m,*a,&amp;b&lt;br /&gt;    @proxied.map{|o|o.__send__ m, *a, &amp;b} # thanks, NiKA&lt;br /&gt;  end&lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;Basically it lets you write &lt;tt&gt;[1,2,3].all.to_s&lt;/tt&gt; and get back &lt;tt&gt;["1","2","3"]&lt;/tt&gt;.  It does so by creating an instance of the &lt;tt&gt;Enumerable::MapProxy&lt;/tt&gt; class that dispatches &lt;em&gt;any&lt;/em&gt; method you send it right on down to each element of the &lt;tt&gt;Enumerable&lt;/tt&gt; object (an &lt;tt&gt;Array&lt;/tt&gt; in the case of &lt;tt&gt;[1,2,3]&lt;/tt&gt;).  It's called &lt;tt&gt;MapProxy&lt;/tt&gt; because it dispatches the method to each element by way of the &lt;tt&gt;#map&lt;/tt&gt; method.&lt;br /&gt;&lt;br /&gt;Its pretty convenient for some kinds of operations, but there's a downside to using Transparent Proxies-- I call them "Transparent" because as far as you can tell when you interact with it, it doesn't actually identify itself in the traditional way.  If you say &lt;tt&gt;[1,2,3].all.class&lt;/tt&gt; you don't get back a class, you get an Array of classes, which are the classes of the elements in the collection.&lt;br /&gt;&lt;br /&gt;The downside therefore is that you get this object with extremely interesting behaviour, but don't really know what's going on unless you can figure out where the object came from.&lt;br /&gt;&lt;br /&gt;If you use Rails, you already use Transparent Proxies, since the &lt;tt&gt;AssociationProxy&lt;/tt&gt; class in the &lt;tt&gt;ActiveRecord::Associations&lt;/tt&gt; module is a well-known example of one.  In many ways the effect is seamless and great, but it can be the cause of some pretty bizarre behaviour, especially in light of the way it chooses to direct method_missing calls back down to the class the association represents.&lt;br /&gt;&lt;br /&gt;For example, did you know that if you have a &lt;tt&gt;belongs_to :customer&lt;/tt&gt; association on your Order class, that &lt;tt&gt;@order.belongs_to&lt;/tt&gt; &lt;em&gt;actually&lt;/em&gt; returns an instance of the AssociationProxy class?&lt;br /&gt;&lt;br /&gt;If you are ever stuck with an object that you "think" might be an instance of a specific Transparent Proxy class, here is a handy way to find out:&lt;br /&gt;&lt;pre&gt;class Class&lt;br /&gt;  def one_of_me? object&lt;br /&gt;    ObjectSpace.each_object(self) do |o|&lt;br /&gt;      return true if o.__id__==object.__id__&lt;br /&gt;    end&lt;br /&gt;    return false&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# to demonstrate...&lt;br /&gt;&lt;br /&gt;Enumerable::MapProxy.&lt;br /&gt;  one_of_me?([1,2,3].all) #&amp;lt;= true&lt;br /&gt;&lt;br /&gt;ActiveRecord::Associations::AssociationProxy.&lt;br /&gt;  one_of_me?(@order.customer) #&amp;lt;= true&lt;/pre&gt;</content><link rel='alternate' type='text/html' href='http://www.rubyfu.com/2007/05/transparent-proxies-part-1.html' title='Transparent Proxies, Part 1'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4227402699056124995&amp;postID=106063482952737835' title='2 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.rubyfu.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/106063482952737835'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/106063482952737835'/><author><name>Brendan Baldwin</name></author></entry><entry><id>tag:blogger.com,1999:blog-4227402699056124995.post-6743817436971038473</id><published>2007-05-15T13:08:00.000-07:00</published><updated>2007-05-15T16:21:32.642-07:00</updated><title type='text'>Hello World</title><content type='html'>Greetings, programs!&lt;br /&gt;&lt;br /&gt;My name is Brendan Baldwin and this is the inaugural post of &lt;a href="http://www.rubyfu.com"&gt;RubyFu.com&lt;/a&gt;, a weblog dedicated to Ruby development.  I'm starting this on a shared hosting account with limited headspace, so I'm opting for static file delivery, published via &lt;a href="http://www.blogger.com"&gt;Blogger.com&lt;/a&gt;.  As such, I'm not quite sure how things like comment moderation and permissions are going to work, so this is a test post.  Feel free to contact me with any issues you encounter with this weblog at brendan at ruby fu dot com.&lt;br /&gt;&lt;br /&gt;End of line.</content><link rel='alternate' type='text/html' href='http://www.rubyfu.com/2007/05/hello-world.html' title='Hello World'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4227402699056124995&amp;postID=6743817436971038473' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.rubyfu.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/6743817436971038473'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4227402699056124995/posts/default/6743817436971038473'/><author><name>Brendan Baldwin</name></author></entry></feed>