What is wrong with my Palindrome program? (Ruby, the user himself enters the string)

So I decided to make a palindrome program, but I did this by checking the line entered by the user. To do this correctly, I wanted to drop the registration, spaces and punctuation. I managed to get everything except the punctuation part that works. Every time I try to use a string like "Madame, I'm Adam," the program crashes. I am very new to Ruby and I only learned my knowledge on the Codecademy website. And I also use the editor provided to run my code. Every time I run it like:

puts "Enter a string!" 
user_input = gets.chomp
user_input.downcase!
user_input = user_input.gsub(/[^0-9a-z ]/i, '')
if user_input.include?(" ")
  user_input.gsub!(/ /, "")
end
if user_input == user_input.reverse
  print "Is a pallindrome"
else
  print "Is not a pallindrome"
end

Failure. But if I ran it like:

puts "Enter a string!" 
user_input = gets.chomp
user_input.downcase!
if user_input.include?(" ")
  user_input.gsub!(/ /, "")
end
if user_input == user_input.reverse
  print "Is a pallindrome"
else
  print "Is not a pallindrome"
end

It works. What am I doing wrong here? Why does my program always crash when I try to cancel punctuation?

+4
1

. Codecademy , , ...

:

gets

', ...

- . , .

Sublime Text ruby. , ruby <your_file>.rb , .

+3

Source: https://habr.com/ru/post/1548423/


All Articles