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

Returns the value of attribute default_proc.



36
37
38
# File 'lib/rufus/tokyo/hmethods.rb', line 36

def default_proc
  @default_proc
end

Instance Method Details

#[](k) ⇒ Object

The [] methods

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



42
43
44
45
46
47
48
49
50
# File 'lib/rufus/tokyo/hmethods.rb', line 42

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

Returns the default value, the value that would be returned by h if k did not exist among h keys.



133
134
135
136
137
138
# File 'lib/rufus/tokyo/hmethods.rb', line 133

def default (key=nil)

  return nil unless @default_proc

  @default_proc.call(self, key) rescue nil
end

#default=(val) ⇒ Object

Sets the default value for the Hash.

Warning : use #default_proc= if you want to change the default_proc directly.



145
146
147
148
# File 'lib/rufus/tokyo/hmethods.rb', line 145

def default= (val)

  @default_proc = val.nil? ? nil : lambda { |h, k| val }
end

#eachObject

Our classical ‘each’



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rufus/tokyo/hmethods.rb', line 61

def each
  #
  # drop to Edo's C API calls to avoid two-step iteration
  # (keys() then each())
  #
  if defined?(@db) and %w[iterinit iternext].all? { |m| @db.respond_to?(m) }
    @db.iterinit
    while k = @db.iternext
      yield(k, self[k])
    end
  #
  # drop to Tokyo's FFI calls to avoid two-step iteration
  # (keys() then each())
  #
  elsif self.class.name != "Rufus::Tokyo::Table" and # use String for Edo
        defined?(@db)                            and
        respond_to?(:lib)                        and
        %w[abs_iterinit abs_iternext].all? { |m| lib.respond_to?(m) }
    begin
      lib.abs_iterinit(@db)
      int = FFI::MemoryPointer.new(:int)
      loop do
        key_pointer = lib.abs_iternext(@db, int)
        break if key_pointer.address.zero?
        k = key_pointer.get_bytes(0, int.get_int(0))
        yield(k, self[k])
      end
    ensure
      int.free if int
    end
  # we couldn't do it fast, so go ahead with slow-but-accurate
  else
    keys.each { |k| yield(k, self[k]) }
  end
end

#merge(h) ⇒ Object

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



116
117
118
119
# File 'lib/rufus/tokyo/hmethods.rb', line 116

def merge (h)

  self.to_h.merge(h)
end

#merge!(h) ⇒ Object

Merges the entries in the given hash into this map



123
124
125
126
127
128
# File 'lib/rufus/tokyo/hmethods.rb', line 123

def merge! (h)

  h.each { |k, v| self[k] = v }

  self
end

#to_aObject

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



106
107
108
109
110
111
112
# File 'lib/rufus/tokyo/hmethods.rb', line 106

def to_a

  #self.collect { |e| e }
    # not OK with ruby 1.9.1

  self.inject([]) { |a, (k, v)| a << [ k, v ]; a }
end

#to_hObject

Turns this instance into a Ruby hash



99
100
101
102
# File 'lib/rufus/tokyo/hmethods.rb', line 99

def to_h

  self.inject({}) { |h, (k, v)| h[k] = v; h }
end

#valuesObject

Returns an array of all the values



54
55
56
57
# File 'lib/rufus/tokyo/hmethods.rb', line 54

def values

  collect { |k, v| v }
end