News for hackers. Stuff that matters.
Re:Github Ruby Styleguide by alanctgardner2 on Wednesday December 12, 2012 @08:15AM (#4909873)
To anyone who's wondering, it seems like 'and' and 'or' are the same as && and ||, but with lower precedence [0]. Does anyone have a good reason why they both exist? It's a pretty big gotcha.

[0] http://stackoverflow.com/questions/1426826/difference-betwee...

Re:Github Ruby Styleguide by philwelch on Wednesday December 12, 2012 @08:34AM (#4909927)
It's an imported Perlism, back from the days of the old assert idiom `foo == bar or die`.

If `foo == bar` evaluates to true, the `or` is short-circuited, else it calls the built-in `die` function which kills the process.

Re:Github Ruby Styleguide by lloeki on Wednesday December 12, 2012 @10:00AM (#4910323)
More precisely && and || are meant to stand in boolean tests, whereas and and or are meant to tie expressions together DSL style, as an alternative to if/then, like so:

   @current_user.logged_in? or redirect_to login_path
   @current_user.can? :do_this or render :status => 403
   shirt.blue? and return bar
   put_suit_on or put_pants_on
Re:Github Ruby Styleguide by graywh on Wednesday December 12, 2012 @12:16PM (#4911229)
That's a good example

  shirt.blue? and return bar
would be equivalent to

  return bar if shirt.blue?
You can do the same with or and unless.
Re:Github Ruby Styleguide by philwelch on Wednesday December 12, 2012 @01:32PM (#4911683)
I find the postfix if and unless far more readable than the Perlish and and or operators. The "or die" construction for asserts is really amusing though.
Re:Github Ruby Styleguide by gurkendoktor on Wednesday December 12, 2012 @08:47AM (#4909972)
&&/|| can bite you just as well:

if first_user = User.find(4) && second_user = User.find(6) ... end

Re:Github Ruby Styleguide by rauljara on Wednesday December 12, 2012 @09:05AM (#4910048)
I may be missing something obvious here. How would this code bite you?
Re:Github Ruby Styleguide by alanctgardner2 on Wednesday December 12, 2012 @09:20AM (#4910104)
> first_user = User.find(4) && second_user = User.find(6)

It looks like it would evaluate as: first_user = (User.find(4) and second_user = User.find(6))

That is to say, the assignment comes after the boolean operation, which is unexpected.

Re:Github Ruby Styleguide by pooriaazimi on Wednesday December 12, 2012 @09:31AM (#4910177)
Well, I expect

    first_user = User.find(4) && second_user = User.find(6)
to be literally equivalent to

    second_user = User.find(6)
    first_user = User.find(4) && second_user
I honestly can't see why you might want it to mean something else...

Edit: I see. if you do something like

    if (first_user = User.find(4) && second_user = User.find(6)) {
      ..
    }
it might bite you. You might expect it to be equivalent to

    first_user = User.find(4)
    second_user = User.find(6)
    if (first_user && second_user) {
      ..
    }
which I personally think is a very, very bad practice. Parenthesis should always be used when there's even the slightest possibility that you or another maintainer/contributor might be confused about.
Re:Github Ruby Styleguide by alanctgardner2 on Wednesday December 12, 2012 @09:45AM (#4910240)
Personally, I'm almost painfully verbose with my parens to avoid exactly this scenario. Better to be explicit about my intent to the interpreter and other coders.

Having an assignment that could be skipped by short-circuiting also seems like bad practice, but I realize it was designed to be a toy example

Re:Github Ruby Styleguide by gurkendoktor on Thursday December 13, 2012 @01:30AM (#4914583)
Skipping the second assignment if I don't need it is more of an optimisation. The "long form" would be,

first_user = User.find(4) if first_user second_user = User.find(6) if second_user ... end end

Re:Github Ruby Styleguide by gurkendoktor on Thursday December 13, 2012 @01:31AM (#4914588)
> Parenthesis should always be used when there's even the slightest possibility that you or another maintainer/contributor might be confused about.

Yes, but you can make the same point in favor of using "and" and "or" (and deprecating &&/||).