Sha256: 76332ba3e98b03249962382fc6d052898418f346c8707989706a5a1e7e2d1ebc

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

class Array
  # flatten & flatten!, standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
  unless ([[]].flatten(1) rescue false)

    # Recursively flatten any contained Arrays into an one-dimensional result.
    # Adapted from rubinius'
    def flatten_with_optional_argument(level=nil)
      return flatten_without_optional_argument unless level || level == (1/0.0)
      dup.flatten!(level) || self
    end

    # Flattens self in place as #flatten. If no changes are
    # made, returns nil, otherwise self.
    # Adapted from rubinius'
    def flatten_with_optional_argument!(level=nil)
      return flatten_without_optional_argument! unless level || level == (1/0.0)
      
      ret, out = nil, []
      ret = recursively_flatten_finite(self, out, level)
      replace(out) if ret
      ret
    end

    alias_method_chain :flatten, :optional_argument
    alias_method_chain :flatten!, :optional_argument

    # Helper to recurse through flattening since the method
    # is not allowed to recurse itself. Detects recursive structures.
    # Adapted from rubinius'; recursion guards are not needed because level is finite
    def recursively_flatten_finite(array, out, level)
      ret = nil
      if level <= 0
        out.concat(array)
      else
        array.each do |o|
          if o.respond_to? :to_ary
            recursively_flatten_finite(o.to_ary, out, level - 1)
            ret = self
          else
            out << o
          end
        end
      end
      ret
    end
    private :recursively_flatten_finite
  end # flatten & flatten!
  
  # index
  unless ([1].index{true} rescue false)
    def index_with_block(*arg)
      return index_without_block(*arg) unless block_given? && arg.empty?
      each_with_index{|o,i| return i if yield o}
      return nil
    end
    alias_method_chain :index, :block
    alias_method :find_index, :index
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
marcandre-backports-1.1.1 lib/backports/array.rb
backports-1.1.1 lib/backports/array.rb