Hi,
While debugging some ruby-code, I found a mis-typed construct like the
following,
which looks like a syntax error to me,
but when you run it in irb it evaluates to nil and does NOT raise an
error:
x = def
puts xyz
end
(where xyz is undefined)
Anyone can explain what the above code means to ruby?
In contrast, this code DOES raise a syntax error:
x = def
puts “xyz”
end
My versions is:
irb 0.9.5(05/04/13)
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
TIA,
--------------------------- grz01
On Feb 4, 4:01 pm, “[email protected]” [email protected] wrote:
end
(where xyz is undefined)
Anyone can explain what the above code means to ruby?
I believe that it is being interpreted as defining an empty method
like so:
def puts(xyz)
end
and assigning to x the result of the call to “def”.
On Feb 4, 2008, at 13:19 , Karl von Laudermann wrote:
x = def
end
and assigning to x the result of the call to “def”.
Yup:
504 % echo "x = def
puts xyz
end
" | parse_tree_show
-:2: warning: void value expression
s(:lasgn, :x, s(:defn, :puts, s(:scope, s(:block, s(:args, :xyz),
s(:nil)))))
Alle Monday 04 February 2008, [email protected] ha scritto:
end
My versions is:
irb 0.9.5(05/04/13)
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
TIA,
--------------------------- grz01
I’m not sure, but I think ruby, seeing a newline after a def, expects
the
statement to go on on the following line, just as when you write
1+2+
3
or
my_method(a, b,
c)
The first piece of code would be translated into
x = def puts xyz
end
that is to the definition of a method called puts which takes one
parameter,
named xyz (the x= part plays no role in this matter).
In the second case, ruby tries to do the same, but finds a string where
it
would expect the argument name, so throws an exception.
Stefano
On Feb 4, 10:18 pm, Karl von Laudermann [email protected]
wrote:
x = def
end
and assigning to x the result of the call to “def”.
Thanks for help guys.
Yes, makes sense.
/grz01