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

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.

Examples:

h = { "a" => "b", "c" => "d" }
h.each_key! {|key| key.upcase }
h => { "A" => "b", "C" => "d" }

Returns:

  • self



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.

Examples:

h = { "a" => "b", "c" => "d" }
h.each_pair! {|key,value| key.upcase, value.upcase }
h => { "A" => "B", "C" => "D" }

Returns:

  • self



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_sortObject

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.

Examples:

h = { "c" => "d", "a" => "b" }
h.each_sort {|key, val| ... }
=> calls the block with "a" => "b", then with "c" => "d"


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.

Examples:

h = { "a" => "b", "c" => "d" }
h.each_value! {|value| value.upcase }
h => { "a" => "B", "c" => "d" }

Returns:

  • self



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_keyself.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.

Examples:

h = {"a" => "b"}
h.map_key{|key| key.upcase }
=> {"A" => "b"}

Returns:

  • (self.class)

    a new object with the mapped keys and existing values.



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_pairself.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.

Examples:

h = {"a" => "b"}
h.map_pair{|key, value| key.upcase, value.upcase }
=> {"A" => "B"}

Returns:

  • (self.class)

    a new object with the mapped keys and mapped values.



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_valueself.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.

Examples:

h = {"a" => "b"}
h.map_value{|value| value.upcase }
=> {"a" => "B"}

Returns:

  • (self.class)

    a new object with the existing keys and mapped values.



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_pairObject

Calls block once for each key-value pair in self, passing the key and value as paramters to the block.

Examples:

h = {"a"=>"b", "c"=>"d", "e"=>"f" }
h.map_pair{|key,value| key+value }
=> ["ab","cd","ef"]


173
174
175
# File 'lib/sixarm_ruby_ramp/pairable.rb', line 173

def yield_pair
  keys.map{|key| yield key, self[key] }
end