Sha256: b5feca33910c29fbf6ec16713c582bc3d7d74f87a194bf42708b0035fe60d6eb

Contents?: true

Size: 746 Bytes

Versions: 2

Compression:

Stored size: 746 Bytes

Contents

class Recurator
  include Enumerable

  def initialize (obj)
    @obj = obj
  end

  def each
    looper = lambda { |o|
      case o
      when Array
        o.each_with_index {|i, index|
          yield [index, i]
          looper.call(i)
        }
      when Hash
        o.each { |key, value|
          yield [key, value]
          looper.call(value)
        }
      end
    }

    if block_given?
      looper.call(@obj)
    else
      self.to_enum
    end
  end

  def keys
    map { |key, value| key }
  end

  def values
    map { |key, value| value }
  end

  def flatten
    ret = []
    each { |key, value|
      ret << key
      if !value.kind_of?(Array) && !value.kind_of?(Hash)
        ret << value
      end
    }
    ret
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
recurator-0.0.2 lib/recurator/recurator.rb
recurator-0.0.1 lib/recurator.rb