Expression results

Hello,

The expressions

e1 = 1 + 2;
e2 = 1 +
2;
e3 = 1
+ 2;
e4 = (1 + 2);
e5 = (1 +
2);
e6 = (1
+ 2);
p e1, e2, e3, e4, e5, e6

give different results, depending on the formatting. What’s the
explanation for this?

Version: Ruby173-7 (Windows installer) on a Windows 2000 box.

Rasmus

Ruby termninates a statement when an
expression is complete OR end of line OR
semicolon. (Semicolons are normally
optional.)

The expressions

e1 = 1 + 2;

3

e2 = 1 +
2;

3

e3 = 1

1

  • 2;

2 (thrown away)

e4 = (1 + 2);

3

e5 = (1 +
2);

3

e6 = (1

  • 2);

3

give different results, depending on the formatting. What’s the
explanation for this?

Did I get these right? Does it make more sense
now? Since Ruby is expression-oriented, “+ 2”
is a valid standalone expression.

HTH,
Hal

···

----- Original Message -----
From: “Rasmus” debitsch@t-online.de
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, March 21, 2003 4:05 PM
Subject: Expression results

Hello,

The expressions

e1 = 1 + 2;
e2 = 1 +
2;
e3 = 1
The above is a complete expression, so e3 = 1

  • 2;
    This is also a complete expression
    e4 = (1 + 2);
    e5 = (1 +
    2);
    e6 = (1
  • 2);
    I don’t know why this is 2.

p e1, e2, e3, e4, e5, e6

give different results, depending on the formatting. What’s the
explanation for this?

Version: Ruby173-7 (Windows installer) on a Windows 2000 box.

I using 1.8.0

···

On Saturday, 22 March 2003 at 7:05:57 +0900, Rasmus wrote:


Jim Freeze

Oliver’s Law:
Experience is something you don’t get until just after you need
it.

I agree, this is very odd.

What’s going on?

Hal

···

----- Original Message -----
From: “Jim Freeze” jim@freeze.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, March 21, 2003 4:20 PM
Subject: Re: Expression results

e6 = (1

  • 2);
    I don’t know why this is 2.

Since when is 2 odd? Prime, yes, but odd?

(Sorry, couldn’t resist.)

···

— “Hal E. Fulton” hal9000@hypermetrics.com wrote:

e6 = (1

  • 2);
    I don’t know why this is 2.

I agree, this is very odd.

What’s going on?

=====

We have got to force them to comply, and we are doing so militarily." – Tom Daschle – February, 1998 (Clinton presidency)

"I am saddened that … we’re now forced to go to war.” – Tom Daschle, March, 2003 (Bush presidency)


Do you Yahoo!?
Yahoo! Platinum - Watch CBS’ NCAA March Madness, live on your desktop!
http://platinum.yahoo.com

I agree, this is very odd.

What’s going on?

···

----- Original Message -----
From: “Hal E. Fulton” hal9000@hypermetrics.com

e6 = (1

  • 2);
    I don’t know why this is 2.

I think that the expression in parenthases is read as:

(1; +2)

which evaluates to 2.

It is odd though… “no sir, I don’t like it.”

Chris

Here’s my guess.

Compare with the following:

batsman@tux-chan:/tmp$ expand -t2 r.rb
e = (
a=1
b=2
if a > b
“foo”
else
“bar”
end)
puts e

b =(1
2
3)

puts b

batsman@tux-chan:/tmp$ ruby r.rb
r.rb:11: warning: useless use of a literal in void context
r.rb:12: warning: useless use of a literal in void context
bar
3

I believe that
(1

  • 2;)
    is seen as
    (
    1 # first ‘statement’
    +2; # second ‘statement’
    ) # value of this expression == that returned by the last statement

This looks somewhat surprising as one would like to think that
parentheses ensure Ruby sees the statement is not finished, but that
would break the usage above.

Conclusion: parentheses don’t ensure that everything inside them is
evaluated as a single expression (statement).

···

On Sat, Mar 22, 2003 at 07:39:02AM +0900, Hal E. Fulton wrote:

----- Original Message -----
From: “Jim Freeze” jim@freeze.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, March 21, 2003 4:20 PM
Subject: Re: Expression results

e6 = (1

  • 2);
    I don’t know why this is 2.

