Sha256: 3e9bb1c2359c18d6dd94df44a1fb45b5788b00a4c51ad8a763b418a3fba52445

Contents?: true

Size: 988 Bytes

Versions: 5

Compression:

Stored size: 988 Bytes

Contents

module PyCall
  class Tuple
    include PyObjectWrapper

    def self.new(init)
      case init
      when Integer
        super(LibPython.PyTuple_New(init))
      when Array
        tuple = new(init.length)
        init.each_with_index do |obj, index|
          tuple[index] = obj
        end
        tuple
      when PyObject
        super(init)
      end
    end

    # Make tuple from array
    def self.[](*ary)
      new(ary)
    end

    def initialize(pyobj)
      super(pyobj, LibPython.PyTuple_Type)
    end

    def length
      LibPython.PyTuple_Size(__pyobj__)
    end

    def [](index)
      LibPython.PyTuple_GetItem(__pyobj__, index).to_ruby
    end

    def []=(index, value)
      value = Conversions.from_ruby(value)
      LibPython.PyTuple_SetItem(__pyobj__, index, value)
    end

    def to_a
      [].tap do |ary|
        i, n = 0, length
        while i < n
          ary << self[i]
          i += 1
        end
      end
    end

    alias to_ary to_a
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pycall-0.1.0.alpha.20170307 lib/pycall/tuple.rb
pycall-0.1.0.alpha.20170302 lib/pycall/tuple.rb
pycall-0.1.0.alpha.20170226 lib/pycall/tuple.rb
pycall-0.1.0.alpha.20170224b lib/pycall/tuple.rb
pycall-0.1.0.alpha.20170224 lib/pycall/tuple.rb