> Everyone should use 'and' and 'or' by default instead of && and ||.
> Code reads better that way. =C2=A0Only if you have a specific reason to,
> should you use && or ||.
It's not a good idea to make a blanket rule like that, imo. The "and"
keyword is not really a substitute for "&&", since it has different
precedence. It's best viewed as a control flow modifier (like "if" or
"unless" when at the end of an expression), rather than a true logical
operator.
If you don't know that it's not quite the same, this can get you into
big trouble by leading to subtle bugs. Consider this code, for
instance:
>> missiles_armed =3D true
=3D> true
>> go_for_launch =3D false
=3D> false
# Using &&
>> go_for_launch && missiles_armed ? :fire_ze_missiles : :abort_launch
=3D> :abort_launch # Looks good here.
# Using "and"
>> go_for_launch and missiles_armed ? :fire_ze_missiles : :abort_launch
=3D> false # Uh-oh! We didn't get the `:abort_launch` we were expecting...
~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI:
http://www.linkedin.com/in/johnxf
SO:
http://stackoverflow.com/users/75170/
On Sat, May 7, 2011 at 22:29, 7stud -- <> wrote:
> Hi,
>
> A lot of beginners make the same mistake you did. =C2=A0 'Compound
> conditionals' have to be written like separate conditionals and then
> hooked together with an 'or' or 'and'. =C2=A0For instance if you wanted t=
o do
> something only if a number were greater than 5 and less then 10, you
> would do this:
>
> x > 5
> x < 10
> and
>
> if x > 5 and x < 10
> =C2=A0#do something
> end
>
> Everyone should use 'and' and 'or' by default instead of && and ||.
> Code reads better that way. =C2=A0Only if you have a specific reason to,
> should you use && or ||.
>
> Good luck.
>
> --
> Posted via http://www.ruby-forum.com/.
>
>