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]
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, 4:01 pm, "gr...@spray.se" <gr...@spray.se> wrote:
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?
Alle Monday 04 February 2008, grz01@spray.se ha scritto:
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
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.
On Feb 4, 2008, at 13:19 , Karl von Laudermann wrote:
On Feb 4, 4:01 pm, "gr...@spray.se" <gr...@spray.se> wrote:
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?
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, 10:18 pm, Karl von Laudermann <doodpa...@mailinator.com> wrote:
On Feb 4, 4:01 pm, "gr...@spray.se" <gr...@spray.se> wrote:
> 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?
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".