Precedence of =, and, or

I’m fairly new to Ruby.
I was surprised to read that
a = b or c
will assign b to a, then perform a logical or of a and c.
I expected the precedence of and/or to be higher than = so that it would
perform a logical or of b and c, then assign the result to a.

Do people like it this way?

I’m interested in the rationale behind this decision.

···

WARNING: All e-mail sent to and from this address will be received or
otherwise recorded by the A.G. Edwards corporate e-mail system and is
subject to archival, monitoring or review by, and/or disclosure to,
someone other than the recipient.


In article 89539780CB9BD51182270002A5897DF602C6D375@hqempn04.agedwards.com,
Volkmann, Mark wrote:

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C20E62.08AFAD70
Content-Type: text/plain; charset=“iso-8859-1”

I’m fairly new to Ruby.
I was surprised to read that
a = b or c
will assign b to a, then perform a logical or of a and c.
I expected the precedence of and/or to be higher than = so that it would
perform a logical or of b and c, then assign the result to a.

Do people like it this way?

I’m interested in the rationale behind this decision.

You can use the higher precedence && and || operators e.g.

irb(main):001:0> b, c = 1, 2
[1, 2]
irb(main):002:0> a = b && c
2
irb(main):003:0> a
2
irb(main):004:0> a = b and c
2
irb(main):005:0> a
1

I find that having the choice is useful, and being an old C person && and

seem very natural to me.

As I use Perl a lot as well as Ruby it seems “natural” to me too.

Mike

···


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

“Volkmann, Mark” Mark.Volkmann@AGEDWARDS.com writes:

I’m fairly new to Ruby. I was surprised to read that a = b or c
will assign b to a, then perform a logical or of a and c. I
expected the precedence of and/or to be higher than = so that it
would perform a logical or of b and c, then assign the result to a.

‘or’ and ‘and’ are statement level joiners, while ‘||’ and ‘&&’ are
for expressions. It lets you write

a = b or fail “b is not true”

or even

a = b || c or fail “nothing is true anymore”

Dave

hello -

i’m not just new to ruby, i’m quite new to programming in general (beyond
some massively kludgy shell scripts).

i’ve been rtfming, but haven’t found an answer yet, so i’d be most
appreciative for any gentle nudges in the right direction.

right now i’m working on a little script that will output x number of
iterations of a user-defined statement.

print “what comment should i use?\n”

$enterComment = STDIN.gets

    $enterComment.chop!

print “how many iterations do you want?\n”

$enterNumber = STDIN.gets

    $enterNumber.chop!

$total = $randomComment + $enterComment + $protect

print $total * $enterNumber

the script breaks on this last line, when i attempt to print $total
$enterNumber times. printing $total * 10 yields the expected results;
printing $enterNumber alone yields the expected result, but i can’t get that
"print $total * $enterNumber" line to work.

help?

thanks…

···


yours,
niall.
… . . . . . . . . .
aleph null. a simple insinuation around silence.
see: http://www.vietnambla.com hear: http://radio.vietnambla.com

… .bebox.audio. …
playing now: neil harvey - vs dettinger - sub rosa vs kompakt

niall munnelly wrote:

hello -

i’m not just new to ruby, i’m quite new to programming in general (beyond
some massively kludgy shell scripts).

i’ve been rtfming, but haven’t found an answer yet, so i’d be most
appreciative for any gentle nudges in the right direction.

right now i’m working on a little script that will output x number of
iterations of a user-defined statement.

print “what comment should i use?\n”

$enterComment = STDIN.gets

   $enterComment.chop!

print “how many iterations do you want?\n”

$enterNumber = STDIN.gets

   $enterNumber.chop!

$total = $randomComment + $enterComment + $protect

print $total * $enterNumber

You are using undefined variables ($randomComment, $protect).
In ruby-speak: you are sending the message ‘+’ to nil.
And please get rid of the ugly dollars! This isn’t Perl.

Han

···

It would help if you told us how the script broke. E.g. what error
message did you get?

However, from what you’ve written it looks like you are not converting the
number that the user has input from a string to an integer.

For example:

