Module Rufus::Tokyo::HashMethods

  1. lib/rufus/tokyo/hmethods.rb

A mixin for Cabinet and Map, gathers all the hash-like methods

Methods

public instance

  1. []
  2. default
  3. default=
  4. each
  5. merge
  6. merge!
  7. to_a
  8. to_h
  9. values

Included modules

  1. Enumerable

Attributes

default_proc [R]

Public instance methods

[] (k)

The [] methods

(assumes there’s an underlying get(k) method)

[show source]
    # File lib/rufus/tokyo/hmethods.rb, line 40
40:     def [] (k)
41: 
42:       val = get(k)
43: 
44:       return val unless val.nil?
45:       return nil unless @default_proc
46: 
47:       @default_proc.call(self, k)
48:     end
default (key=nil)
[show source]
    # File lib/rufus/tokyo/hmethods.rb, line 94
94:     def default (key=nil)
95: 
96:       val = self[key]
97: 
98:       val.nil? ? @default_proc.call(self, key) : val
99:     end
default= (val)
[show source]
     # File lib/rufus/tokyo/hmethods.rb, line 101
101:     def default= (val)
102: 
103:       @default_proc = lambda { |h, k| val }
104:     end
each () {|k, self[k]| ...}

Our classical ‘each’

[show source]
    # File lib/rufus/tokyo/hmethods.rb, line 59
59:     def each
60: 
61:       keys.each { |k| yield(k, self[k]) }
62:     end
merge (h)

Returns a new Ruby hash which is a merge of this Map and the given hash

[show source]
    # File lib/rufus/tokyo/hmethods.rb, line 80
80:     def merge (h)
81: 
82:       self.to_h.merge(h)
83:     end
merge! (h)

Merges the entries in the given hash into this map

[show source]
    # File lib/rufus/tokyo/hmethods.rb, line 87
87:     def merge! (h)
88: 
89:       h.each { |k, v| self[k] = v }
90: 
91:       self
92:     end
to_a ()

Turns this instance into an array of [ key, value ]

[show source]
    # File lib/rufus/tokyo/hmethods.rb, line 73
73:     def to_a
74: 
75:       self.collect { |e| e }
76:     end
to_h ()

Turns this instance into a Ruby hash

[show source]
    # File lib/rufus/tokyo/hmethods.rb, line 66
66:     def to_h
67: 
68:       self.inject({}) { |h, (k, v)| h[k] = v; h }
69:     end
values ()

Returns an array of all the values

[show source]
    # File lib/rufus/tokyo/hmethods.rb, line 52
52:     def values
53: 
54:       collect { |k, v| v }
55:     end