I know I can live without it, but the question's been bugging me.
Is there a Ruby idiom that's equivalent to Groovy's Elvis operator (?:
)?
Essentially, I want to be able to shorten this
PARAM = ARGV[0] ? ARGV[0] : 'default'
Or equivalently
PARAM = 'default' unless PARAM = ARGV[0]
Into something like this
PARAM = ARGV[0] ?: 'default'
Never mind :-) I just found the answer myself after finding out the name of the operator.
From here:
PARAM = ARGV[0] || 'default'
(Must be 'cause I'm juggling 4 languages right now so I forgot I could do that in the first place.)
Possible since Ruby 2.3
.
dog&.owner&.phone
Isn't PARAM = ARGV[0] ? ARGV[0] : 'default'
the same as PARAM = (ARGV[0] || 'default')
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With