Ruby-lambda参数

Ruby-lambda参数,ruby,syntax,Ruby,Syntax,此代码按预期工作(不执行任何操作,甚至不产生警告/错误): 此代码生成警告(警告:块参数的多个值(0表示1)): 此代码失败并出现错误(ArgumentError:参数数目错误(0表示2)): 我认为lambda需要传递所有参数 从第二个例子中,我发现事实并非如此。为什么它在只给出一个参数的情况下工作,而在多个参数的情况下工作(失败并出错) PS:ruby 1.8.6(2008-08-11 patchlevel 287)[universal-darwin9.0] 更新:我已经用ruby 1.9.

此代码按预期工作(不执行任何操作,甚至不产生警告/错误):

此代码生成警告(警告:块参数的多个值(0表示1)):

此代码失败并出现错误(ArgumentError:参数数目错误(0表示2)):

我认为lambda需要传递所有参数

从第二个例子中,我发现事实并非如此。为什么它在只给出一个参数的情况下工作,而在多个参数的情况下工作(失败并出错)

PS:ruby 1.8.6(2008-08-11 patchlevel 287)[universal-darwin9.0]

更新:我已经用ruby 1.9.1p376检查了这些示例。并且它按照预期工作-第二个示例也产生了一个错误。看起来这是1.8版(或版)的一个特性,它将教会您有关Ruby中闭包的所有知识

# So, what's the final verdict on those 7 closure-like entities?          
#
#                                                     "return" returns from closure
#                                    True closure?    or declaring context...?         Arity check?
#                                    ---------------  -----------------------------    -------------------
# 1. block (called with yield)       N                declaring                        no
# 2. block (&b => f(&b) => yield)    N                declaring                        no
# 3. block (&b => b.call)            Y except return  declaring                        warn on too few
# 4. Proc.new                        Y except return  declaring                        warn on too few
# 5. proc                                    <<< alias for lambda in 1.8, Proc.new in 1.9 >>>
# 6. lambda                          Y                closure                          yes, except arity 1
# 7. method                          Y                closure                          yes
#那么,对这7个类似闭包的实体的最终结论是什么?
#
#“返回”从关闭返回
#真闭包?或声明上下文…?算术检查?
#                                    ---------------  -----------------------------    -------------------
#1.区块(以收益率调用)N声明否
#2.块(&b=>f(&b)=>yield)N声明否
#3.阻塞(&b=>b.call)Y,但返回声明警告太少
#4.Proc.new Y,但返回时声明警告太少
#5.proc>
#6.lambda Y闭包是,但arity 1除外
#7.方法Y关闭是

lambda很奇怪,当参数少于两个时,它们的行为会有所不同。请查看详细信息。

当lambda需要参数,而我们没有提供参数,或者我们提供了错误数量的参数时,会引发异常

l = lambda { |name| puts "Today we will practice #{name} meditation." }
l.call
ArgumentError: wrong number of arguments (given 0, expected 1)
我们可以使用arity方法找出预期参数的数量:

l.arity  # Output: => 1
与方法一样,lambda接受以下所有类型的参数/参数:

l.arity  # Output: => 1
  • 位置参数(必需和可选)
  • 单个splat参数(*)
  • 关键字参数(必需和可选)
  • 双splat参数(**)
  • 带符号(&)前缀的显式块参数
以下示例说明了接受多种类型参数的lambda的语法

# Stabby syntax
l = -> (cushion, meditation="kinhin", *room_items, time:, posture: "kekkafuza", **periods, &p) do
  p.call
end

# Regular syntax
l = lambda do |cushion, meditation="kinhin", *room_items, time:, posture:     "kekkafuza", **periods, &p|
  p.call
end

l.call("zafu", "zazen", "zabuton", "incense", time: 40, period1: "morning", period2: "afternoon" ) { puts "Hello from inside the block, which is now a proc." }

Output:
Hello from inside the block, which is now a proc.

Lambdas处理参数的方式与处理方法相同。在

中对上述所有参数/参数类型都有全面的解释,但我在这里找不到答案。您能指出吗?谢谢!但无论如何,仍然不清楚为什么会存在这种行为(正确-存在)@AndersD这种行为的存在是因为没有“ruby规范”很长一段时间以来,很多行为都是偶然出现的。rubinius的创建导致了ruby规范的创建,这导致了此类行为的发现,这种行为违反了最小意外原则。现在有很多工作要做,以确保不需要不同的行为不会不同。HTH。
l.arity  # Output: => 1
# Stabby syntax
l = -> (cushion, meditation="kinhin", *room_items, time:, posture: "kekkafuza", **periods, &p) do
  p.call
end

# Regular syntax
l = lambda do |cushion, meditation="kinhin", *room_items, time:, posture:     "kekkafuza", **periods, &p|
  p.call
end

l.call("zafu", "zazen", "zabuton", "incense", time: 40, period1: "morning", period2: "afternoon" ) { puts "Hello from inside the block, which is now a proc." }

Output:
Hello from inside the block, which is now a proc.