Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中,有没有更简洁的方法来调用映射上的外部方法?_Ruby_Enumerable_Proc - Fatal编程技术网

在Ruby中,有没有更简洁的方法来调用映射上的外部方法?

在Ruby中,有没有更简洁的方法来调用映射上的外部方法?,ruby,enumerable,proc,Ruby,Enumerable,Proc,有没有更简洁的方法 # 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? )

是的,这种方式是可能的,但对性能不好(见帖子:):


你说的“无积分”到底是什么意思这太棒了。你能解释一下这个过程吗(没有双关语)?迷人的!我个人认为原版读起来更好(到目前为止)。但有不止一种方法做事情是好的。@SergioTulentsev在这里看到了——他就是这么做的。非常好的信息。但愿我能不止一次地找到答案:)
targets = ['./foo', './bar', './free']
targets.map(&Dir.method(:exists?))
# => [false, false, false]  #all are false,as I don't have such directories.