DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Using && Instead Of And
Source: <a href="http://www.themomorohoax.com/2008/12/08/and-vs-amperand-in-ruby">and vs && (double ampersand) in Ruby - Momoro Machine</a> [themomorohoax.com] via <a href="http://twitter.com/AkitaOnRails/status/16959633756">Twitter / Fabio Akita: RT @igrigorik: just learne ...</a> [twitter.com]
<snip>
Many people seem to prefer using and instead of && in Ruby, as it sounds more like speech.
However, do note that it’s slightly different from using &&. The difference is important enough that I think you should avoid using and.
alien = true speaks_english = false # 1 alien and speaks_english ? 'hello' : '**silence**' #=> "**silence**" # 2 alien && speaks_english ? 'hello' : '**silence**' #=> "**silence**" # 3 speaks_english and alien ? 'hello' : '**silence**' #=> false # oops # 4 speaks_english && alien ? 'hello' : '**silence**' #=> "**silence**"
</snip> Interestingly enough ...
alien = true speaks_english = false # 1 if alien and speaks_english then 'hello' else '**silence**' end #=> "**silence**" # 2 if alien && speaks_english then 'hello' else '**silence**' end #=> "**silence**" # 3 if speaks_english and alien then 'hello' else '**silence**' end #=> "**silence**" # 4 if speaks_english && alien then 'hello' else '**silence**' end #=> "**silence**"






Comments
Alexey Tarasevich replied on Wed, 2010/01/06 - 7:52pm