为什么在圆括号前面没有点的情况下无法进行Ruby 1.9 lambda调用? [英] Why isn't the Ruby 1.9 lambda call possible without the dot in front of the parentheses ?

查看:61
本文介绍了为什么在圆括号前面没有点的情况下无法进行Ruby 1.9 lambda调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了最新的Ruby版本,以了解最新的变化.我试图做的第一件事就是调用Ruby lambda/block/proc,就像处理Python可调用项一样.

I checked out the latest Ruby version, to play a bit with the latest changes. The first thing I tried to do was call a Ruby lambda/block/proc just like you'd do with a Python callable.

a = lambda {|x| puts x}
a.call(4) # works, and prints 4
a[4] # works and prints 4
a.(4) # same
a(4) # undefined method 'a' for main:Object

为什么不能拨打最后一个电话?会吗?

Why isn't the last call possible? Will it ever be?

推荐答案

AFAIK这是因为ruby不允许您为对象定义()方法.不允许您定义()方法的原因可能是由于括号在方法调用中是可选的.

AFAIK it's because ruby doesn't let you define the () method for an object. The reason it doesn't let you define the () method is probably due to the fact that parentheses are optional in method calls.

对于它的价值,这里有个黑客让您使用() http://github.com/coderrr/parenthesis_hacks/blob/master/lib/lambda.rb

And for what it's worth, here's a hack to let you invoke lambdas using () http://github.com/coderrr/parenthesis_hacks/blob/master/lib/lambda.rb

这篇关于为什么在圆括号前面没有点的情况下无法进行Ruby 1.9 lambda调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