Symbol#to_proc 与 stabby lambda 语法的简写 [英] Symbol#to_proc shorthand with the stabby lambda syntax
问题描述
非常简短的问题:是否可以在 Ruby 中使用 Symbol#to_proc 速记(例如 lambda(&:upcase)
和 stabby lambda 语法?例如,我可以这样说:
Pretty short question: Is it possible to use the Symbol#to_proc shorthand (e.g. lambda(&:upcase)
with the stabby lambda syntax in Ruby? For example, I can say this:
p = lambda &:upcase
在 p
中获得 Proc
但我找不到使用 ->
的等效项.这:
to get a Proc
in p
but I can't find an equivalent using ->
. This:
p = -> &:upcase
当然不行.
推荐答案
显然不支持.
我认为这与 proc
和 lambda
实际上是方法,而不是关键字.它们支持我们通常与 each
相关联的相同功能以及来自 Enumerable
模块.但是,->
是一个特殊的单独解析的语言结构.
I think it has something to do with the fact that proc
and lambda
are actually methods, and not keywords. They support the same features that we usually associate with each
and the other methods from the Enumerable
module. However, ->
is a special language construct which is parsed separately.
我想不出为什么像->这样的东西&:method
应该是不可能的,但到目前为止,Ruby 语言的语法根本不允许这样做.
I can't think of a reason why something like -> &:method
shouldn't be possible, but as of now the syntax of the Ruby language simply doesn't allow it.
这篇关于Symbol#to_proc 与 stabby lambda 语法的简写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!