Using String's Strip Method Rather Than Chomp
I have been in the habit of using String#chomp when interactively
requesting input from people. I am considering using String#strip
instead. It does more than chomp, in that it will remove leading
whitespace, trailing whitespace and still the $/ which is to say the
record separator. I think that it is more suitable for interactive
input, unless you are counting on leading whitespace from someone for
some reason.
The statement that we always say when talking about chomp and to_i or
to_f is that to_i and to_f does the same thing as chomp, and this is
almost true. But to_i and to_f actually work more like strip.
Consider this:
1 2 3 4 5 6 | |
As you can see, in reality to_i strips the leading whitespace, and by
definition any trailing whitespace (though in reality all string content
that isn’t part of the first recognizable ‘numeric-like’ string)
including the record separator.
So, I am trading in my habitual String#chomp use for the cleaner
String#strip use, and where appropriate String#strip! and I believe I
will still have fun with Ruby!
Let me know what you think about this small movement?