Ruby implementation array#flatten : I need to implement ruby Array#Flatten. This implementation remove all the nested arrays

    a = [ 1, 2, [3, [4, 5] ] ]

      def my_flatten(arr)
        arr.reduce([]) do |result, item| 
          item.is_a?(Array) ? result + my_flatten(item) : result << item
        end
      end

    my_flatten(a) #=> [1, 2, 3, 4, 5]

Prompt how to implement such behavior

    a.flatten(1) #=> [1, 2, 3, [4, 5]]