This article uses Ruby 2.6.5 installed on macOS Catalina 10.15.6. I have summarized the include? method in my own way.
--Method to judge whether the element specified by the argument is included in the array or character string
array = ["dog", "cat"]
puts array.include?("dog") # => true
puts array.include?("hoge") # => false
--Output example: Enter the name you want to register (Example) Yamada Taro ――In this case, you want to receive the entered name and output an error statement if there is a "." Or "blank".
def check_name(name)
if name.include?(".")
puts "!error!Symbols cannot be registered"
elsif name.include?(" ")
puts "!error!Blanks cannot be registered"
else
puts "Registration has been completed"
end
end
puts 'Please enter the name you want to register'
name = gets
check_name(name)
--In the second line, include in the argument name of the check method? If there is an argument "." (Period) in, true is returned and an error statement is output. --Similarly, if there is an include? Argument "" (blank) in name, true is returned and an error statement is output.
Recommended Posts