0

I want to check if array has any elements that are instances of Array. I could make a recursive function and that iterates and finds all elements that are Array. Is there a shorter way to do this?

Get all elements that are of x type and manipulate them (i.e. modify or delete them)

6
  • 1
    arr.flatten.size > arr.size works, but @sawa's answer is more efficient and reads better. Commented Aug 14, 2015 at 1:26
  • 2
    @CarySwoveland array.flatten == array may work. Commented Aug 14, 2015 at 1:32
  • i need to delete items recursively Commented Aug 14, 2015 at 2:26
  • 1
    I don't understand what you mean by "delete items recursively", or more generally, your reference to recursion. This couldn't concern deeply nested arrays, for example, because if you delete an array a from arr everything within a is gone, of course. Moreover, it would make no sense to start at the "bottom" and work up, because when you get to the "top" and deleted the element a of arr, all your previous work would have been just a waste of time--those deletions would have occurred anyway had you just deleted a in the first place. Commented Aug 14, 2015 at 5:20
  • i meant delete element x even in nested arrays Commented Aug 14, 2015 at 14:55

2 Answers 2

4

It can be done this way:

array.any?{|e| e.is_a?(Array)}
Sign up to request clarification or add additional context in comments.

7 Comments

This will only check if the top level array has any elements that are arrays, but will not check the sub arrays (and sub-sub arrays and so on) as the OP requested.
@neuronaut I cannot understand your comment under the OP's description. "check the sub arrays (and sub-sub arrays and so on)" is not what OP requested.
It looks like the OP is asking to find instances of arrays and then search those arrays for instances of arrays, and so on, recursively. At least, I think that's what's meant by "I could make a recursive function and that iterates and finds all elements"
@neuronaut That is the OP's solution, which does not do what the OP asked for. Furthermore, how can an array have a subarray without having an array as its direct element? You mean that it may be embedded in some (non-array) construct that is an element of the array?
The OP is asking "if array has ....", which is answered either by yes (true) or no (false).
|
0

Here is one possible solution,
You can use Ruby blocks to act on sub-arrays & nested sub-arrays recursively.

arr = [1, [2,3], [4,[5,6]]]

def act_on_array(arr, block) 
    arr.each do |i|  
        if i.is_a?(Array) then
            block.yield(i)
            act_on_array(i, block)
        end
    end 
end

act_on_array(arr, lambda {|x| puts "I should act on #{x} as it is an array" })

Output of program

I should act on [2, 3] as it is an array
I should act on [4, [5, 6]] as it is an array
I should act on [5, 6] as it is an array

Comments

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.