Class: Card
Overview
One of 52 of cards that are used to play card games.
A card is comprised of one rank and one suit, with 13 ranks and 4 suits there are 52 unique playing cards. A card can be created in one of two ways:
The first method uses the Rank and Suit enumerators card = Card.new(Rank::KING, Suit::HEART)
The second method uses rank and suit values. card = Card.new(“KH”)
Rank and suit values are case insensitive, so these work as well card = Card.new(“Kh”) card = Card.new(“kH”)
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#rank ⇒ Object
readonly
Returns the value of attribute rank.
-
#suit ⇒ Object
readonly
Returns the value of attribute suit.
-
#uid ⇒ Object
readonly
Returns the value of attribute uid.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(card) ⇒ Object
- #==(card) ⇒ Object
- #eql?(card) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(*args) ⇒ Card
constructor
A new instance of Card.
- #to_s ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(*args) ⇒ Card
Returns a new instance of Card.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rora/model/card.rb', line 22 def initialize(*args) if(args.size == 2) @rank = args[0] @suit = args[1] end if(args.size == 1) raise ArgumentError, "#{args[0]} is an invalid card sequence" if args[0].length != 2 @rank = Rank.get(args[0][0].chr) @suit = Suit.get(args[0][1].chr) end @key = @rank.key + @suit.key @card_repository = CardRepository.instance @uid = @card_repository.get(@key) end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
20 21 22 |
# File 'lib/rora/model/card.rb', line 20 def key @key end |
#rank ⇒ Object (readonly)
Returns the value of attribute rank.
20 21 22 |
# File 'lib/rora/model/card.rb', line 20 def rank @rank end |
#suit ⇒ Object (readonly)
Returns the value of attribute suit.
20 21 22 |
# File 'lib/rora/model/card.rb', line 20 def suit @suit end |
#uid ⇒ Object (readonly)
Returns the value of attribute uid.
20 21 22 |
# File 'lib/rora/model/card.rb', line 20 def uid @uid end |
Class Method Details
.to_cards(string) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/rora/model/card.rb', line 59 def self.to_cards string cards = Array.new if !string.include?(",") && !string.include?(" ") string.scan(/../).each { |chars| cards << Card.new(chars) } else string.split(string.include?(",") ? "," : " ").each { |chars| cards << Card.new(chars) } end cards end |
Instance Method Details
#<=>(card) ⇒ Object
37 38 39 40 |
# File 'lib/rora/model/card.rb', line 37 def <=>(card) return self.rank <=> card.rank if card.rank != self.rank self.suit <=> card.suit end |
#==(card) ⇒ Object
50 51 52 53 |
# File 'lib/rora/model/card.rb', line 50 def == card return false if !card.kind_of? Card uid == card.uid end |
#eql?(card) ⇒ Boolean
46 47 48 |
# File 'lib/rora/model/card.rb', line 46 def eql? card self == card end |
#hash ⇒ Object
55 56 57 |
# File 'lib/rora/model/card.rb', line 55 def hash uid end |
#to_s ⇒ Object
69 70 71 |
# File 'lib/rora/model/card.rb', line 69 def to_s "#{value}" end |
#value ⇒ Object
42 43 44 |
# File 'lib/rora/model/card.rb', line 42 def value "#{@rank.value} of #{@suit.value}s" end |