Class: Hash

Inherits:
Object show all
Defined in:
lib/builtinME.rb

Instance Method Summary collapse

Instance Method Details

#eachObject Also known as: each_pair



244
245
246
247
# File 'lib/builtinME.rb', line 244

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

#each_keyObject



249
250
251
252
# File 'lib/builtinME.rb', line 249

def each_key
    ks = keys
    ks.each {|k| yield(k)}
end

#each_valueObject



254
255
256
257
# File 'lib/builtinME.rb', line 254

def each_value
    vs = values
    vs.each {|k| yield(k)}
end

#empty?Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/builtinME.rb', line 259

def empty?
    length == 0
end

#index(value) ⇒ Object



310
311
312
313
# File 'lib/builtinME.rb', line 310

def index value
    each {|k, v| return k if value == v }
    return nil
end

#inspectObject Also known as: to_s



276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/builtinME.rb', line 276

def inspect
    r = '{'
    is_first = true
    each_pair do |k, v|
        if !is_first
            r << ", "
        end
        is_first = false
        r << k.inspect
        r << '=>'
        r << v.inspect
    end
    r << '}'
end

#invertObject



293
294
295
296
297
# File 'lib/builtinME.rb', line 293

def invert
    h = {}
    each {|k, v| h[v] = k}
    h
end

#merge(other) ⇒ Object



306
307
308
# File 'lib/builtinME.rb', line 306

def merge other
    clone.merge!(other)
end

#to_aObject



265
266
267
268
269
270
271
272
273
274
# File 'lib/builtinME.rb', line 265

def to_a
    res = []
    each_pair do |k, v|
      item = []
      item << k
      item << v
      res << item
    end
    res
end

#update(other) ⇒ Object Also known as: merge!



299
300
301
302
# File 'lib/builtinME.rb', line 299

def update other
    other.each {|k, v| self[k] = v}
    self
end