3
\$\begingroup\$

In ruby, is there a more concise way of expressing multiple AND conditions in an if statement?

For example this is the code I have:

    if first_name.blank? and last_name.blank? and email.blank? and phone.blank?
      #do something
    end

Is there a better way to express this in Ruby? 1.9.2 or 1.9.3

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$
if [first_name, last_name, email, phone].all?(&:blank?)
  #do something
end

Caveat: While and/&& short-circuit the expression (that's it, only the needed operands are evaluated), an array evaluates all its items in advance. In your case it does not seem something to worry about (they seem cheap attributes to get), but if you ever need lazy evaluation and still want to use this approach, it's possible using procs, just slightly more verbose:

p = proc
if [p{first_name}, p{last_name}, p{email}, p{phone}].all? { |p| p.call.blank? }
  #do something
end
\$\endgroup\$
5
  • \$\begingroup\$ I received an undefined method 'all' on the code example above. See repl.it/D7X \$\endgroup\$
    – koa
    Commented Nov 5, 2012 at 7:50
  • \$\begingroup\$ @koa: all?. The enumerable methods are a must read (really, read and understand them one by one). ruby-doc.org/core-1.9.3/Enumerable.html \$\endgroup\$
    – tokland
    Commented Nov 5, 2012 at 8:02
  • \$\begingroup\$ Sometimes the logic setting up a condition is the worst part of programming, and, after getting done, still won't look right. It's like staring at a word and it just doesn't look like it's spelled right, though you know it is. I generally try to go with whatever combination results in the most concise, yet readable, code for the long-term. After I get done writing it, it could be passed to several other programmers so I don't want them coming back later asking what-the-heck that code does. I've used all? against an array, and chains of && and ||, depending on what felt right. \$\endgroup\$ Commented Nov 9, 2012 at 5:08
  • \$\begingroup\$ @the Tin Man. Thanks for commenting. I think we had our differences in the past about how to write logic, didn't we? :-) I agree that it's hard to get it right, logic is so important (is there anything more important in an app?) that it deserves to invest time on it. For me the best logic is that which resembles mathematical logic, that uses no early "return"s, in-line conditionals, nor any other imperative stuff. Just the good old logic expressions. \$\endgroup\$
    – tokland
    Commented Nov 9, 2012 at 11:32
  • \$\begingroup\$ When I was writing assembly language, Perl and C, I wrote my conditionals a lot more tersely and concisely. Languages like Ruby encourage a more expressive coding style, that tries to sit between a conversation with the computer, and cold-logic. I prefer conciseness over verbosity personally, but not everyone can deal with Perl-like code, so I try to adjust. In your answer above, your point about short-circuiting is important, especially as the list of conditions grows. Blindly looping over every element can become costly so && and || become important. \$\endgroup\$ Commented Nov 9, 2012 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.