ruby syntax question

G

grz01

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
 
K

Karl von Laudermann

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".
 
S

Stefano Crocco

Alle Monday 04 February 2008, (e-mail address removed) 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.

Stefano
 
R

Ryan Davis

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".


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)))))
 
G

grz01

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".


Thanks for help guys.
Yes, makes sense.

/grz01
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top