I'm coding something in Ruby where, given a value foo
output from a method call, I want to:
foo
if foo
is truthyfoo
is falsy.The simplest naive way to implement this is probably:
foo = procedure(input)
if foo
foo
else
log_error
default
end
but this feels overly verbose because foo
is repeated three times, and this style is very imperative.
What's the cleanest, most idiomatic way to write this?
(Performance matters-- let's assume that foo
is truthy in the vast majority of cases.)
Living off of Ruby's Perl heritage:
foo = procedure(input) and return foo
log_error
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