[Ruby] Notes on gets method

When I compared it with the character string from the standard input, it didn't work for some reason. The cause and solution are described as a memorandum.

problem

The gets method is a method for receiving strings from standard input. If you try to compare the input string obtained using this method with another string I didn't get the results I expected.

input = gets #Enter hoge
bool = (input == 'hoge')
print bool 
false

I should be comparing strings with the same value, but for some reason false is returned.

Cause

After checking, it seems that the gets method adds ** line feed code (\ n) ** at the end by default.

input = puts #Enter hoge
p input
"hoge\n"

I was trying to compare with the line feed code included, so it seems that it was played even if I compared it.

solution

It seems that I should delete the line feed code at the end, so I checked it and found There is a chomp method as a method for that, When I used this, I got the expected result!

input = gets.chomp #Enter hoge
bool = (input == 'hoge')
print bool 
true

Supplement

I thought that chomp was an abbreviation for something, and when I looked it up, the answer in teratail was helpful. What does chomp stand for?

It contains a lot of speculation Originally, there was chop and I was able to erase the last character. It was a (Unix) manner that the end of the text file ends with a line break, so this was fine. There is a problem that there is no line break at the end and there are environments where it is not a single character A "remove trailing newline" function has been added and it is chomp because it is for multiline of chop.

It seems that the chomp method is used to delete the line feed code from the last character of chop.

reference

https://stackoverflow.com/questions/21504202/why-is-stringchomp-named-like-this

Recommended Posts

[Ruby] Notes on gets method
Ruby Thread # [] = method notes
[Ruby on Rails] Convenient helper method
Ruby to_s method
Ruby on Rails installation method [Mac edition]
Ruby Hash notes
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
Notes on using FCM with Ruby on Rails
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
[Ruby on Rails] NoMethodError undefined method `devise_for'error resolution
Ruby on Rails address automatic input implementation method
[Ruby on Rails] How to use session method
Ruby Learning # 30 Initialize Method
Notes on Protocol Buffers
abbreviation for ruby method
python notes on docker
[Android] Notes on xml
Ruby Learning # 24 Exponent Method
Notes on regular expressions
Ruby study notes (puts)
Ruby on Rails Elementary
Ruby on Rails basics
definition of ruby method
Ruby On Rails Association
[Ruby] Method definition summary
[Java] Basic method notes
Method summary to update multiple columns [Ruby on Rails]
Ruby on Rails Refactoring method example summary around MVC
Ruby on Rails Tutorial Troublesome notes when running on Windows
[Ruby on Rails] Search function (model, method selection formula)
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
[Ruby on Rails] undefined method ʻid'for nil: NilClass error resolution method
Integer check method with ruby
Ruby algorithm (inject, method definition)
[Ruby on Rails] about has_secure_password
Ruby on Rails record search, create if not find_or_create_by method
Ruby installation on WSL2 + Ubuntu 20.04
Commentary on partial! --Ruby on Rails
Install Ruby 2.7 on RHEL 8 (AppStream)
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Install Ruby 2.7 on CentOS 7 (SCL)
Ruby on rails learning record -2020.10.06
Ruby study notes (variables & functions)
[Ruby] Method that returns truth
Ruby on Rails validation summary
Ruby on Jets Verification (WSL)
Ruby on Rails Basic Memorandum
[ruby] Method call with argument
Install Ruby on Sakura's VPS