Class: SortedHash
- Inherits:
-
Hash
- Object
- Hash
- SortedHash
- Defined in:
- lib/wakame/util.rb
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
- #clear ⇒ Object
- #delete(key, &blk) ⇒ Object
- #each(&blk) ⇒ Object
- #first ⇒ Object
- #first_key ⇒ Object
-
#initialize ⇒ SortedHash
constructor
A new instance of SortedHash.
- #inspect ⇒ Object
- #invert ⇒ Object
- #keys ⇒ Object
- #last ⇒ Object
- #last_key ⇒ Object
- #store(key, value) ⇒ Object
Constructor Details
#initialize ⇒ SortedHash
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 |
#clear ⇒ Object
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 |
#first ⇒ Object
400 401 402 |
# File 'lib/wakame/util.rb', line 400 def first self[first_key] end |
#first_key ⇒ Object
396 397 398 |
# File 'lib/wakame/util.rb', line 396 def first_key @keyorder.first end |
#inspect ⇒ Object
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 |
#invert ⇒ Object
424 425 426 |
# File 'lib/wakame/util.rb', line 424 def invert raise NotImplementedError end |
#keys ⇒ Object
386 387 388 |
# File 'lib/wakame/util.rb', line 386 def keys @keyorder end |
#last ⇒ Object
408 409 410 |
# File 'lib/wakame/util.rb', line 408 def last self[last_key] end |
#last_key ⇒ Object
404 405 406 |
# File 'lib/wakame/util.rb', line 404 def last_key @keyorder.last end |
#store(key, value) ⇒ Object
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 |