Class: PyCall::Dict

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pycall/dict.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(h) ⇒ Object



8
9
10
# File 'lib/pycall/dict.rb', line 8

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

Instance Method Details

#[](key) ⇒ Object



24
25
26
27
28
# File 'lib/pycall/dict.rb', line 24

def [](key)
  super
rescue PyError
  nil
end

#delete(key) ⇒ Object



30
31
32
33
34
# File 'lib/pycall/dict.rb', line 30

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

#each(&block) ⇒ Object



36
37
38
39
40
# File 'lib/pycall/dict.rb', line 36

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

#has_key?(key) ⇒ Boolean Also known as: include?, key?, member?

Returns:

  • (Boolean)


16
17
18
# File 'lib/pycall/dict.rb', line 16

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

#lengthObject



12
13
14
# File 'lib/pycall/dict.rb', line 12

def length
  PyCall.len(self)
end

#to_hObject



42
43
44
45
46
# File 'lib/pycall/dict.rb', line 42

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