Class: Thor::OrderedHash
- Inherits:
-
Object
- Object
- Thor::OrderedHash
- Includes:
- Enumerable
- Defined in:
- lib/thor/lib/thor/ordered_hash.rb
Overview
This class is based on the Ruby 1.9 ordered hashes. It keeps the semantics and most of the efficiency of normal hashes while also keeping track of the order in which elements were set.
Direct Known Subclasses
Defined Under Namespace
Classes: Node
Instance Method Summary collapse
- #+(other) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #each {|[@first.key, @first.value]| ... } ⇒ Object
-
#initialize ⇒ OrderedHash
constructor
A new instance of OrderedHash.
- #initialize_copy(other) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize ⇒ OrderedHash
Returns a new instance of OrderedHash.
9 10 11 |
# File 'lib/thor/lib/thor/ordered_hash.rb', line 9 def initialize @hash = {} end |
Instance Method Details
#+(other) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/thor/lib/thor/ordered_hash.rb', line 56 def +(other) new = clone other.each do |key, value| new[key] = value unless self[key] end new end |
#[](key) ⇒ Object
17 18 19 |
# File 'lib/thor/lib/thor/ordered_hash.rb', line 17 def [](key) @hash[key] && @hash[key].value end |
#[]=(key, value) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/thor/lib/thor/ordered_hash.rb', line 21 def []=(key, value) node = Node.new(key, value) if old = @hash[key] if old.prev old.prev.next = old.next else # old is @first and @last @first = @last = nil end end if @first.nil? @first = @last = node else node.prev = @last @last.next = node @last = node end @hash[key] = node value end |
#each {|[@first.key, @first.value]| ... } ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/thor/lib/thor/ordered_hash.rb', line 44 def each return unless @first yield [@first.key, @first.value] node = @first yield [node.key, node.value] while node = node.next self end |
#initialize_copy(other) ⇒ Object
13 14 15 |
# File 'lib/thor/lib/thor/ordered_hash.rb', line 13 def initialize_copy(other) @hash = other.instance_variable_get('@hash').clone end |
#values ⇒ Object
52 53 54 |
# File 'lib/thor/lib/thor/ordered_hash.rb', line 52 def values self.map { |k, v| v } end |