Class: FiftyTwo::Card

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/fiftytwo/card.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deck, rank, suit) ⇒ Card

Returns a new instance of Card.



12
13
14
15
16
# File 'lib/fiftytwo/card.rb', line 12

def initialize(deck, rank, suit)
  @deck = deck
  @rank = rank
  @suit = suit
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, **args, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/fiftytwo/card.rb', line 39

def method_missing(name, **args, &block)
  return super unless name.to_s.ends_with?("?")

  attributes.each do |attribute|
    return attribute.send(name) if attribute.respond_to?(name)
  end

  super
end

Instance Attribute Details

#deckObject (readonly)

Returns the value of attribute deck.



9
10
11
# File 'lib/fiftytwo/card.rb', line 9

def deck
  @deck
end

#rankObject (readonly)

Returns the value of attribute rank.



9
10
11
# File 'lib/fiftytwo/card.rb', line 9

def rank
  @rank
end

#suitObject (readonly)

Returns the value of attribute suit.



9
10
11
# File 'lib/fiftytwo/card.rb', line 9

def suit
  @suit
end

Instance Method Details

#<=>(other) ⇒ Object



35
36
37
# File 'lib/fiftytwo/card.rb', line 35

def <=>(other)
  [rank, suit] <=> [other.rank, other.suit]
end

#codeObject



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

def code
  "#{rank.code}#{suit.code}"
end

#identifierObject



27
28
29
# File 'lib/fiftytwo/card.rb', line 27

def identifier
  "#{rank.identifier}#{suit.identifier}"
end

#renderObject



31
32
33
# File 'lib/fiftytwo/card.rb', line 31

def render
  code.rjust(3).send(suit.color.name)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/fiftytwo/card.rb', line 49

def respond_to_missing?(name, include_private = false)
  return false unless name.to_s.ends_with?("?")
  return false unless attributes.any? { |a| a.respond_to?(name) }
end

#to_sObject Also known as: name



22
23
24
# File 'lib/fiftytwo/card.rb', line 22

def to_s
  "#{rank} of #{suit}"
end