irb(main):005:0> print “hello” * “4”
TypeError: no implicit conversion from string
from (irb):5:in `*’
from (irb):5
irb(main):001:0> print “hello” * 4
hellohellohellohellonil
irb(main):007:0> print “hello” * “4”.to_i
hellohellohellohellonil

Cheers,
Nat.

···

Dr. Nathaniel Pryce
B13media Ltd.
Studio 3a, Aberdeen Business Centre, 22/24 Highbury Grove, London, N5 2EA
http://www.b13media.com

----- Original Message -----
From: “niall munnelly” aleph@aleph-null.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, June 07, 2002 10:29 PM
Subject: big dumb very newbie question

hello -

i’m not just new to ruby, i’m quite new to programming in general (beyond
some massively kludgy shell scripts).

i’ve been rtfming, but haven’t found an answer yet, so i’d be most
appreciative for any gentle nudges in the right direction.

right now i’m working on a little script that will output x number of
iterations of a user-defined statement.

print “what comment should i use?\n”

$enterComment = STDIN.gets

    $enterComment.chop!

print “how many iterations do you want?\n”

$enterNumber = STDIN.gets

    $enterNumber.chop!

$total = $randomComment + $enterComment + $protect

print $total * $enterNumber

the script breaks on this last line, when i attempt to print $total
$enterNumber times. printing $total * 10 yields the expected results;
printing $enterNumber alone yields the expected result, but i can’t get
that
“print $total * $enterNumber” line to work.

help?

thanks…


yours,
niall.
… . . . . . . . . .
aleph null. a simple insinuation around
silence.
see: http://www.vietnambla.com hear: http://radio.vietnambla.com

… .bebox.audio. …
playing now: neil harvey - vs dettinger - sub rosa vs kompakt

You are using undefined variables ($randomComment, $protect).

no, i just didn’t want to clutter the mail, sorry - like i said, printing the
statement that relies on those variables alone works. it’s the last line,
variable * variable, that breaks.

i wasn’t sure if the comments would be superfluous to the mail; i’ll be sure
to include them in future questions.

In ruby-speak: you are sending the message ‘+’ to nil.
And please get rid of the ugly dollars! This isn’t Perl.

i wouldn’t know the difference.

i have noticed that there are many approaches for them though (puts and print
are interchangeable?) - is there a canonical syntax to which i should refer?

···

On Sat, Jun 08, 2002 at 06:52:35AM +0900, Han Holl wrote:


yours,
niall.
… . . . . . . . . .
aleph null. a simple insinuation around silence.
see: http://www.vietnambla.com hear: http://radio.vietnambla.com

… .bebox.audio. …
playing now: phoenecia - odd job - ramen noodle version by prefuse 73 - odd job discrimination

niall munnelly wrote:

[snip…]
statement that relies on those variables alone works. it’s the last line,
variable * variable, that breaks.

i wasn’t sure if the comments would be superfluous to the mail; i’ll be sure
to include them in future questions.

Sure - it’s often useless to try to figure out why something doesn’t
work if it’s not all here to examine. If something seems too long to
post, the best thing is to try to reproduce the error in a much smaller
script, and post the smaller script. As it turns out, the act of
condensing your work like that is often exactly what it takes to make
you see the answer yourself.

In this case though … 3 is a number and “3” is a string. They’re not
quite the same thing. So this works:

“abc” * 3 (yields the string “abcabcabc”)

But this doesn’t:

“abc” * “3” (produces a “type error”)

Your $enterNumber variable refers to a string, but you can ask Ruby to
treat it as a number by applying the “to integer” method, to_i.

print $total * $enterNumber.to_i

By the way, which of the fm’s are you rtfm’ing? Have you run across the
User’s Guide yet?

Mark

Your $enterNumber variable refers to a string, but you can ask Ruby to
treat it as a number by applying the “to integer” method, to_i.

print $total * $enterNumber.to_i

o, i see. thanks!

By the way, which of the fm’s are you rtfm’ing? Have you run across the
User’s Guide yet?

i was l,ooking at http://www.rubycentral.com/faq/rubyfaq.html and some
google search results.

i just found the user’s guide now:
http://www.ruby-lang.org/~slagell/ruby/rubyguide.html

thanks again.

···


yours,
niall.
… . . . . . . . . .
aleph null. a simple insinuation around silence.
see: http://www.vietnambla.com hear: http://radio.vietnambla.com

… .bebox.audio. …
playing now: arovane - yeer - yeer-disper split 7"