有没有更简洁的方法来做到这一点?
# Given a directory containing subdirectories: foo, bar
targets = ['./foo', './bar', './free']
targets.map{ |d| Dir.exists? d }
# => [ true, true, false ]
我希望能够做一些类似于 proc 调用的事情......感觉更干净:
# targets.map( Dir.exists? )
有没有更简洁的方法来做到这一点?
# Given a directory containing subdirectories: foo, bar
targets = ['./foo', './bar', './free']
targets.map{ |d| Dir.exists? d }
# => [ true, true, false ]
我希望能够做一些类似于 proc 调用的事情......感觉更干净:
# targets.map( Dir.exists? )
是的,可以这样,但对性能不利(请参阅帖子:&method(:method_name) 成语是否对 Ruby 中的性能不利?):
targets = ['./foo', './bar', './free']
targets.map(&Dir.method(:exists?))
# => [false, false, false] #all are false,as I don't have such directories.