I agree, this is very odd.

What’s going on?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

     Why use Windows when you can have air conditioning?
     Why use Windows, when you can leave through the door?
-- Konrad Blum

Seems to be same as: e6 = (1; +2)

def f1
puts “in f1”
99
end
def f2
puts “in f2”
123
end
a = (f1

  • f2)
    puts a

Produces:

in f1
in f2
123

and is the same if you do

a = (f1
f2)

Regards,

Brian.

···

On Sat, Mar 22, 2003 at 07:39:02AM +0900, Hal E. Fulton wrote:

e6 = (1

  • 2);
    I don’t know why this is 2.

I agree, this is very odd.

What’s going on?

[snip]

You all seem to have analyzed this correctly.

For now, the bottom line is: JUST DON’T
FORMAT THAT WAY.

Hal

···

----- Original Message -----
From: “Brian Candler” B.Candler@pobox.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, March 21, 2003 5:10 PM
Subject: Re: Expression results

On Sat, Mar 22, 2003 at 07:39:02AM +0900, Hal E. Fulton wrote:

e6 = (1

  • 2);
    I don’t know why this is 2.

I agree, this is very odd.

What’s going on?

Seems to be same as: e6 = (1; +2)

In article 20030321231000.GA65019@uk.tiscali.com,
Brian Candler B.Candler@pobox.com writes:

Seems to be same as: e6 = (1; +2)

The position before ;' is void context. And 1’ is a literal which has clearly no side effect.

Why Ruby doesn’t warn it as toplevel?

% ruby -w -e ‘1’
-e:1: warning: useless use of a literal in void context

···


Tanaka Akira

“Michael Campbell” michael_s_campbell@yahoo.com schrieb im Newsbeitrag
news:20030321224454.5114.qmail@web12408.mail.yahoo.com

I don’t know why this is 2.

I agree, this is very odd.

What’s going on?

Since when is 2 odd? Prime, yes, but odd?

It’s not just plain odd it’s very odd. That’s a big difference - even
with small numbers. :-))

(Sorry, couldn’t resist.)

me 2 :slight_smile:

robert

“Michael Campbell” michael_s_campbell@yahoo.com

Since when is 2 odd? Prime, yes, but odd?

Well, if you have 2 heads it is kinda odd :wink:

(Sorry, couldn’t resist.)

Even I couldn’t !

Scripsit ille »Michael Campbell« michael_s_campbell@yahoo.com:

e6 = (1

  • 2);

Let me guess…

it’s parsed as:

e6 =
(
1;
+ 2;
);

which evaluates 1, then + 2 and returns the last value - +2.

Hey… that is, structure blocks (not meaning anything more) can be done
like this:

(
something
something else
)

or if one really wants code to look like Perl:

/some_regex/ and ((

)) or
/some_regex/ and ((

)) or
((

))

(the ((parens)) are doubled to inhibit the “assignment in conditional”
warning which is useless if you use ((…)) as replacement for Perl’s
do { … } construct)

And no, I don’t think that construct is really useful. But it allows
some sort of fall-through by letting the block return false or nil.

It looks like the rule is: if there may be a semicolon at the end of
the line, it is parsed as if there was one.

Am I right?

I don’t know why this is 2.

I agree, this is very odd.

What’s going on?

Since when is 2 odd? Prime, yes, but odd?

2: The Odd Prime –
It’s the only even prime, therefore is odd. QED.

but of that fortune cookie, I like best:

31: The Arbitrary Prime –
Determined by unanimous unvote. We needed an arbitrary prime in
case the prof asked for one, and so had an election. 91 received
the most votes (well, it looks prime) and 3+4i the next most.
However, 31 was the only candidate to receive none at all.

Try to find all mistakes. I see two nonobvious mistakes in there…

BTW: 91 is the second composite number which looks prime. 1 is the first
one, of course.

···

— “Hal E. Fulton” hal9000@hypermetrics.com wrote:


What isn’t remembered, never happened.
Memory is merely a record.
You just need to rewrite that record.

It does for me (under 1.6.8):

$ cat x.rb
#!/usr/local/bin/ruby -w
e6 = (1
+2)
puts e6
$ ruby x.rb
x.rb:2: warning: useless use of a literal in void context
2

