Class: CF::Dictionary
- Includes:
- Enumerable
- Defined in:
- lib/corefoundation/dictionary.rb
Overview
Wrapper class for CFDictionary. It implements Enumerable.
Values returned by the accessor methods or yielded by the block are retained and marked as releasable on garbage collection This means you can safely use the returned values even if the CFDictionary itself has been destroyed
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.mutable ⇒ CF::Dictionary
Return a new, empty mutable CF::Dictionary.
Instance Method Summary collapse
-
#[](key) ⇒ CF::Base?
Returns the value associated with the key, or nil The key should be a CF::Base instance.
-
#[]=(key, value) ⇒ CF::Base
Sets the value associated with the key, or nil The key should be a CF::Base instance.
-
#each ⇒ Object
Iterates over the array yielding the value to the block The value is wrapped in the appropriate CF::Base subclass and retained (but marked for releasing upon garbage collection).
-
#length ⇒ Integer
(also: #size)
Returns the number of key value pairs in the dictionary.
-
#merge!(other) ⇒ Object
Adds the key value pairs from the argument to self.
-
#to_ruby ⇒ Hash
Returns a ruby hash constructed by calling `to_ruby` on each key and value.
Methods inherited from Base
check_cftype, #eql?, #equals?, finalize, #hash, #initialize, #null?, #to_cf, typecast
Methods included from Memory
#inspect, #release, #retain, #to_ptr
Methods included from Register
Constructor Details
This class inherits a constructor from CF::Base
Class Method Details
.mutable ⇒ CF::Dictionary
Return a new, empty mutable CF::Dictionary
48 49 50 |
# File 'lib/corefoundation/dictionary.rb', line 48 def self.mutable new(CF.CFDictionaryCreateMutable nil, 0, CF.kCFTypeDictionaryKeyCallBacks.to_ptr, CF.kCFTypeDictionaryValueCallBacks.to_ptr) end |
Instance Method Details
#[](key) ⇒ CF::Base?
Returns the value associated with the key, or nil The key should be a CF::Base instance. As a convenience, instances of string will be converted to CF::String
69 70 71 72 73 74 |
# File 'lib/corefoundation/dictionary.rb', line 69 def [](key) key = CF::String.from_string(key) if key.is_a?(::String) self.class.check_cftype(key) raw = CF.CFDictionaryGetValue(self, key) raw.null? ? nil : self.class.typecast(raw) end |
#[]=(key, value) ⇒ CF::Base
Sets the value associated with the key, or nil The key should be a CF::Base instance. As a convenience, instances of string will be converted to CF::String
81 82 83 84 85 86 |
# File 'lib/corefoundation/dictionary.rb', line 81 def []=(key, value) key = CF::String.from_string(key) if key.is_a?(::String) self.class.check_cftype(key) self.class.check_cftype(value) CF.CFDictionarySetValue(self, key, value) end |
#each ⇒ Object
Iterates over the array yielding the value to the block The value is wrapped in the appropriate CF::Base subclass and retained (but marked for releasing upon garbage collection)
56 57 58 59 60 61 62 |
# File 'lib/corefoundation/dictionary.rb', line 56 def each callback = lambda do |key, value, _| yield [Base.typecast(key), Base.typecast(value)] end CF.CFDictionaryApplyFunction(self, callback, nil) self end |
#length ⇒ Integer Also known as: size
Returns the number of key value pairs in the dictionary
101 102 103 |
# File 'lib/corefoundation/dictionary.rb', line 101 def length CF.CFDictionaryGetCount(self) end |
#merge!(other) ⇒ Object
Adds the key value pairs from the argument to self
92 93 94 95 96 97 |
# File 'lib/corefoundation/dictionary.rb', line 92 def merge!(other) other.each do |k,v| self[k] = v end self end |
#to_ruby ⇒ Hash
Returns a ruby hash constructed by calling `to_ruby` on each key and value
108 109 110 |
# File 'lib/corefoundation/dictionary.rb', line 108 def to_ruby Hash[collect {|k,v| [k.to_ruby, v.to_ruby]}] end |