0

I need to compare two arrays in Rails but I need to ignore case. I am trying to compare the header array with the expected header array.

  helper_method :check_header
  def check_header(expected_header,csv_file)
        header = CSV.open(csv_file, 'r') { |csv| csv.first }
        valid_csv = true
        if header !=   expected_header
           $csv_error = "Header:<br> #{header} <br> Expected Header: <br> #{expected_header} "
           valid_csv = false
        end
        return valid_csv
  end

I tried .downcase but that is only for strings, not arrays. Is there a similar operator for arrays or do I have to walk through the elements of the array?

Chris Mendla
  • 987
  • 2
  • 10
  • 25
  • [Comparing two arrays in Ruby](https://stackoverflow.com/questions/9095017/comparing-two-arrays-in-ruby) – Imran Ali Oct 17 '17 at 19:49
  • Possible duplicate of [Comparing two arrays in Ruby](https://stackoverflow.com/questions/9095017/comparing-two-arrays-in-ruby) – Imran Ali Oct 17 '17 at 19:50

1 Answers1

0

If you want to downcase the members of an array, you can do so like this:

> ary = ["A", "B", "C"]
 => ["A", "B", "C"] 

> ary.map(&:downcase)
 => ["a", "b", "c"] 

So if you want to know whether two arrays of strings are identical up to case, you can do:

ary1.map(&:downcase) == ary2.map(&:downcase)

Note that this is still array comparison, so order matters.

hoffm
  • 2,386
  • 23
  • 36