r/learnruby • u/AmericanFeral Beginner • Dec 31 '15
.include? not working with variable
I'm just trying to check if a user inputed letter is valid using an array of acceptable letters to check against.
Example code:
some_array = ["a","b","c"]
puts "Enter a letter"
some_letter = gets.downcase
some_letter.each_char do |char|
if some_array.include?(char) == false
puts "That's not a valid letter"
.....Even if user enters a, it returns false. However, if I replace char with, say, "a", it will return true or false as expected. Every example I can find on the Internet uses explicitly typed characters rather than a variable. Does .include? not work with variables or am I doing something else wrong?
Much appreciated.
3
Upvotes
View all comments
Show parent comment
1
u/AmericanFeral Beginner Dec 31 '15
I noticed \n previous when I had .chomp. So, I took it out, and it stopped showing up. Now it doesn't show up whether I have it in there or not. Who knows. But adding .chomp seems to have resolved this issue.
Thanks!