Live data from Hacker News

Ruby Demystified: and vs. &&

blog.tinfoilsecurity.com

1–10 of 74 posts

Re: Ruby Demystified: and vs. &&

#6
Not to start a language war, but this blog post demonstrates two things Python got right that Ruby got wrong.

First, in Python, there's only one way to express a boolean "and". The && operator is left out.

Second, this post demonstrates a danger of allowing assignment via '=' evaluate to an expression (granted with less weird precedence rules it wouldn't turn out as bad in this case).

Re: Ruby Demystified: and vs. &&

#7
> When I first started learning Ruby, I was excited to discover that Ruby has the keywords and and or as boolean operators. I quickly got into the habit of writing all of my boolean statements with these, instead of the more conventional && and ||, because my code became so much readable

     pry(main)> a = true && false
     
     pry(main)> a = true and false

Hmm...I dunno, I don't think the latter is necessarily easier to read than the former. The use of the symbols helps, for me at least, to delineate between value/variables and operators. Those symbols aren't pretty but their ugliness can serve a purpose.

I don't mind using them in SQL, but only because SQL is case-insensitive and I can stick to the style of making all syntax uppercase.

Re: Ruby Demystified: and vs. &&

#8
This is basically the author finding out something in Ruby that was copied from Perl. E.g. it allows one to do:

  $fh = open() or die("File did not open")
Instead of:

  $fh = open()
  die("File did not open") unless $fh

Re: Ruby Demystified: and vs. &&

#9
Makes for an interesting blog post, I guess. But precedence for "&&" and "||" in Ruby are similar to precedence for "&&" and "||" in C, Java and others, and they are similar to precedence rules for "and" and "or" in Python. Precedence rules for "and" and "or" in Ruby are an anomaly not just in the Ruby world but in many (most?) mainstream languages.
Post reply on HN