Lyle Johnson
10/28/2003 1:26:00 PM
Karl von Laudermann wrote:
> 2) In The Ruby Way, it talks about the importance of using whitespace
> consistently, and gives the following examples:
>
> x = y + z
> x = y+z
> x = y+ z
> x = y +z
>
> Regarding the last form, the text says, "...the parser thinks that y is
> a method call and +z is a parameter passed to it! It will then give an
> error message for that line if there is no method named y."
>
> My question is, what if there is a method named y, which takes one
> parameter?
The definition for such a method would look something like this:
def y(value)
2*value
end
> Is there a way that this line can be executed successfully,
> without an error message?
Yes; as suggested by the text that you quoted from The Ruby Way, if you
first define the "y" method, and then try to execute that line, you
shouldn't get an error message:
def y(value)
2*value
end
z = 4
x = y +z (now x = y(+z) = 2*(+4) = 8)
> Can +z ever be a meaningful and correct argument in a method call?
> Variable names can't begin with a "+" character, correct?
The "+" character isn't modifying the variable name (i.e. the variable
name is still "z"). This is what is known as the "unary plus" operator,
which practically speaking just multiplies the following value by
positive one (ho hum). His more outgoing brother is the "unary minus"
operator, which multiplies the following value by negative one and thus
flips its sign, which is usually the more interesting case.