The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Ruby's Operator Precedence: More subtle than you might think

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Florian Frank

Posts: 48
Nickname: ffrank
Registered: Dec, 2005

Florian Frank is a humanoid life-form, living on the third planet of the solar system.
Ruby's Operator Precedence: More subtle than you might think Posted: Aug 8, 2007 6:07 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Florian Frank.
Original Post: Ruby's Operator Precedence: More subtle than you might think
Feed Title: The Rubylution: Tag Ruby
Feed URL: http://rubylution.ping.de/xml/rss/tag/ruby/feed.xml
Feed Description: Starts… Now.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Florian Frank
Latest Posts From The Rubylution: Tag Ruby

Advertisement

I just read Rick DeNatale's article about Ruby's Operator Precedence, in which he mentioned, that Ruby's and and or operators have a weaker precedence than && and || (and also weaker than =). This is similar to Perl's and and or operators, and the reason why I use the following rules of thumb for the use of the different operators in both languages:

  1. Use && and || for expressions (especially more complex ones) like a = a && b || c.
  2. Use and and or instead of if enabled then do_it end or do_it if enabled as in enabled and do_it.
  3. Or use and and or for very simple expressions as in if a and b then ... end.

Rule 3 exists only, because I think it is slightly more readable to use and or or than && and ||. This advantage goes pretty much away if you have to start using parentheses to render more complex expressions unambiguous. It could as well be dropped.

But in Ruby other than in Perl there is another reason for rule 3:

Ruby's and and or operators have the SAME precedence!

Yeah, you read this right: So, for example, true || true && false # => true, but true or true and false # => false.

This is a rather surprising (as in unfortunate) peculiarity of Ruby, and I do not think, that it is a good idea. I guess most people are unaware of this issue, and their code does only work by accident - until it stops doing so for unknown reasons. Matz' rationalisation of this behaviour (somewhere on ruby-talk) was "'and' and 'or' don't have different precedence in English either." It didn't convince me then and doesn't convince me now: We don't program in English, because it is too unprecise. Why burden Ruby with the same problem as well?

Read: Ruby's Operator Precedence: More subtle than you might think

Topic: Se buscan usuarios y programadores para PingBlog Previous Topic   Next Topic Topic: Visual Ruby On Rails - News Digest, August 2007

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use