About gets and gets.chomp

It's not a big deal to write code in Ruby, but I overlooked it and got hooked.

I thought I knew the difference between gets and gets.chomp

I thought I knew that gets had a newline code at the end and gets.chomp didn't have a newline code, but ...

input = gets
array=['cat','cat1','cat2','cat3','dog','tomcat']
array.each do |a|
  puts a.include?(input)
end

Then, all return with false. As I wrote at the beginning, gets has a newline code at the end, so the include? Method is looking for a match with cat plus newline code, so it will be false.

input = gets
array=[]
for i in 1..6 do
  array_input=gets
  array << array_input
end
array.each do |a|
  puts a.include?(input)
end

If you enter cat, cat1, ... and the same elements as the above code array in order, only the first and last (cat and tomcat) will be returned as true. (I was addicted to this) This means that if the value entered in gets in the part that is being turned in the for statement ends with cat, the line feed code will be inserted at the end, so it will match. Anything that does not end with cat is recognized as inconsistent.

answer

If input = gets.chomp, the line feed code will not be included in the first input character string, so the intended result will be obtained.

input = gets.chomp
array=['cat','cat1','cat2','cat3','dog','tomcat']
array.each do |a|
  puts a.include?(input)
end

Recommended Posts

About gets and gets.chomp
About the difference between gets and gets.chomp (other than line breaks)
About Bean and DI
About classes and instances
About redirect and forward
About encapsulation and inheritance
About Serializable and serialVersionUID
About for statement and if statement
About synchronized and Reentrant Lock
About Ruby hashes and symbols
[Java] About String and StringBuilder
About the same and equivalent
About classes and instances (evolution)
About pluck and ids methods
Consideration about classes and instances
About Java Packages and imports
About Ruby and object model
About Ruby classes and instances
About instance variables and attr_ *
About self-introduction and common errors
About C # lambda expressions and Linq
About Java static and non-static methods
About fastqc of Biocontainers and Java
Learn more about gems and bundlers
About the equals () and hashcode () methods
[Java beginner] About abstraction and interface
About Ruby single quotes and double quotes
About Gradle's setup phase and execution phase
About Ruby product operator (&) and sum operator (|)
About go get and go install from Go1.16
About removeAll and retainAll of ArrayList
About =
About if statement and branch processing
About object-oriented inheritance and about yield Ruby
About Java primitive types and reference types
Consideration about Rails and Clean Architecture
This and that about Base64 (Java)
About Docker, disguise server and container
[Swift] A note about function and closure
About the operation of next () and nextLine ()
[About JDBC that connects Java and SQL]
About the difference between irb and pry
About the mechanism of the Web and HTTP
Find out about instance methods and self