# Avoiding null in variable assignment

I want to get empty string or the string value of the object

Which code you will use and why?

value = object.to_s


or this

value = object.nil? ? "" : object

• The second alternative doesn't result in a string, so I guess there's a typo? – TryPyPy Jan 25 '11 at 22:50
• @TryPyPy if you are going to print value right after those statements, there is no difference. You just need something that responds_to? to_s – Pablo Fernandez Jan 26 '11 at 23:33
• I want to express in my code that there is check for nil and the object could be actually nil, so that if someone is reading my code to be aware of that. so probably the value = object || "" will fits best my style even that object.to_s is shorter. – Gudata Jan 27 '11 at 5:55

If object is either nil or a string, you can just do value = object || "".

If it can be anything and you want to get a string, your second solution doesn't actually do what you want, since it won't turn the object into a string if it's not nil. To fix that your second solution would become value = object.nil? ? object.to_s : "". Of course since now you're calling to_s in both solutions there is no reason to prefer the second over the first, so I'd go with the first.

• if it's a string I would definitely go for the || solution – ecoologic Jan 25 '11 at 23:46
• If object is always either nil or string, you do not need to do either of the above because nil.to_s returns "" . Thus all you need is object.to_s You second option (checking object.nil?) gains you absolutely nothing at all... however your first option (using object || "") will also work appropriately if object is not nil, but is false... and it's what I'd choose.... it's also shorter, which means it's easier to read. :) – Taryn East Feb 22 '11 at 18:23
• @Taryn: "You second option (checking object.nil?) gains you absolutely nothing at all" That's what I said. Note that I recommend going with object.to_s in that case. – sepp2k Feb 22 '11 at 18:31

I've read in here(act_as_good_style) (search for .nil? first occurrence) that you should not check for .nil? unless you really want to check that, while if you want to know if the object is valued you should go for something like that

value = object ? object.to_s : ''


That by the way fits very well with my policy standard behavior first(exception for very short code first when else statement too long).

• The thing I don't get is why you'd want to explicitly handle nil when just value = object.to_s will do the exact same thing anyway. – sepp2k Jan 26 '11 at 11:03
• Also, this code won't work when object === false. – Gareth Jan 26 '11 at 23:00
• you're both right, thanks for correcting me... beh that link is not mine and written by somebody that knows what she says... – ecoologic Jan 26 '11 at 23:16

I would do:

 v = object.to_s 

nil.to_s returns "".

Remember nil is also an object in ruby.

In your specific case, using object.to_s, you don't actually need to check for nil at all since ruby handles this for you. If the object is nil it will return an empty string.

Evidence from the irb:

• object = nil # => nil

• object.to_s # => ""

• object = Object.new # => #<Object:0x10132e220>

• object.to_s # => "#<Object:0x10132e220>"

I would do this, personally:

value = object unless onject.nil?


This seems a little more expressive to me. Its something I wish we could do in C++, instead of using the ternary operator.

• hmm, you may need to spell-check and logic-check that example... :) – Taryn East Feb 22 '11 at 18:28