Sha256: eae4af237838d7f354edebd80cf297b8de3244ba5ce296f64689737604808ae5

Contents?: true

Size: 1.26 KB

Versions: 11

Compression:

Stored size: 1.26 KB

Contents

module PyCall
  class List
    include PyObjectWrapper
    include Enumerable

    def self.new(init=nil)
      case init
      when LibPython::PyObjectStruct
        super
      when nil
        new(0)
      when Integer
        new(LibPython.PyList_New(init))
      when Array
        new.tap do |list|
          init.each do |item|
            list << item
          end
        end
      else
        new(obj.to_ary)
      end
    end

    def <<(value)
      value = Conversions.from_ruby(value)
      LibPython.PyList_Append(__pyobj__, value)
      self
    end

    def size
      LibPython.PyList_Size(__pyobj__)
    end

    def include?(value)
      value = Conversions.from_ruby(value)
      value = LibPython.PySequence_Contains(__pyobj__, value)
      raise PyError.fetch if value == -1
      1 == value
    end

    def ==(other)
      case other
      when Array
        self.to_a == other
      else
        super
      end
    end

    def each
      return enum_for unless block_given?
      i, n = 0, size
      while i < n
        yield self[i]
        i += 1
      end
      self
    end

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

    alias to_ary to_a
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
pycall-0.1.0.alpha.20170502 lib/pycall/list.rb
pycall-0.1.0.alpha.20170426 lib/pycall/list.rb
pycall-0.1.0.alpha.20170419b lib/pycall/list.rb
pycall-0.1.0.alpha.20170419a lib/pycall/list.rb
pycall-0.1.0.alpha.20170419 lib/pycall/list.rb
pycall-0.1.0.alpha.20170403 lib/pycall/list.rb
pycall-0.1.0.alpha.20170329 lib/pycall/list.rb
pycall-0.1.0.alpha.20170317 lib/pycall/list.rb
pycall-0.1.0.alpha.20170311 lib/pycall/list.rb
pycall-0.1.0.alpha.20170309 lib/pycall/list.rb
pycall-0.1.0.alpha.20170308 lib/pycall/list.rb