Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Assignment Syntax

A silly, syntactical question:

If the assignment operator is really a function, like

def value=(x)
  @value = x
end

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?

like image 575
inyourcorner Avatar asked Feb 10 '11 22:02

inyourcorner


Video Answer


1 Answers

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.

like image 70
cam Avatar answered Oct 09 '22 09:10

cam