Class: OrderedHash
Overview
This is a hack from blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/20551 But seriously folks, just for fuck sake upgrade your damn Ruby finally :/ I shall drop 1.8 support at some point, not very soon, but it’s a matter of months, not years, OK?
Instance Attribute Summary collapse
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
Instance Method Summary collapse
- #[]=(key, val) ⇒ Object
- #delete(key) ⇒ Object
- #each ⇒ Object
- #each_key ⇒ Object
- #each_pair ⇒ Object
- #each_value ⇒ Object
-
#initialize ⇒ OrderedHash
constructor
A new instance of OrderedHash.
- #merge(hash) ⇒ Object
- #select(&block) ⇒ Object
- #store ⇒ Object
Constructor Details
#initialize ⇒ OrderedHash
Returns a new instance of OrderedHash.
10 11 12 |
# File 'lib/ordered_hash_1.8.rb', line 10 def initialize @keys = [] end |
Instance Attribute Details
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
9 10 11 |
# File 'lib/ordered_hash_1.8.rb', line 9 def keys @keys end |
Instance Method Details
#[]=(key, val) ⇒ Object
14 15 16 17 |
# File 'lib/ordered_hash_1.8.rb', line 14 def []=(key, val) @keys << key super end |
#delete(key) ⇒ Object
19 20 21 22 |
# File 'lib/ordered_hash_1.8.rb', line 19 def delete(key) @keys.delete(key) super end |
#each ⇒ Object
24 25 26 |
# File 'lib/ordered_hash_1.8.rb', line 24 def each @keys.each { |k| yield k, self[k] } end |
#each_key ⇒ Object
28 29 30 |
# File 'lib/ordered_hash_1.8.rb', line 28 def each_key @keys.each { |k| yield k } end |
#each_pair ⇒ Object
7 |
# File 'lib/ordered_hash_1.8.rb', line 7 alias_method :each_pair, :each |
#each_value ⇒ Object
32 33 34 |
# File 'lib/ordered_hash_1.8.rb', line 32 def each_value @keys.each { |k| yield self[k] } end |
#merge(hash) ⇒ Object
36 37 38 39 |
# File 'lib/ordered_hash_1.8.rb', line 36 def merge(hash) @keys.push(*hash.keys) super(hash) end |
#select(&block) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/ordered_hash_1.8.rb', line 41 def select(&block) self.inject(self.class.new) do |buffer, pair| if block.call(*pair) buffer.merge(pair[0] => pair[1]) end buffer end end |
#store ⇒ Object
6 |
# File 'lib/ordered_hash_1.8.rb', line 6 alias_method :store, :[]= |