Ruby赋值语法 [英] Ruby Assignment Syntax
问题描述
一个愚蠢的句法问题:
如果赋值运算符确实是一个函数,例如
If the assignment operator is really a function, like
def value=(x)
@value = x
end
在左操作数和"="之间没有空格,那么为什么可以将赋值指定为test.value = x(带空格),但是方法定义不能写为:
without a space between the left-hand operand and the "=", then why can the assignment be made as test.value = x (with a space), but the method definition cannot be written as:
def value = (x)
@value = x
end
带有空格.这仅仅是语法分析器所规定的语法吗?
with the space. Is this simply syntax dictated by the parser?
推荐答案
def
后面必须是函数名称的标记,可以选择后面是参数列表.参数列表上的括号是可选的(例如,def value= x
是适当的定义). def value = (x)
看起来像def
,后跟两个标记,然后是一个不解析的参数列表.
def
needs to be followed by a token for the function name, optionally followed by an argument list. The parenthesis on the argument list is optional (e.g., def value= x
is an appropriate definition). def value = (x)
looks like def
followed by two tokens and then an argument list, which does not parse.
这篇关于Ruby赋值语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!