3 ms·
I agree with the grandparent general point, but i would say that it is string formatting more precisely where these nil conversions are useful. For example, yo
by epidemian 3y ago
I agree with the grandparent general point, but i would say that it is string formatting more precisely where these nil conversions are useful.
For example, you can write something like:
bananas = 4
puts "you have #{bananas} banana#{'s' if bananas != 1}"
And this works because: `if` is an expression, `if` evaluates to `nil` if the condition is not met and there's no `else`, and `nil.to_s` is an empty string.
> And logging / error handling / debugging seems like the context where you would least want nil to become an empty string
Yep, 100% agree. And i think Ruby's designers agree too, since the language has the shortest method possible, `p`, for print-debugging. You can do `p something` and that'll print a useful string for debugging (`""` for an empty string, `nil` for nil, `[]` for an empty array, etc).
- deleted 3y ago[deleted]