# Ruby syntax question

**URL:** <https://www.ruby-forum.com/t/ruby-syntax-question/128085>\
**Category:** Ruby\
**Created:** [February 4, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/ruby-syntax-question/128085 "2008-02-04T22:05:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 4, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/ruby-syntax-question/128085/1 "2008-02-04T22:05:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 4, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/ruby-syntax-question/128085/2 "2008-02-04T22:21:02Z")

</div>

On Feb 4, 4:01 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 5, 2008, 11:29am UTC](https://www.ruby-forum.com/t/ruby-syntax-question/128085/3 "2008-02-05T11:29:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 4, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/ruby-syntax-question/128085/4 "2008-02-04T22:22:17Z")

</div>

Alle Monday 04 February 2008, removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 5, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/ruby-syntax-question/128085/5 "2008-02-05T13:00:44Z")

</div>

On Feb 4, 10:18 pm, Karl von Laudermann [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > x = def  
> > end
> 
> and assigning to x the result of the call to “def”.

Thanks for help guys.  
Yes, makes sense.

/grz01
