有什么方法可以从 ruby case 语句中访问表达式吗?
我想从 then 子句中访问 case 语句表达式,即
food = "cheese"
case food
when "dip" then "carrot sticks"
when "cheese" then "#{expr} crackers"
else
"mayo"
end
在本例中 expr 是食物的当前值。在这种情况下,我知道,我可以简单地访问变量 food,但是在某些情况下,该值可能不再可访问(array.shift 等)。除了将 expr 移出到局部变量中然后访问它之外,是否有其他方法可以直接访问 case expr 的值?
罗哈
附注我知道这个具体的例子是微不足道的,它只是一个示例场景。
I would like to access the case statements expression from within a then clause i.e.
food = "cheese"
case food
when "dip" then "carrot sticks"
when "cheese" then "#{expr} crackers"
else
"mayo"
end
where in this case expr would be the current value of food. In this case I know, I could simply access the variable food, however there may be cases where the value is no longer accessible (array.shift etc..). Other than moving the expr out into a local variable and then accessing it, is there a way of directly accessing the value of the cases expr?
Roja
p.s. I know that this specific example is trivial its mealy an example scenario.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
How about:
这很混乱,但似乎有效......
This is messy but seems like it works...