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

[]

public [](k)

The [] methods

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

[View source]


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

public default(key = nil)
[View source]


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=

public default=(val)
[View source]


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

def default= (val)

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

each

public each

Our classical ‘each’

[View source]


59
60
61
62
# File 'lib/rufus/tokyo/hmethods.rb', line 59

def each

  keys.each { |k| yield(k, self[k]) }
end

merge

public merge(h)

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

[View source]


80
81
82
83
# File 'lib/rufus/tokyo/hmethods.rb', line 80

def merge (h)

  self.to_h.merge(h)
end

merge!

public merge!(h)

Merges the entries in the given hash into this map

[View source]


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

public to_a

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

[View source]


73
74
75
76
# File 'lib/rufus/tokyo/hmethods.rb', line 73

def to_a

  self.collect { |e| e }
end

to_h

public to_h

Turns this instance into a Ruby hash

[View source]


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

public values

Returns an array of all the values

[View source]


52
53
54
55
# File 'lib/rufus/tokyo/hmethods.rb', line 52

def values

  collect { |k, v| v }
end
Generated on Friday, September 18 2009 at 10:35:42 AM by YARD 0.2.3.5 (ruby-1.8.7).