Sha256: 95c3c86f33d93c0a61dccc64122d337b8d676a3841ab47c04f023aa2cc13581e

Contents?: true

Size: 1.61 KB

Versions: 8

Compression:

Stored size: 1.61 KB

Contents

module Enumerable

  def each_coordinate opts = {}, &block
    iter_coordinate :each, opts, &block
  end

  def map_coordinate opts = {}, &block
    iter_coordinate :map, opts, &block
  end
  alias_method :collect_coordinate, :map_coordinate

  def map_coordinate! opts = {}, &block
    iter_coordinate :map!, opts, &block
  end
  alias_method :collect_coordinate!, :map_coordinate!

  def iter_coordinate meth, opts = {}, &block
    opts[:recurse] = true if opts[:recurse].nil?

    if Array === self and Numeric === self[0]
      yield self
    else

      self.__send__ meth do |pair|
        raise IndexError unless Array === pair
        case pair[0]
        when Numeric
          yield pair
        when Array
          pair.iter_coordinate meth, opts, &block if opts[:recurse]
        else
          raise IndexError.new "#{pair[0]} is not a Numeric or Array type"
        end
      end
    end
  end

  def rotate_until &block
    return if block[]
    found = false
    length.times do
      push shift
      if block[]
        found = true
        break
      end
    end
    raise IndexError unless found
  end

  def rotate_until_first_equals obj
    rotate_until { at(0) == obj }
  end

  def polygonally_equal_to? obj
    raise ArgumentError unless Enumerable === obj
    return false if self.length != obj.length

    equal = true

    # clone so can pop/rotate
    me = self.clone
    obj = obj.clone

    # pop to drop the duplicate, polygon-closing, coordinate
    me.pop
    obj.pop

    begin
      obj.rotate_until_first_equals me[0]
      equal = me == obj
    rescue IndexError
      equal = false
    end

    equal
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
terraformer-0.1.0 lib/ext/enumerable.rb
terraformer-0.0.9 lib/ext/enumerable.rb
terraformer-0.0.8 lib/ext/enumerable.rb
terraformer-0.0.7 lib/ext/enumerable.rb
terraformer-0.0.6 lib/ext/enumerable.rb
terraformer-0.0.4 lib/ext/enumerable.rb
terraformer-0.0.3 lib/ext/enumerable.rb
terraformer-0.0.2 lib/ext/enumerable.rb