F
Frantisek Psotka
is in ruby operator for:
a = b if b
a (operator) b
?
a = b if b
a (operator) b
?
Farrel said:a ||= b
a ||= b
a = b if b
a ||= b
Farrel
a ||= b
a ||= b
yes, i can write
a = b || a
but isn't it more complex. imagine that b is nil. then a = a will be
evaluated?
maybe time for feature request? new operator a =|| (a = b if b)
(variable = params[:nice_symbol] if params[:nice_symbol] doesnt look
very nice)
Direction for Ruby future should be to clean those ugly perlism from
Ruby (for example magic variable $_)
yes, i can write
a = b || a
but isn't it more complex. imagine that b is nil. then a = a will be
evaluated?
maybe time for feature request? new operator a =|| (a = b if b)
(variable = params[:nice_symbol] if params[:nice_symbol] doesnt look
very nice)
"a ||= b" means "a = b unless a".
I don't think there is an operator for "a = b if b".
Luckily, "a = b if b" is valid ruby code. "a = b || a" or "a = (b or
a)" would also work.
I guess I don't understand what the point of having a = b if b is.
The only thing I can see that's different than a ||= b is that false
will become nil. Here's what I tried:
def demo(some_value)
out = some_value if some_value
puts("out: #{out.inspect}")
out = nil || some_value
puts("out: #{out.inspect}")
out = some_value
puts("out: #{out.inspect}")
a_different_variable ||= some_value
puts("a_different_variable: #{a_different_variable.inspect}")
puts('------------')
end
demo(nil)
demo(88)
demo(false)
Outputs:
out: nil
out: nil
out: nil
a_different_variable: nil
------------
out: 88
out: 88
out: 88
a_different_variable: 88
------------
out: nil
out: false
out: false
a_different_variable: false
------------
So... Why doesn't ||= work for how you're using it? (Why do you want
the "b if b" behavior?)
-- Ben (aka divokz)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.