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:in `palindrome?'
     # spec.rb:7:in `block (2 levels) in <top (required)>'
     # ./lib/rspec_runner.rb:36:in `block in run_rspec'
     # ./lib/rspec_runner.rb:32:in `run_rspec'
     # ./lib/rspec_runner.rb:23:in `run'
     # lib/graders/weighted_rspec_grader.rb:6:in `grade!'
     # ./grade:31:in `<main>'

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

···

--
Regards :slight_smile:
Karan

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'

···

--
Posted via http://www.ruby-forum.com/.

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

a = "radar"
b = "fred"

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