4

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? )
Jason T Featheringham
  • 3,305
  • 24
  • 32

1 Answers1

4

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.
Community
  • 1
  • 1
Arup Rakshit
  • 113,563
  • 27
  • 250
  • 306