Class: Shithead::SortedStack

Inherits:
Stack
  • Object
show all
Defined in:
lib/shithead/sorted_stack.rb

Instance Attribute Summary

Attributes inherited from Stack

#sets

Instance Method Summary collapse

Methods inherited from Stack

#cards, #clearable?, #empty?, #initialize, #release_to, #size, #top

Constructor Details

This class inherits a constructor from Shithead::Stack

Instance Method Details

#add(card) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/shithead/sorted_stack.rb', line 2

def add(card)
  if set_for(card.value)
    set_for(card.value).add card
  else
    sets << Shithead::Set.new([card])
    sets.replace sets.sort_by(&:rank).reverse
  end
end

#delete(card) ⇒ Object



11
12
13
14
15
16
# File 'lib/shithead/sorted_stack.rb', line 11

def delete(card)
  set = set_for(card.value)
  set.delete card

  sets.delete set if set.empty?
end

#take(set) ⇒ Object



18
19
20
21
# File 'lib/shithead/sorted_stack.rb', line 18

def take(set)
  sets.delete set
  set
end

#to_sObject



23
24
25
# File 'lib/shithead/sorted_stack.rb', line 23

def to_s
  sets.collect(&:to_s).join(" ")
end