From: Patrick Fernie Date: 2005-08-03T01:40:08+09:00 Subject: Re: my map vs. collect bone Regardless of the fact I like #map more than #collect, here's my thought on maintaing both map_ and collect_: why not use a little meta-programming? I'm not at my dev machine atm, so no ruby, but generally: module Enumerable def Enumerable.method_added(meth) if meth.to_s =~ /^collect_/ alias_method meth.to_s.sub(/^collect/, 'map').to_sym, meth end end end module Enumerable def collect_test puts "This is the new method" end end a = [] a.collect_test #=> "This is the new method" a.map_test #=> "This is the new method" Or, you could use method_missing, which will help when defining methods in classes including the module (the above case only works for methods defined in the Enumerable module, I believe (away from Ruby atm, so correct me if I'm wrong)): module Enumerable def method_missing(meth, *args) if meth.to_s =~ /^map_/ self.send(meth.to_s.sub(/^map/, 'collect').to_sym, *args) else NoMethodError.new(meth.to_s) end end end class Array def collect_another(str) puts "Another: #{str}" end end a = [] a.collect_another("yep!") #=> "Another: yep!" a.map_another("also yep!") #=> "Another: also yep!" I like this method less because then map_ doesn't show up in documentation and when looking into the object. Also, it seems gragile to me; somebody who knows more will probably correct me ;). -P On 8/2/05, David A. Black wrote: > Hi -- > > On Wed, 3 Aug 2005, Christian Neukirchen wrote: > > > "Trans" writes: > > > >> The point is, can we just settle on one or the other for a future > >> version of Ruby? Although I prefer 'collect', I don't really care > >> which. Just pick one. > > > > I prefer #map because I do more Lisp than Smalltalk. :-) > > I prefer to keep both because I do more Ruby than Lisp or Smalltalk > :-) > > > David > > -- > David A. Black > dblack@wobblini.net >