调用/应用 lambda 与函数调用 - Ruby 中的语法是不同的。为什么?

发布于 2024-11-15 08:56:34 字数 162 浏览 2 评论 0原文

我对 Ruby 还算陌生,并且仍在尝试了解一些语言设计原则。如果我没猜错的话,那么 Ruby 中的 lambda 表达式调用必须使用方括号,而“常规”函数调用则使用“常规”/圆括号。

语法不同是否有特殊原因?或者,换句话说,(为什么)调用者应该知道他们是调用函数还是应用 lambda 表达式?

I am kinda new to Ruby and still trying to understand some of the language design principles. IF I've got it right, the lambda expression call in Ruby must be with square braces, while the "regular" function call is with "regular"/round braces.

Is there a special reason that the syntax is different? Or, in other words, (why) should the caller be aware whether they call a function or apply a lambda expression?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隱形的亼 2024-11-22 08:56:35

常规 Ruby 方法调用使用 () 而不是用于块的大括号。如果您不喜欢使用 [] 来调用 lambda,则始终可以使用 call 方法。

示例:

>> by_two = lambda { |x| x * 2 } #=> #<Proc:0x0000000101304588@(irb):1>
>> by_two[5] #=> 10
>> by_two.call(5) #=> 10

编辑

在较新版本的 Ruby 中也是如此:

>> by_two.(5) #=> 10

至于为什么不能只执行 by_two(5),当 Ruby 看到裸字时,它首先尝试将其解析为局部变量,如果作为方法失败。

Regular Ruby method calls use () not curly braces which are for blocks. If you don't like [] for calling a lambda, you can always use the call method.

Example:

>> by_two = lambda { |x| x * 2 } #=> #<Proc:0x0000000101304588@(irb):1>
>> by_two[5] #=> 10
>> by_two.call(5) #=> 10

Edit

In newer version of Ruby also:

>> by_two.(5) #=> 10

As to why you can't just do by_two(5), when Ruby sees a bareword it first tries to resolve it as a local variable and if that fails as a method.

把梦留给海 2024-11-22 08:56:35

因为在 Ruby 中,方法不是 lambda(例如在 JavaScript 中)。

方法总是属于对象,可以继承(通过子类化或混合),可以在对象的特征类中覆盖,并且可以被赋予一个块(这是一个 lambda)。它们有自己的变量范围。方法定义示例:

a = :some_variable
def some_method
  # do something, but not possible to access local variable a
end

# call with:
some_method

然而,lambda/procs 是普通的闭包,可能存储在变量中 - 没有别的:

a = :some_variable
some_lambda = lambda{
  # do something, access local variable a if you want to
}

# call with:
some_lambda[]

Ruby 将这两种方法与强大的语法结合在一起,例如传递块:

def some_method_with_block(a)
  # do something, call given block (which is a lambda) with:
  yield(a) ? 42 : 21
end

# example call:
some_method_with_block(1) do |x|
  x.odd?
end #=> 42

Because in Ruby, methods are not lambdas (like, for example, in JavaScript).

Methods always belong to objects, can be inherited (by sub-classing or mixins), can be overwritten in an object's eigenclass and can be given a block (which is a lambda). They have their own scope for variables. Example method definition:

a = :some_variable
def some_method
  # do something, but not possible to access local variable a
end

# call with:
some_method

However lambdas/procs are plain closures, maybe stored in a variable - nothing else:

a = :some_variable
some_lambda = lambda{
  # do something, access local variable a if you want to
}

# call with:
some_lambda[]

Ruby combines both approaches with a powerful syntax, for example, passing blocks:

def some_method_with_block(a)
  # do something, call given block (which is a lambda) with:
  yield(a) ? 42 : 21
end

# example call:
some_method_with_block(1) do |x|
  x.odd?
end #=> 42
薄凉少年不暖心 2024-11-22 08:56:35

如果您想要括号,可以

by_two = lambda { |x| x * 2 }
by_two.(5) # => 10

注意 by_two(5) 之间的 .

If you want brackets, you can do

by_two = lambda { |x| x * 2 }
by_two.(5) # => 10

Note the . between by_two and (5).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文