Sha256: 4bdd179f48d38b1f8c2fa40b05f22bb0b8ce87595f7bfa7ca166f506721ee3c1

Contents?: true

Size: 1.04 KB

Versions: 18

Compression:

Stored size: 1.04 KB

Contents

#--
# Author::    Tyler Rick
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
# License::   Ruby License
# Submit to Facets?:: Yes.
#++

class Array
  
  # A lot like array.flatten, except that it will also "flatten" ranges (by converting range to range.to_a) and any other objects that respond to to_a contained in the array in addition to arrays contained in the array.
  # Compare with Array#expand_ranges
  def to_a_recursive
    map do |item|
      # Assume that all arrays contain only elements that do not respond to to_a_recursive or arrays.
      if item.respond_to? :to_a_recursive
        item.to_a_recursive
      else
        item.to_a
      end
    end
  end
end

#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test
require 'test/unit'
class TheTest < Test::Unit::TestCase
  def test_1
    assert_equal [[1, 2, 3], [5, 6, 7]], [1..3, 5..7].to_a_recursive
  end
  def test_2
    assert_equal [[1, 2, 3], [ [5, 6, 7], [9, 10] ] ], [1..3, [5..7, 9..10]].to_a_recursive
  end
end
=end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
quality_extensions-0.1.1 lib/qualitysmith_extensions/array/to_a_recursive.rb
quality_extensions-0.1.2 lib/quality_extensions/array/to_a_recursive.rb
quality_extensions-0.1.4 lib/quality_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.13 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.60 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.4 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.5 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.24 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.34 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.17 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.20 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.29 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.49 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.6 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.7 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.33 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.63 lib/qualitysmith_extensions/array/to_a_recursive.rb
qualitysmith_extensions-0.0.64 lib/qualitysmith_extensions/array/to_a_recursive.rb