Zach
Zach

Reputation: 31

Ruby: Checking if Nested Array Contains Array

I'm having trouble checking if a nested array contains a single array. For example:

a = [[1,2], [3,4]]
a.each do |i|
  b= i.inspect
  puts a.include?(b)
end

The output it false and false. If anyone could help out that would be great.

Upvotes: 1

Views: 2168

Answers (2)

Marc Fletcher
Marc Fletcher

Reputation: 1072

a = [[1,2], [3,4]]
a.each do |i|
  # puts i.to_s
  puts a.include?(i)
end

This should do it. inspect is not necessary. This outputs true and true. Uncomment the comment in the code to see the output.

Upvotes: 1

XYZ
XYZ

Reputation: 27397

  1. Flatten the array, Array.flatten
  2. Find the target element, include?

Upvotes: 0

Related Questions