I'm looking for a way to convert an empty string to nil
in place using Ruby. If I end up with a string that is empty spaces I can do
" ".strip!
This will give me the empty string ""
.
What I would like to be able to do is something like this.
" ".strip!.to_nil!
This will get an in place replacement of the empty string with nil
. to_nil!
would change the string to nil
directly if it is .empty?
otherwise if the string is not empty it would not change.
The key here is that I want it to happen directly rather than through an assignment such as
f = nil if f.strip!.empty?
String#presence
but it's not in-place (note that both inplace/rebindings are -usually- a bad practice).