Gavin Sinclair
4/19/2006 6:26:00 AM
Daniel Schierbeck wrote:
> Browsing through the list of method, I couldn't find a method that did
> what I wanted it to, although Enumerable#every is close.
>
> class Array
> def send_to_each(symbol, *args)
> collect{|obj| obj.send(symbol, *args)}
> end
> end
>
> arr = ["cat", "dog", "monkey"]
> arr.send_to_each(:upcase) #=> ["CAT", "DOG", "MONKEY"]
> arr #=> ["cat", "dog", "monkey"]
> arr.send_to_each(:upcase!) #=> ["CAT", "DOG", "MONKEY"]
> arr #=> ["CAT", "DOG", "MONKEY"]
I think the following are better style:
arr = ["cat", "dog", "monkey"]
arr.map { |e| e.upcase }
# -> ["CAT", "DOG", "MONKEY"]
arr
# -> ["cat", "dog", "monkey"]
arr.each { |e| e.upcase! }
# -> ["CAT", "DOG", "MONKEY"]
arr
# -> ["CAT", "DOG", "MONKEY"]
Cheers,
Gavin