Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Order of operations

Reply
Thread Tools

Order of operations

 
 
Jack Christensen
Guest
Posts: n/a
 
      06-09-2005
Just got bit by a little order of operations confusion.

irb(main):012:0> a = nil or 1
=> 1
irb(main):013:0> a
=> nil
irb(main):014:0> a = (nil or 1)
=> 1
irb(main):015:0> a
=> 1

Probably old news to long time rubyists but it surprised me. Is there
any way to get a warning or something when a statement like the above
may not be behaving like you would think?

Jack


 
Reply With Quote
 
 
 
 
Devin Mullins
Guest
Posts: n/a
 
      06-09-2005
I doubt this is the answer you're looking for, but if you use || instead
of or, the precedence is different. i.e. a = nil || 1 does what you
think it does.

Devin

Jack Christensen wrote:

> Just got bit by a little order of operations confusion.
>
> irb(main):012:0> a = nil or 1
> => 1
> irb(main):013:0> a
> => nil
> irb(main):014:0> a = (nil or 1)
> => 1
> irb(main):015:0> a
> => 1
>
> Probably old news to long time rubyists but it surprised me. Is there
> any way to get a warning or something when a statement like the above
> may not be behaving like you would think?
>
> Jack
>
>




 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      06-09-2005
Jack Christensen wrote:
> Just got bit by a little order of operations confusion.
>
> irb(main):012:0> a = nil or 1
> => 1
> irb(main):013:0> a
> => nil
> irb(main):014:0> a = (nil or 1)
> => 1
> irb(main):015:0> a
> => 1


But note:

>> a = nil || 1

=> 1
>> a

=> 1
>>

> Probably old news to long time rubyists but it surprised me. Is there
> any way to get a warning or something when a statement like the above
> may not be behaving like you would think?


No, because Ruby cannot know what you expect. But note that there *are*
warnings:

>> if ( a = 10 )
>> "ja"
>> end

(irb):3: warning: found = in conditional, should be ==
=> "ja"
>>


"or" and "and" have much lower precedence than "||" and "&&" to give you
the choice and for example do things like

arr.empty? and puts "it's empty!"
a > 0 and puts "positive"

Kind regards

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Dealing with higher order operations coupled with primitives Stefan Ram Java 0 06-22-2012 03:43 AM
Re: Dealing with higher order operations coupled with primitives Gina Engli Java 0 06-22-2012 03:17 AM
order of operations pauldepstein@att.net C++ 2 07-27-2008 08:40 PM
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM
order of operations with division and multiplication Tim923 C++ 4 04-08-2005 09:01 PM



Advertisments