Palindrome error

Hi all,
I am making a function which should return “true” if the input variable
“string” is a palindrome, and returns either “nil” or “false” otherwise.

Following is the regex i used:

def palindrome?(string)
*/\w#{string.reverse}$/i =~ /\w{string}$/i

  • end

The above function, however returns error:

TypeError:
can’t convert Regexp to String
# your_code.rb:2:in =~' # your_code.rb:2:inpalindrome?’
# spec.rb:7:in block (2 levels) in <top (required)>' # ./lib/rspec_runner.rb:36:inblock in run_rspec’
# ./lib/rspec_runner.rb:32:in run_rspec' # ./lib/rspec_runner.rb:23:inrun’
# lib/graders/weighted_rspec_grader.rb:6:in grade!' # ./grade:31:in

Please help.
And my apologies to take your time with such petty problems

Hi,

You cannot pattern match a regex against a regex. This doesn’t make
sense.

If I understand you correctly, you want to disregard character case?
Simply use normal strings and compare them with casecmp():

def palindrome? str
str.casecmp(str.reverse) == 0
end

puts palindrome? ‘Anna’
puts palindrome? ‘Tom’

Why are you using a regex? There is nothing regex about this!

a = “radar”
b = “fred”

a == a.reverse => true
b == b.reverse => false