Module: Rufus::Tokyo::HashMethods

Includes:
Enumerable
Included in:
Edo::CabinetCore, Edo::TableCore, Cabinet, Map, Table
Defined in:
lib/rufus/tokyo/hmethods.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_procObject (readonly)

Returns the value of attribute default_proc.



106
107
108
# File 'lib/rufus/tokyo/hmethods.rb', line 106

def default_proc
  @default_proc
end

Instance Method Details

#[](k) ⇒ Object

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(key = nil) ⇒ Object



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=(val) ⇒ Object



101
102
103
104
# File 'lib/rufus/tokyo/hmethods.rb', line 101

def default= (val)

  @default_proc = lambda { |h, k| val }
end

#eachObject

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(h) ⇒ Object

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!(h) ⇒ Object

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_aObject

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_hObject

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

#valuesObject

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