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
131 132 133 134 135 |
# File 'lib/curio.rb', line 131 def <<(item) key = coerce_key item.send(key_method) @map[key] = item self end |
#[](key) ⇒ object
Get an item from the collection
161 162 163 |
# File 'lib/curio.rb', line 161 def [](key) fetch key, nil end |
#fetch(key, *args) {|undefined| ... } ⇒ object
Fetch an item from the collection
147 148 149 150 151 152 |
# File 'lib/curio.rb', line 147 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
109 110 111 112 |
# File 'lib/curio.rb', line 109 def initialize(*) @map = { } super end |
#key?(key) ⇒ Boolean Also known as: has?
Check if collection has an item with specified key
172 173 174 |
# File 'lib/curio.rb', line 172 def key?(key) @map.key? coerce_key(key) end |
#to_h ⇒ Hash
Returns a hash representation of the collection
182 183 184 |
# File 'lib/curio.rb', line 182 def to_h @map end |
#values ⇒ Array Also known as: all
Get all the items in collection
95 |
# File 'lib/curio.rb', line 95 def_delegators :values, :each |