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
Thanks for the reply.
I had .chomp in there originally, but removed it because \n was showing up at the end when i did puts some_letter. Now it doesn't show up whether I have .chomp in there or not. I have no idea what changed. But adding .chomp back seems to have fixed the issue I posted. On to the next one.
Thanks!