Reputation: 1364
I can replace this:
le_one = load_le_other() unless le_one
with this:
le_one ||= load_le_other()
How can I replace this:
le_other = load_le_other()
le_one = le_other if le_other
with something other than this:
le_other = load_le_other()
le_one = le_other || le_one
load_le_other
is expensive to call twice. The goal is to make it one liner.
Upvotes: 1
Views: 1732
Reputation: 821
This will assign the appropriate values to both le_one
and le_other
:
le_one = (le_other = le_load_other()).nil? ? le_one : le_other
It's counterintuitive that you could assign le_one
to itself before you've even defined it, but it works.
If you have already defined le_one
you can also do this:
le_other = le_load_other()&.tap { |val| le_one = val }
The &.
causes the tap (and its block) to only execute if le_load_other() returns a non-nil value.
Upvotes: 0
Reputation: 110745
If load_le_other()
cannot return false
,
le_one = le_other = load_le_other()
Note that if
le_other = load_le_other()
#=> nil
le_one = le_other if le_other
# le_one = nil if nil
#=> nil
Upvotes: 0