在case
/when
的情况下,是否有任何局部变量可以使用。
例如:
case [1,2,3].each
when ->(n){n.to_a.empty?}
puts "empty"
else
# Get the current Enumerator object here
<this> do |i|
puts "The number is #{i}"
end
end
当您在 else 块中调用 self
时,它会返回main
对象。 唯一与local_variables
一起出现的是[:_]
. 我看到我可以在 proc ->(){}
中交出带有when
的当前项目. 但是我一直无法通过then
、else
,甚至在when
块内访问它。
我不想用case this=[1,2,3].each
分配它. 我特别要求进一步了解在游戏中使用对象的case
/when
。
为了进一步说明这个问题,因为人们误解了调查员。
举个例子:
case "a"
when ->(n){n.next!.equal?("c")}
puts "c"
when ->(n){n.next!.equal?("b")}
puts "b"
when ->(n){n.next!.equal?("a")}
puts "a"
else
puts "The state failed to match up."
# GET ACTUAL VALUE HERE?
end
# => The state failed to match up.
这证明对象正在case
内更改。 它有效。 然而,我没有已知的方法来访问当前值是什么。 这就是我寻求答案的问题。
似乎你需要一个循环,而不是一个开关。