From: Stefano Crocco Date: 2008-02-05T06:21:21+09:00 Subject: Re: ruby syntax question 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. Stefano