and the same for (1; +2)

···

On Sat, Mar 22, 2003 at 07:39:51PM +0900, Tanaka Akira wrote:

In article 20030321231000.GA65019@uk.tiscali.com,
Brian Candler B.Candler@pobox.com writes:

Seems to be same as: e6 = (1; +2)

The position before ;' is void context. And 1’ is a literal which has clearly no side effect.

Why Ruby doesn’t warn it as toplevel?

“Michael Campbell” michael_s_campbell@yahoo.com schrieb im Newsbeitrag
news:20030321224454.5114.qmail@web12408.mail.yahoo.com

I don’t know why this is 2.

I agree, this is very odd.

What’s going on?

Since when is 2 odd? Prime, yes, but odd?

It’s not just plain odd it’s very odd. That’s a big difference - even
with small numbers. :-))

Small numbers are even, yet 2 is very odd? Does that make 2 a large number?

(Sorry, couldn’t resist.)

me 2 :slight_smile:

robert

me 3, which is certainly odd :slight_smile:

···

On Sat, 22 Mar 2003 21:27:49 +0900 “Robert” bob.news@gmx.net wrote:


To call me “awesome” is an understatement.

I don’t know what you actually call 1, but it’s not composite (doesn’t
have prime factors).

What are the nonobvious mistakes in there?

Gavin

···

On Sunday, March 23, 2003, 10:50:50 AM, Rudolf wrote:

31: The Arbitrary Prime –
Determined by unanimous unvote. We needed an arbitrary prime in
case the prof asked for one, and so had an election. 91 received
the most votes (well, it looks prime) and 3+4i the next most.
However, 31 was the only candidate to receive none at all.

Try to find all mistakes. I see two nonobvious mistakes in there…

BTW: 91 is the second composite number which looks prime. 1 is the first
one, of course.

In article 20030322104441.GA65744@uk.tiscali.com,
Brian Candler B.Candler@pobox.com writes:

It does for me (under 1.6.8):

1.8.0 doesn’t.

% ruby -v -e ‘x = (1; 2)’
ruby 1.8.0 (2003-03-21) [i686-linux]

Bug?

···


Tanaka Akira

“Michael Campbell” michael_s_campbell@yahoo.com schrieb im Newsbeitrag
news:20030321224454.5114.qmail@web12408.mail.yahoo.com

I don’t know why this is 2.

I agree, this is very odd.

What’s going on?

Since when is 2 odd? Prime, yes, but odd?

It’s not just plain odd it’s very odd. That’s a big difference - even
with small numbers. :-))

Small numbers are even, yet 2 is very odd? Does that make 2 a large number?

For sufficiently large values of 2, yes. =)**2.

Scripsit ille aut illa »Gavin Sinclair« gsinclair@soyabean.com.au:

31: The Arbitrary Prime –
Determined by unanimous unvote. We needed an arbitrary prime in
case the prof asked for one, and so had an election. 91 received
the most votes (well, it looks prime) and 3+4i the next most.
However, 31 was the only candidate to receive none at all.

Try to find all mistakes. I see two nonobvious mistakes in there…

BTW: 91 is the second composite number which looks prime. 1 is the first
one, of course.

I don’t know what you actually call 1, but it’s not composite (doesn’t
have prime factors).

Sorry, I meant nonprime… well, for some reason, 51 and 57 are sometimes
mistaken as primes. But 91 is the first one only divisible by nonobvious
divisors in the base-10 system: 7 and 13.

What are the nonobvious mistakes in there?

  • 3+4i isn’t prime, it’s (2+i)**2.

  • 31 was the ONLY candidate to receive none at all => there were
    infinitely many votes of a finite number of students. If you need
    an ARBITRARY prime, of course EVERY prime is a candidate.

···

On Sunday, March 23, 2003, 10:50:50 AM, Rudolf wrote:


Desweiteren fiel mir auf, das die Kommandozeile immer statt ‘?’ ein
Fragezeichen generierte.
[Da_David in t-online.homepage.technik; OE hat mal wieder versagt]

% ruby -v -e 'x = (1; 2)'
ruby 1.8.0 (2003-03-21) [i686-linux]

The node is removed before ruby make the test.
1.6 create a block, 1.8 can simplify the block.

Guy Decoux