有更简洁的方法吗?
# 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? )
答案 0 :(得分:4)
是的,可能这样,但对性能不利(见帖子: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.