When you want to convert a varible v
into false
when it is nil
/false
and true
otherwise, there is an easy way to do it:
!!v
But is there an easy way to convert v
into nil
when it is nil
/false
and retain its value otherwise?
You can use ||=
:
a = 'hello'
a ||= nil #=> "hello"
a = true
a ||= nil #=> true
a = false
a ||= nil #=> nil
a = nil
a ||= nil #=> nil
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