From: Todd Benson Date: 2008-03-12T02:44:18+09:00 Subject: Re: newbie question On Tue, Mar 11, 2008 at 6:18 AM, Benjamin Oakes wrote: > 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) > > Because your first test will be different if "out" already exists and is not nil. The OP wants "out" to change only if "some_variable" is not nil. Todd