Ruby 中的冒号是什么?它不是运营商
What is the colon classified as in Ruby? It's not an operator
在将此标记为“What is the colon operator in Ruby?”的副本之前,请阅读问题(只是先发制人)。我不是在问冒号的实际作用。
我最近试图想出一个聪明的解决方案来解决“". My first thought was to overload the colon operator for the String class, just like Ruby does for the +
method on String (I'm aware of the potential downsides of doing that). But I discovered that the :
is not simply an overloadable operator of String。
"ruby operators" 的 first result on Google 不显示冒号作为运算符。而且我找不到任何声称它是运算符的来源。我想它会与 hashrocket (=>
) 属于同一类别,但我不知道你会怎么称呼它们。
那么这个特殊的冒号字符被归类为什么呢?你能指出它在 Ruby 来源中的定义吗?
程序解析分两步进行:
- 将字符序列转换为标记序列,并且
- 将标记序列变成语法树。
运算符、简单对象字面量、关键字(如begin
)、=>
都是记号,在第2步处理。记号在Ruby C代码中有内部名称.
但是,冒号作为方法中命名参数的一部分或散列中的符号键在步骤 1 中处理,不是标记;它没有特定的名称。
在将此标记为“What is the colon operator in Ruby?”的副本之前,请阅读问题(只是先发制人)。我不是在问冒号的实际作用。
我最近试图想出一个聪明的解决方案来解决“+
method on String (I'm aware of the potential downsides of doing that). But I discovered that the :
is not simply an overloadable operator of String。
"ruby operators" 的 first result on Google 不显示冒号作为运算符。而且我找不到任何声称它是运算符的来源。我想它会与 hashrocket (=>
) 属于同一类别,但我不知道你会怎么称呼它们。
那么这个特殊的冒号字符被归类为什么呢?你能指出它在 Ruby 来源中的定义吗?
程序解析分两步进行:
- 将字符序列转换为标记序列,并且
- 将标记序列变成语法树。
运算符、简单对象字面量、关键字(如begin
)、=>
都是记号,在第2步处理。记号在Ruby C代码中有内部名称.
但是,冒号作为方法中命名参数的一部分或散列中的符号键在步骤 1 中处理,不是标记;它没有特定的名称。