4

有没有更简洁的方法来做到这一点?

# 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? )
4

1 回答 1

4

是的,可以这样,但对性能不利(请参阅帖子:&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.
于 2013-09-15T19:36:30.233 回答