Class: Card

Inherits:
Object
  • Object
show all
Defined in:
lib/rora/model/card.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Card

Returns a new instance of Card.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rora/model/card.rb', line 20

def initialize(*args)
  if(args.size == 2)
    @rank = args[0]
    @suit = args[1]
  end
  if(args.size == 1)
    raise ArgumentError, "#{args} is an invalid card sequence" if args[0].length != 2
    @rank = Rank.get(args[0][0])
    @suit = Suit.get(args[0][1])
  end
end

Instance Attribute Details

#rankObject (readonly)

Returns the value of attribute rank.



18
19
20
# File 'lib/rora/model/card.rb', line 18

def rank
  @rank
end

#suitObject (readonly)

Returns the value of attribute suit.



18
19
20
# File 'lib/rora/model/card.rb', line 18

def suit
  @suit
end

Class Method Details

.to_cards(string) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/rora/model/card.rb', line 60

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



52
53
54
# File 'lib/rora/model/card.rb', line 52

def == card
  self.id == card.id
end

#eql?(card) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/rora/model/card.rb', line 48

def eql? card
  self == card
end

#hashObject



56
57
58
# File 'lib/rora/model/card.rb', line 56

def hash
  return self.id
end

#idObject



32
33
34
# File 'lib/rora/model/card.rb', line 32

def id
  @rank.id * @suit.id
end

#keyObject



36
37
38
# File 'lib/rora/model/card.rb', line 36

def key
  @rank.key + @suit.key
end

#nameObject



44
45
46
# File 'lib/rora/model/card.rb', line 44

def name
  "#{@rank.value} of #{@suit.value}s"
end

#to_sObject



70
71
72
# File 'lib/rora/model/card.rb', line 70

def to_s
  "Card: name='#{name}' value='#{@rank.key}#{@suit.key}' id=#{id}"
end

#valueObject



40
41
42
# File 'lib/rora/model/card.rb', line 40

def value
  @rank.value + @suit.value
end