Module: Curio::Methods
- Extended by:
- Forwardable
- Defined in:
- lib/curio.rb
Overview
The collection methods
Instance Method Summary collapse
-
#<<(item) ⇒ self
Add an item to the collection.
-
#[](key) ⇒ object
Get an item from the collection.
-
#fetch(key, *args) {|undefined| ... } ⇒ object
Fetch an item from the collection.
-
#initialize ⇒ undefined
private
Setup the map and call parent initializer.
-
#key?(key) ⇒ Boolean
(also: #has?)
Check if collection has an item with specified key.
-
#to_h ⇒ Hash
Returns a hash representation of the collection.
-
#values ⇒ Array
(also: #all)
Get all the items in collection.
Instance Method Details
#<<(item) ⇒ self
Add an item to the collection
133 134 135 136 137 |
# File 'lib/curio.rb', line 133 def <<(item) key = coerce_key item.send(key_method) @map[key] = item self end |
#[](key) ⇒ object
Get an item from the collection
163 164 165 |
# File 'lib/curio.rb', line 163 def [](key) fetch key, nil end |
#fetch(key, *args) {|undefined| ... } ⇒ object
Fetch an item from the collection
149 150 151 152 153 154 |
# File 'lib/curio.rb', line 149 def fetch(key, *args, &block) args.unshift coerce_key(key) @map.fetch(*args, &block) rescue KeyError raise NotFoundError, key end |
#initialize ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Setup the map and call parent initializer
111 112 113 114 |
# File 'lib/curio.rb', line 111 def initialize(*) @map = { } super end |
#key?(key) ⇒ Boolean Also known as: has?
Check if collection has an item with specified key
174 175 176 |
# File 'lib/curio.rb', line 174 def key?(key) @map.key? coerce_key(key) end |
#to_h ⇒ Hash
Returns a hash representation of the collection
184 185 186 |
# File 'lib/curio.rb', line 184 def to_h @map end |
#values ⇒ Array Also known as: all
Get all the items in collection
97 |
# File 'lib/curio.rb', line 97 def_delegators :values, :each |