Class: Gems::List
- Inherits:
-
Hash
- Object
- Hash
- Gems::List
- Defined in:
- lib/gems/list.rb
Instance Method Summary collapse
-
#+(other) ⇒ Object
Returns a new Gemlist which is the union of both GemLists.
- #-(other) ⇒ Object
- #each_gem_with_version(&block) ⇒ Object
-
#initialize(hsh = {}) ⇒ List
constructor
A new instance of List.
- #longest_key_length ⇒ Object
Constructor Details
#initialize(hsh = {}) ⇒ List
Returns a new instance of List.
3 4 5 6 7 |
# File 'lib/gems/list.rb', line 3 def initialize(hsh={}) hsh.each do |key, value| self[key] = value end end |
Instance Method Details
#+(other) ⇒ Object
Returns a new Gemlist which is the union of both GemLists.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/gems/list.rb', line 24 def +(other) union = self.dup other.each do |gem, versions| if union.has_key? gem union[gem] = (union[gem] + versions).uniq.sort else union[gem] = versions end end union end |
#-(other) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/gems/list.rb', line 9 def -(other) diff = Gems::List.new self.each do |gem, versions| if other.has_key?(gem) other_versions = other[gem] versions_unique_to_current = versions - other_versions diff[gem] = versions_unique_to_current if versions_unique_to_current.size > 0 else diff[gem] = versions end end diff end |
#each_gem_with_version(&block) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/gems/list.rb', line 36 def each_gem_with_version(&block) raise ArgumentError, 'No block provided' unless block self.each do |gemname, versions| versions.each do |version| block.call(gemname, version) end end end |
#longest_key_length ⇒ Object
45 46 47 |
# File 'lib/gems/list.rb', line 45 def longest_key_length map{|key, value| key.to_s.size}.max || 0 end |