Class: Alucard::Card
- Inherits:
-
Object
- Object
- Alucard::Card
- Defined in:
- lib/alucard/card.rb
Constant Summary collapse
- RANKS =
'A23456789TJQK'- SUITS =
'SHCD'- RANK_NAMES =
%w[Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King]
- SUIT_NAMES =
%w[Spades Hearts Clubs Diamonds]
Instance Attribute Summary collapse
-
#rank ⇒ Object
readonly
Returns the value of attribute rank.
-
#suit ⇒ Object
readonly
Returns the value of attribute suit.
Instance Method Summary collapse
-
#initialize(spec) ⇒ Card
constructor
A new instance of Card.
- #to_s ⇒ Object
Constructor Details
#initialize(spec) ⇒ Card
Returns a new instance of Card.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/alucard/card.rb', line 10 def initialize spec case spec when Fixnum @rank, @suit = spec % 13, spec / 13 when /([#{RANKS}])([#{SUITS}])/ @rank, @suit = RANKS.index($1), SUITS.index($2) when /(\w+) of (\w+)/ r, s = $1.capitalize, $2.capitalize raise ArgumentError, "Not a rank name: '#{r}'" unless RANK_NAMES.include? r raise ArgumentError, "Not a suit name: '#{s}'" unless SUIT_NAMES.include? s @rank, @suit = RANK_NAMES.index(r), SUIT_NAMES.index(s) else raise ArgumentError, "Bad card spec: '#{spec}'" end end |
Instance Attribute Details
#rank ⇒ Object (readonly)
Returns the value of attribute rank.
8 9 10 |
# File 'lib/alucard/card.rb', line 8 def rank @rank end |
#suit ⇒ Object (readonly)
Returns the value of attribute suit.
8 9 10 |
# File 'lib/alucard/card.rb', line 8 def suit @suit end |