You're confusing the value of a with what the statement a = false if a.nil? is returning.
The console prints the output value of the whole statement; not necessarily the value of a.
If the value of a did change (as in, when it was assigned to), then that new value will print in the console. But if a doesn't change, then nil will be printed.
irb(main):001:0> a = false if a.nil?
=> false # this false is what "a = false if a.nil?" returned. It's false because 'a' was set to 'false'.
irb(main):002:0> a
=> false
irb(main):003:0> a = false if a.nil?
=> nil # this nil is what "a = false if a.nil?" returned. It's nil because 'a' was NOT set to anything.
irb(main):004:0> a
=> false
c/o github.com/rubocop-hq/ruby-style-g...
"Don't use ||= to initialize boolean variables. (Consider what would happen if the current value happened to be false.)"
I've found that when using a language, style guides are more valuable than just style; they can help me avoid common pitfalls like this.
Thanks. I'd never considered styleguides to be such an invaluable resource.
@happy = post_complete? if @happy .nil? is a lot more succinct.I'm going to update the post as I actually prefer this and I think it should be mentioned.It may be that I'm tired and haven't understood it properly but as I understand it, it doesn't do exactly the same thing.
The first time, it evaluates to
false. The second time, however, it evaluates tonil.This is what I get in
pry:I'll need to take a look again at this in the morning. Any input would be welcome!
You're confusing the value of
awith what the statementa = false if a.nil?is returning.The console prints the output value of the whole statement; not necessarily the value of
a.If the value of
adid change (as in, when it was assigned to), then that new value will print in the console. But ifadoesn't change, thennilwill be printed.🤦 Of course. Thanks for the explanation. I'd convinced myself that it was returning the variable's value. Doh!
So, in my case I would use it like so: