Module: Pairable
- Included in:
- Hash
- Defined in:
- lib/sixarm_ruby_ramp/pairable.rb
Overview
Extensions for a class that provides key-value pairs.
The including class must provide these methods:
* obj[key] → value
* obj[key] = value
* keys → enumerable
Instance Method Summary collapse
-
#each_key! ⇒ Object
Call block once for each key-value pair in self, passing the key as a parameter, and updating it in place.
-
#each_pair! ⇒ Object
Call block once for each key-value pair in self, passing the key and value as parameters, and updating it in place.
-
#each_sort ⇒ Object
Call block once for each key-value pair in self, passing the key and value to the block as a two-element array.
-
#each_value! ⇒ Object
Call block once for each key-value pair in self, passing the value as a parameter, and updating it in place.
-
#map_key ⇒ self.class
Call block once for each key-pair value in self, passing the key as a parameter to the block, and mapping it to a result.
-
#map_pair ⇒ self.class
Call block once for each key-value pair in self, passing the key and value as parameters to the block.
-
#map_value ⇒ self.class
Call block once for each key-value pair in self, passing the value as a parameter to the block, and mapping it to a result.
-
#yield_pair ⇒ Object
Calls block once for each key-value pair in self, passing the key and value as paramters to the block.
Instance Method Details
#each_key! ⇒ Object
Call block once for each key-value pair in self, passing the key as a parameter, and updating it in place.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sixarm_ruby_ramp/pairable.rb', line 39 def each_key! replacements=[] keys.each{|key| value=self[key] key2=yield(key) if key===key2 #nop else replacements << [key,key2,value] end } replacements.each{|key,key2,value| self.delete(key) self[key2]=value } return self end |
#each_pair! ⇒ Object
Call block once for each key-value pair in self, passing the key and value as parameters, and updating it in place.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/sixarm_ruby_ramp/pairable.rb', line 92 def each_pair! replacements=[] keys.each{|key| value=self[key] key2,value2=yield(key,value) if key===key2 if value===value2 #nop else self[key]=value2 end else replacements << [key,key2,value2] end } replacements.each{|key,key2,value2| self.delete(key) self[key2]=value2 } return self end |
#each_sort ⇒ Object
Call block once for each key-value pair in self, passing the key and value to the block as a two-element array.
The keys are sorted.
24 25 26 |
# File 'lib/sixarm_ruby_ramp/pairable.rb', line 24 def each_sort keys.sort.each{|key| yield key, self[key] } end |
#each_value! ⇒ Object
Call block once for each key-value pair in self, passing the value as a parameter, and updating it in place.
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/sixarm_ruby_ramp/pairable.rb', line 68 def each_value! keys.each{|key| value=self[key] value2=yield(value) if value===value2 #nop else self[key]=yield(value) end } return self end |
#map_key ⇒ self.class
Call block once for each key-pair value in self, passing the key as a parameter to the block, and mapping it to a result.
125 126 127 128 129 |
# File 'lib/sixarm_ruby_ramp/pairable.rb', line 125 def map_key result = self.class.new keys.each{|key| result[yield key] = self[key]} result end |
#map_pair ⇒ self.class
Call block once for each key-value pair in self, passing the key and value as parameters to the block. and mapping these to a result.
159 160 161 162 163 |
# File 'lib/sixarm_ruby_ramp/pairable.rb', line 159 def map_pair result = self.class.new keys.each{|key| k, v = yield key, self[key]; result[k] = v} result end |
#map_value ⇒ self.class
Call block once for each key-value pair in self, passing the value as a parameter to the block, and mapping it to a result.
142 143 144 145 146 |
# File 'lib/sixarm_ruby_ramp/pairable.rb', line 142 def map_value result = self.class.new keys.each{|key| result[key] = yield self[key]} result end |
#yield_pair ⇒ Object
Calls block once for each key-value pair in self, passing the key and value as paramters to the block.
173 174 175 |
# File 'lib/sixarm_ruby_ramp/pairable.rb', line 173 def yield_pair keys.map{|key| yield key, self[key] } end |