I am looking for a way to convert an empty string to zero in place using Ruby. If I get a string that is empty space, I can do
" ".strip!
This will give me an empty string "".
What I would like to do is something like this.
" ".strip!.to_nil!
This will replace the empty string with nil. to_nil! will change the string to nil directly if it is empty? otherwise, if the line is not empty, it will not change.
The key point here is that I want this to happen directly, and not through an assignment such as
f = nil if f.strip!.empty?
source share