From: Gary Wright Date: 2007-03-17T03:28:58+09:00 Subject: Re: Anyone playing with higher order messaging in ruby? On Mar 16, 2007, at 1:32 PM, Jenda Krynicky wrote: > it's a shame the builtins do not work like this already. And while > it's > possible to overwrite the "collect", "select" and "reject" in > Enumerable, to overwrite the "each" you'd (AFAIK) have to touch all > the > individual classes. Somewhat related.... In Ruby 1.9 iterating methods (I couldn't think of a better term) such as each, map, and so on return an Enumerator object if a block isn't provided. This makes it easier to add the magic dot behavior: class Enumerable::Enumerator def method_missing(*args, &block) each { |x| x.send(*args, &block ) } end end [1,-2,3,-4].map.abs # [1,2,3,4] [1,-2,3,-4].map.abs.map.succ # [2,3,4,5] %w{mon tue wed}.map.capitalize # ["Mon", "Tue", "Wed"]` This works because Enumerable::Enumerator#each returns the original collection (instead of the iterator) when a block is provided. [1,2].each.class # Enumerator [1,2].each.each.class # Enumerator [1,2].each.each {}.class # Array Gary Wright