Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/palindrome_ext/array.rb

Overview

Overrides Default Array Class by adding #palindrome? method.

Instance Method Summary collapse

Instance Method Details

#palindrome?(strict = true) ⇒ Boolean

Returns Whether or not the array is a palindrome.

Parameters:

  • strict (defaults to: true)

    whether or not to ignore whitespace and non-alphanumeric characters.

Returns:

  • (Boolean)

    Whether or not the array is a palindrome.



9
10
11
12
13
14
15
# File 'lib/palindrome_ext/array.rb', line 9

def palindrome?(strict = true)
  return self == reverse if strict

  arr = map { |x| x.to_s.downcase.tr("^a-z0-9", "") }
  arr.delete_if(&:empty?)
  arr == arr.reverse
end