Class: SortedHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/wakame/util.rb

Instance Method Summary collapse

Constructor Details

#initializeSortedHash

Returns a new instance of SortedHash.



357
358
359
# File 'lib/wakame/util.rb', line 357

def initialize
  @keyorder=[]
end

Instance Method Details

#[]=(key, value) ⇒ Object



375
376
377
# File 'lib/wakame/util.rb', line 375

def []=(key, value)
  store(key, value)
end

#clearObject



412
413
414
415
# File 'lib/wakame/util.rb', line 412

def clear
  super
  @keyorder.clear
end

#delete(key, &blk) ⇒ Object



379
380
381
382
383
384
# File 'lib/wakame/util.rb', line 379

def delete(key, &blk)
  if has_key?(key)
    @keyorder.delete(key)
    super(key, &blk)
  end
end

#each(&blk) ⇒ Object



390
391
392
393
394
# File 'lib/wakame/util.rb', line 390

def each(&blk)
  @keyorder.each { |k|
    blk.call(k, self[k])
  }
end

#firstObject



400
401
402
# File 'lib/wakame/util.rb', line 400

def first
  self[first_key]
end

#first_keyObject



396
397
398
# File 'lib/wakame/util.rb', line 396

def first_key
  @keyorder.first
end

#inspectObject



417
418
419
420
421
422
# File 'lib/wakame/util.rb', line 417

def inspect
  str = "{"
  str << @keyorder.collect{|k| "#{k}=>#{self[k]}" }.join(', ')
  str << "}"
  str
end

#invertObject

Raises:

  • (NotImplementedError)


424
425
426
# File 'lib/wakame/util.rb', line 424

def invert
  raise NotImplementedError
end

#keysObject



386
387
388
# File 'lib/wakame/util.rb', line 386

def keys
  @keyorder
end

#lastObject



408
409
410
# File 'lib/wakame/util.rb', line 408

def last
  self[last_key]
end

#last_keyObject



404
405
406
# File 'lib/wakame/util.rb', line 404

def last_key
  @keyorder.last
end

#store(key, value) ⇒ Object

Raises:

  • (TypeError)


362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/wakame/util.rb', line 362

def store(key, value)
  raise TypeError, "#{key} is not Comparable" unless key.kind_of?(Comparable)
  if has_key?(key)
    ret = super(key, value)
  else
    ret = super(key, value)
    @keyorder << key
    @keyorder.sort!
    
  end
  ret
end