ruby 展平整数数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 展平整数数组相关的知识,希望对你有一定的参考价值。

class Array

  # Flattens only an array of integers with varying dimensions
  def flatty!
    self.to_s.tr('[]','').split(',').map(&:to_i)
  end

  def flatty_test
    a1 = [[1, 2, [3]], 4]
    a2 = [1, [2, 3, 4], [[[5, [6]]]]]
    puts a1.flatty!.to_s
    puts a2.flatty!.to_s
  end
end

以上是关于ruby 展平整数数组的主要内容,如果未能解决你的问题,请参考以下文章