3
votes
\$\begingroup\$

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
\$\endgroup\$

locked by Jamal Nov 7 '16 at 23:56

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. See the help center for guidance on writing a good question.

Read more about locked posts here.

  • 2
    \$\begingroup\$ The second alternative doesn't result in a string, so I guess there's a typo? \$\endgroup\$ – TryPyPy Jan 25 '11 at 22:50
  • \$\begingroup\$ @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 \$\endgroup\$ – Pablo Fernandez Jan 26 '11 at 23:33
  • \$\begingroup\$ 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. \$\endgroup\$ – Gudata Jan 27 '11 at 5:55
12
votes
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ if it's a string I would definitely go for the || solution \$\endgroup\$ – ecoologic Jan 25 '11 at 23:46
  • \$\begingroup\$ 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. :) \$\endgroup\$ – Taryn East Feb 22 '11 at 18:23
  • \$\begingroup\$ @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. \$\endgroup\$ – sepp2k Feb 22 '11 at 18:31
5
votes
\$\begingroup\$

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).

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – sepp2k Jan 26 '11 at 11:03
  • 1
    \$\begingroup\$ Also, this code won't work when object === false. \$\endgroup\$ – Gareth Jan 26 '11 at 23:00
  • \$\begingroup\$ you're both right, thanks for correcting me... beh that link is not mine and written by somebody that knows what she says... \$\endgroup\$ – ecoologic Jan 26 '11 at 23:16
4
votes
\$\begingroup\$

I would do:

v = object.to_s

nil.to_s returns "".

Remember nil is also an object in ruby.

\$\endgroup\$
3
votes
\$\begingroup\$

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>"
\$\endgroup\$
2
votes
\$\begingroup\$

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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.