Empty set of parens, as expression

Z

Zsban Ambrus

It seemed to me that in ruby an empty set of parens just means nil.
For example

n=1;n=();p([(),5,2,n,(),7])

prints

[nil, 5, 2, nil, nil, 7]

However, when I write them as the last statement in a method or a begin
(with or without a semicolon after it, it seems to do nothing. For example

def set; a= 2; (); end; puts set();

prints 2.

It seems that (()) and ((())) work the same way as ().
(As an expression I mean, not in 'p()' and 'p(())' which are of course
different.)

(All examples are with ruby 1.8.1 (2003-12-25) [i686-linux])

I think that this behaviour might stem from that you can use a parenthisized
list of commands (separated with semicolons) as an expression just like
a begin-end block. (This is a Good Thing^{TM} IMO.) Thus, '()' is just an
empty block.

However, if this is the case I don't understand why its return value is
ignored at the end of a block:

p((2; ();))

prints 2, but

p((2; begin end;))

prints nil. (Again, the semicolon after '()' or 'begin end' changes nothing.

Is this difference between () and begin...end intended or accidental, and
might it change in the future?

Thanks for any comments,

ambrus
 
Y

Yukihiro Matsumoto

Hi,

In message "Empty set of parens, as expression"

|It seemed to me that in ruby an empty set of parens just means nil.
|For example
|
|n=1;n=();p([(),5,2,n,(),7])
|
|prints
|
|[nil, 5, 2, nil, nil, 7]
|
|However, when I write them as the last statement in a method or a begin
|(with or without a semicolon after it, it seems to do nothing. For example
|
|def set; a= 2; (); end; puts set();
|
|prints 2.

Point taken. It will be fixed; "set" will return nil.

matz.
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top