Module: Rufus::Tokyo::HashMethods
Included Modules
Enumerable
A mixin for Cabinet and Map, gathers all the hash-like methods
Attributes
Instance Attributes
| default_proc | [R] | public |
Returns the value of attribute default_proc. |
|---|
Public Visibility
Public Instance Method Summary
| #[](k) |
The [] methods. |
|---|---|
| #default(key = nil) | |
| #default=(val) | |
| #each |
Our classical ‘each’. |
| #merge(h) |
Returns a new Ruby hash which is a merge of this Map and the given hash. |
| #merge!(h) |
Merges the entries in the given hash into this map. |
| #to_a |
Turns this instance into an array of [ key, value ]. |
| #to_h |
Turns this instance into a Ruby hash. |
| #values |
Returns an array of all the values. |
Public Instance Method Details
[]
The [] methods
(assumes there’s an underlying get(k) method)
40 41 42 43 44 45 46 47 48 |
# File 'lib/rufus/tokyo/hmethods.rb', line 40 def [] (k) val = get(k) return val unless val.nil? return nil unless @default_proc @default_proc.call(self, k) end |
default
94 95 96 97 98 99 |
# File 'lib/rufus/tokyo/hmethods.rb', line 94 def default (key=nil) val = self[key] val.nil? ? @default_proc.call(self, key) : val end |
default=
101 102 103 104 |
# File 'lib/rufus/tokyo/hmethods.rb', line 101 def default= (val) @default_proc = lambda { |h, k| val } end |
each
Our classical ‘each’
59 60 61 62 |
# File 'lib/rufus/tokyo/hmethods.rb', line 59 def each keys.each { |k| yield(k, self[k]) } end |
merge
Returns a new Ruby hash which is a merge of this Map and the given hash
80 81 82 83 |
# File 'lib/rufus/tokyo/hmethods.rb', line 80 def merge (h) self.to_h.merge(h) end |
merge!
Merges the entries in the given hash into this map
87 88 89 90 91 92 |
# File 'lib/rufus/tokyo/hmethods.rb', line 87 def merge! (h) h.each { |k, v| self[k] = v } self end |
to_a
Turns this instance into an array of [ key, value ]
73 74 75 76 |
# File 'lib/rufus/tokyo/hmethods.rb', line 73 def to_a self.collect { |e| e } end |
to_h
Turns this instance into a Ruby hash
66 67 68 69 |
# File 'lib/rufus/tokyo/hmethods.rb', line 66 def to_h self.inject({}) { |h, (k, v)| h[k] = v; h } end |
values
Returns an array of all the values
52 53 54 55 |
# File 'lib/rufus/tokyo/hmethods.rb', line 52 def values collect { |k, v| v } end |