DRY up Ruby ternary
Posted
by
Reed G. Law
on Stack Overflow
See other posts from Stack Overflow
or by Reed G. Law
Published on 2011-01-07T22:25:24Z
Indexed on
2011/01/07
22:53 UTC
Read the original article
Hit count: 211
I often have a situation where I want to do some conditional logic and then return a part of the condition. How can I do this without repeating the part of the condition in the true or false expression?
For example:
ClassName.method.blank? ? false : ClassName.method
Is there any way to avoid repeating ClassName.method
?
Here is a real-world example:
PROFESSIONAL_ROLES.key(self.professional_role).nil? ?
948460516 : PROFESSIONAL_ROLES.key(self.professional_role)
© Stack Overflow or respective owner