Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorthand for passing each item in an enumerable to another method [duplicate]

Is there a more concise way of doing this?

# Given a directory containing subdirectories: foo, bar
targets = ['./foo', './bar', './free']
targets.map{ |d| Dir.exists? d }
# => [ true, true, false ]

I'd love to be able to do something similar to proc calls... it feels cleaner:

# targets.map( Dir.exists? )
like image 802
Jason T Featheringham Avatar asked Jan 25 '26 06:01

Jason T Featheringham


1 Answers

Yes, possible this way, but not good for performance (see post: Is the &method(:method_name) idiom bad for perfomance in Ruby?):

targets = ['./foo', './bar', './free']
targets.map(&Dir.method(:exists?))
# => [false, false, false]  #all are false,as I don't have such directories.
like image 157
Arup Rakshit Avatar answered Jan 26 '26 21:01

Arup Rakshit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!