Class: Deck
- Inherits:
-
Object
- Object
- Deck
- Includes:
- Enumerable
- Defined in:
- lib/deck.rb,
lib/deck/deck.rb,
lib/deck/version.rb
Direct Known Subclasses
Constant Summary collapse
- SUITS =
%w(Spades Hearts Diamonds Clubs)
- RANKS =
%w(Ace 2 3 4 5 6 7 8 9 10 Jack Queen King)
- VERSION =
"1.1.2"
Instance Attribute Summary collapse
-
#cards ⇒ Object
readonly
Returns the value of attribute cards.
Instance Method Summary collapse
- #<<(card) ⇒ Object
- #draw ⇒ Object
- #each ⇒ Object
-
#initialize(options = {}) ⇒ Deck
constructor
create a new deck of cards.
- #inspect ⇒ Object
- #shuffle! ⇒ Object
- #size ⇒ Object (also: #length)
Constructor Details
#initialize(options = {}) ⇒ Deck
create a new deck of cards
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/deck/deck.rb', line 8 def initialize( = {}) defaults = { :jokers => false } = defaults.merge() @cards = [] Deck::SUITS.each{|s| Deck::RANKS.each{|r| self << Card.new(r,s) } } if [:jokers] self << Card.new self << Card.new end end |
Instance Attribute Details
#cards ⇒ Object (readonly)
Returns the value of attribute cards.
5 6 7 |
# File 'lib/deck/deck.rb', line 5 def cards @cards end |
Instance Method Details
#<<(card) ⇒ Object
42 43 44 |
# File 'lib/deck/deck.rb', line 42 def <<(card) @cards << card end |
#draw ⇒ Object
53 54 55 |
# File 'lib/deck/deck.rb', line 53 def draw cards.shift end |
#each ⇒ Object
38 39 40 |
# File 'lib/deck/deck.rb', line 38 def each @cards.each { |card| yield card } end |
#inspect ⇒ Object
34 35 36 |
# File 'lib/deck/deck.rb', line 34 def inspect to_s end |
#shuffle! ⇒ Object
30 31 32 |
# File 'lib/deck/deck.rb', line 30 def shuffle! 1000.times { @cards.push @cards.delete_at(rand(size-1)) } end |
#size ⇒ Object Also known as: length
47 48 49 |
# File 'lib/deck/deck.rb', line 47 def size @cards.size end |