Search code examples
rubypalindromeenumerable

How to check if an appropriate enumerable is a palindrome


Obviously, a hash would not work for this kind of test. Anyways, here's what I have so far:

module Enumerable
  def palindrome?
    arr = []
    self.reverse_each do |x|
      arr << x
    end
    self == arr
  end
end

Another idea I had was to cycle through arr and self element by element with a for loop to check.


Solution

  • module Enumerable
      def palindrome?
        a = to_a
        a == a.reverse
      end
    end