Reputation: 18855
Is there a nicer way of doing the following in Ruby?:
user.some_property = something if !something.nil?
So, set user.some_property
to the value of something
if something
is not nil
. I need to make that check or Rails throws an error.
Thanks
Upvotes: 0
Views: 190
Reputation: 8212
I'm sorry, but I can't see why:
user.some_property = something unless something.nil?
is preferable to:
user.some_property = something if something
Unless you want some_property to be set as false when something = false rather than nil.
Upvotes: 2
Reputation: 10997
I think the obvious choice is
user.some_property = something unless something.nil?
But if you do this, you need to think about the consequences of user.some_property
potentially being nil
. A good way to handle this might be a default value for some_property
on initialization
Upvotes: 3