5

Is this a shorter, more elegant, more DRY way to write this in Ruby?

if first_variable
   first_variable # return variable if it exists
else
   second_variable # otherwise, return something else
end

Or this?

if first_variable
   first_variable.method_name 
else
   second_variable
end

3 Answers 3

19

Your two examples are semantically different, so I'll only give an example of the first.

return first_variable || second_variable

Your second example returns the result of a method call if first_variable is not nil. This is different than your first example, so I don't understand comparing them. I also don't understand your use of DRY. You are not repeating yourself in either case. Why does an if statement bother you so much? This is not the stuff you should be worrying about.

7
  • 1
    return first_variable || second_variable is the way to go. Commented Nov 5, 2010 at 12:39
  • Thanks, I was mostly wondering if there was a way to avoid writing "first_variable" twice. I am working on an application where I am checking to see if a lot of variables exist, and then returning it if does. Some of those variable names exist. It just seemed like there was a more elegant way to do it. Or that there might something similar to ampersand+colon(&:) shortcut.
    – Chanpory
    Commented Nov 5, 2010 at 15:33
  • Ok, I removed the second example. I only use Ruby for simple scripts, I am not really a "rubyist". I try to learn the Ruby way to do things, but it is not one of my primary languages, so I'm not always up to speed on common idioms. Commented Nov 5, 2010 at 18:12
  • Can you use or instead? Commented Oct 15, 2013 at 5:11
  • @KieranAndrews: You can. You should be aware however that or has lower precedence than ||. They both short circuit. Commented Oct 15, 2013 at 5:35
1

Ruby also supports the ternary operation known from C:

return first_variable ? first_variable.method_name : second_variable

Another possibility is to write the usual "if" version on one line, which feels more like natural language:

return if first_variable then first_variable.method_name else second_variable end
1
  • I prefer the ternary version. There is better visual separation of the value being returned making it easier to see what is happening. Functionally they are the same, but maintenance-wise the first is easier to grok. Commented Nov 5, 2010 at 13:47
1

Using || is the only syntax that is DRY using basic Ruby. However this won't work:

  • if you want to reject var1 when it's in a truthy state, e.g. ""
  • if you want to keep var1 when it's in a falsy state, e.g. false (which is different for your purposes to nil)

In short if your aim is to check whether var1 is present, you will have to repeat yourself with some kind of var1.check ? var1 : var2 syntax. (check isn't a real method – I'm using that to mean whatever check you want to do on it to ensure it's present for your app's purposes.)

The Demand gem which I wrote gets around this and allows a DRY syntax:

return demand(var1, var2)

This checks whether var1 is present and returns it if so. Otherwise, it returns var2.

1
  • 1
    The first exception you mention can be solved by using the presence method: var1.presence || var2 This works because presence returns the value of var1 if var1.present? is true, and nil otherwise.
    – AddAppend
    Commented Dec 17, 2019 at 21:27

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.