class PyBind::PyDict

Public Class Methods

new(init = nil) click to toggle source
Calls superclass method PyBind::PyObjectWrapper::new
# File lib/pybind/types/dict.rb, line 7
def self.new(init = nil)
  case init
  when PyObjectStruct
    super
  when nil
    new(LibPython.PyDict_New())
  when Hash
    new.tap do |dict|
      init.each do |key, value|
        dict[key] = value
      end
    end
  else
    raise TypeError, "the argument must be a PyObjectStruct or a Hash"
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/pybind/types/dict.rb, line 24
def [](key)
  case key
  when String, Symbol
    LibPython.PyDict_GetItemString(@pystruct, key.to_s).to_ruby
  else
    key = key.to_python
    LibPython.PyDict_GetItem(@pystruct, key).to_ruby
  end
end
[]=(key, value) click to toggle source
# File lib/pybind/types/dict.rb, line 34
def []=(key, value)
  value = value.to_python
  case key
  when String, Symbol
    LibPython.PyDict_SetItemString(@pystruct, key.to_s, value)
  else
    key = key.to_python
    LibPython.PyDict_SetItem(@pystruct, key, value)
  end
  value
end
delete(key) click to toggle source
# File lib/pybind/types/dict.rb, line 46
def delete(key)
  case key
  when String, Symbol
    value = LibPython.PyDict_GetItemString(@pystruct, key).to_ruby
    LibPython.PyDict_DelItemString(@pystruct, key.to_s)
  else
    key = key.to_python
    value = LibPython.PyDict_GetItem(@pystruct, key).to_ruby
    LibPython.PyDict_DelItem(@pystruct, key)
  end
  value
end
each() { |key, self| ... } click to toggle source
# File lib/pybind/types/dict.rb, line 86
def each
  return enum_for unless block_given?
  keys.each do |key|
    yield key, self[key]
  end
  self
end
has_key?(key) click to toggle source
# File lib/pybind/types/dict.rb, line 71
def has_key?(key)
  key = key.to_python
  value = LibPython.PyDict_Contains(@pystruct, key)
  raise PyError.fetch if value == -1
  value == 1
end
keys() click to toggle source
# File lib/pybind/types/dict.rb, line 63
def keys
  LibPython.PyDict_Keys(@pystruct).to_ruby
end
size() click to toggle source
# File lib/pybind/types/dict.rb, line 59
def size
  LibPython.PyDict_Size(@pystruct)
end
to_a() click to toggle source
# File lib/pybind/types/dict.rb, line 78
def to_a
  LibPython.PyDict_Items(@pystruct).to_ruby
end
to_hash() click to toggle source
# File lib/pybind/types/dict.rb, line 82
def to_hash
  Hash[to_a]
end
values() click to toggle source
# File lib/pybind/types/dict.rb, line 67
def values
  LibPython.PyDict_Values(@pystruct).to_ruby
end