From: Trans Date: 2007-07-19T01:17:26+09:00 Subject: Re: Are we cool? method_missing in enumerator On Jul 18, 8:01 am, Sammy Larbi wrote: > I might test that it can still throw missing method exception if the > called method isn't supposed to be doing what you're making it do (ie, > perhaps it was a typo) > > Sam > > Trans wrote, On 7/17/2007 9:01 AM: > > > Can anyone demonstrate problems with the following? > > > # Adds immediate elementwise ability to Enumerator. > > # Any non-enumeration method is passed on to each. > > # > > # [1,2,3].to_enum(:map) + 3 > > # => [4,5,6] > > # > > # Note there may be a few essential methods this can not > > # be used on, such as #object_id. > > > class Enumerable::Enumerator > > def method_missing(sym,*args,&blk) > > each{ |x| x.send(sym,*args,&blk) } > > end > > end > > > I like the simplicity of this and would rather use it than add an > > another class layer, but I need to be sure I'm not overlooking any > > potential dangers with doing so. Yea, that's the "danger" part, because it's going to try to apply that typo to the enumerable's elements. I many cases that will no doubt fail --in which case maybe it wouldn't hurt to but it in a begin rescue clause in order to give a more comprehensible error message. OTOH, some typos might get through, in which case testing is key. I think I've come up with a good alternative that is safer, but still reads well (as of 1.9): arr.map.x + 3 What do you think? T.