有没有更简洁的方法在Ruby中调用外部方法?

这样做有更简洁的方法吗?

# 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. 
Interesting Posts