Sha256: 8f8a043bb603b0a0e7a981d3ff7adc462cea1accdaf2be3f9167ac1ce135f99b

Contents?: true

Size: 776 Bytes

Versions: 6

Compression:

Stored size: 776 Bytes

Contents

module PyCall
  Dict = builtins.dict
  class Dict
    register_python_type_mapping

    include Enumerable

    def self.new(h)
      super(h, {})
    end

    def length
      PyCall.len(self)
    end

    def has_key?(key)
      LibPython::Helpers.dict_contains(__pyptr__, key)
    end

    alias include? has_key?
    alias key? has_key?
    alias member? has_key?

    def [](key)
      super
    rescue PyError
      nil
    end

    def delete(key)
      v = self[key]
      LibPython::Helpers.delitem(__pyptr__, key)
      v
    end

    def each(&block)
      return enum_for unless block_given?
      LibPython::Helpers.dict_each(__pyptr__, &block)
      self
    end

    def to_h
      inject({}) do |h, (k, v)|
        h.update(k => v)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pycall-1.5.1 lib/pycall/dict.rb
pycall-1.5.0 lib/pycall/dict.rb
pycall-1.4.2 lib/pycall/dict.rb
pycall-1.4.1 lib/pycall/dict.rb
pycall-1.4.0 lib/pycall/dict.rb
pycall-1.3.1 lib/pycall/dict.rb