About the difference between gets and gets.chomp (other than line breaks)

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!

Conclusion

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.

Finally

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. lengthIn 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

About the difference between gets and gets.chomp (other than line breaks)
About the difference between irb and pry
About gets and gets.chomp
[Rails / ActiveRecord] About the difference between create and create!
[Ruby] I thought about the difference between each_with_index and each.with_index
About the difference between classes and instances in Ruby
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
Understand the difference between each_with_index and each.with_index
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
The difference between puts and print in Ruby is not just the presence or absence of line breaks
[Java] Understand the difference between List and Set
[iOS] Understand the difference between frame and bounds
Understand the difference between abstract classes and interfaces!
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
What is the difference between a class and a struct? ?? ??
What is the difference between System Spec and Feature Spec?
In fact, Ruby distinguishes between line breaks and whitespace.
About the relationship between HTTP methods, actions and CRUD
[Rails] What is the difference between redirect and render?
[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
Compare the difference between dockerfile before and after docker-slim
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between skip and pending? [RSpec]
Difference between vh and%
[Rails] I investigated the difference between redirect_to and render.
Difference between i ++ and ++ i
What is the difference between Java EE and Jakarta EE?
[Swift] UITextField taught me the difference between nil and ""
[Rails] What is the difference between bundle install and bundle update?
Difference between Java and JavaScript (how to find the average)
What is the difference between an action and an instance method?
[Java] Check the difference between orElse and orElseGet with IntStream
Think about the differences between functions and methods (in Java)
Let's override the difference between == (identity) and equals method (equivalence)
The difference between programming with Ruby classes and programming without it
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
About the same and equivalent
String concatenation and line breaks
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
About the relationship between the Java String equality operator (==) and initialization. Beginners
What is the difference between a web server and an application server?
[Java] What is the difference between form, entity and dto? [Bean]
Easy to understand the difference between Ruby instance method and class method.