Call a method on each object of a collection
def append_2s(word)
word + "ss"
end
['test', 'ab', 'ss'].map(&method(:append_2s)) #=> ['testss', 'abss', 'ssss']
Written by Jean-Richard Lai
Related protips
2 Responses
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/40509/44e500d45df133c3e941d1293c3e224c.png)
Why don't use each?
['test', 'ab', 'ss'].each do {|a| append_2s(a) }
over 1 year ago
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/6522/12003957_10206752435102297_2524621803006121539_n.jpg)
@emadb I think it is a little more elegant. But you can do it in even less chars with:
#removes whitespaces from items
['test ', ' ab', ' ss'].collect(&:strip)
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#