When I was solving the drill problem posted on the Internet, I made a new discovery about the difference between ruby's `gets``` and
`gets.chomp```, so I will output it!
Line breaks are included in one character! What? Some of you may have thought. It was a new discovery for me ... Take the following code as an example.
ruby.rb
str = gets
puts str.length
The terminal is waiting for input because it uses the gets method.
If you enter'abc', 4 will be output.
I thought that 3 would be returned, and I laughed in a petite panic state, "Why !! ????"
I wondered if I did the same thing with gets.chomp
and it returned 3 as expected.
Until now, it was only recognized that line breaks would not occur by adding chomp
.
It was a new discovery that the return value changed.
length
In the method, not only line breaks but also spaces are counted as one character, but I'm not used to that feeling and it feels strange. Are you used to this?
Recommended Posts