3

I need a function to check if an array contains other arrays, or more generally if an array contains a certain class. My naive first approach is:

found=false
[1,"a",[],:asdf].each { |x| found=(x.is_a? Array) ? true : found }
puts found

Any way to optimize this?

4 Answers 4

6

In newer Rubies (2.5+) you don't need to pass a block to any?. That links goes to the latest docs, which has a similar example checking for integers.

Therefore, you can simply use:

[1, "a", [], :asdf].any?(Array)
# => true

[1, "a", :asdf].any?(Array)
# => false

That seems the simplest syntax and most sensible approach here if your Ruby version allows it :)

Sign up to request clarification or add additional context in comments.

Comments

4

You can try #any?

For example:

[1,"a",[],:asdf].any? { |el| el.is_a? Array }
#=> true

Comments

2

Almost always when you find yourself using each, you are missing some higher-level abstraction.

In this case, that abstraction is the method Array#any?, which will return true if any element of the array satisfies the predicate:

[1, "a", [], :asdf].any?(Array)

3 Comments

Your edit makes your answer the same as @SRack's earlier answer.
@CarySwoveland, this is one of the ways he farms his reputation for all these years.
@Nakilon, we are fortunate to have Mr. Mittag. I've learned a lot from him.
1

this approach iterate over all elements even if you found an array. in your test case it will iterate over :asdf, and you don't have to check this.

I think you need to break from loop if you found the certain type(Array in our Example).

found = false
[1,"a",[],:asdf].each do |x|
    if x.is_a? Array
     found = true
     break
    end
end

puts found

Also you can use any instead of each

found = [1,"a",[],:asdf].any? { |x| x.is_a? Array }
puts found # true

1 Comment

That edit makes this a copy of @AlexGolubenko's answer :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.