Class: Patience::Card::Suit

Inherits:
Object show all
Defined in:
lib/patience/suit.rb

Overview

Suit supplies only one method: #red?. The idea is that every actual suit has its own class, which is inherited from Suit. Each suit class defines its own #black? method dynamically.

Constant Summary collapse

Heart =

Create classes for every suit, giving the answer on the question about their blackness. If the answer is negative, obviously the suit is red.

create_suit_class.call(1, false)
Diamond =
create_suit_class.call(2, false)
Spade =
create_suit_class.call(3, true)
Club =
create_suit_class.call(4, true)

Instance Method Summary collapse

Instance Method Details

#<=>(other_suit) ⇒ Object

Compares two suits with each other for equality.



55
56
57
# File 'lib/patience/suit.rb', line 55

def <=>(other_suit)
  @num <=> other_suit.to_i
end

#different_color?(other_suit) ⇒ Boolean

Returns true if the color of suit of differs from other suit’s color.

Returns:

  • (Boolean)


67
68
69
# File 'lib/patience/suit.rb', line 67

def different_color?(other_suit)
  (black? && other_suit.red?) || (red? && other_suit.black?)
end

#red?Boolean

Checks whether card is “red” (being not black). The opposite of the Card#black?. Returns true if the card is red. Otherwise, returns false.

Returns:

  • (Boolean)


44
45
46
# File 'lib/patience/suit.rb', line 44

def red?
  not black?
end

#same_color?(other_suit) ⇒ Boolean

Returns true, if the color of suit is the same as the color of other suit.

Returns:

  • (Boolean)


61
62
63
# File 'lib/patience/suit.rb', line 61

def same_color?(other_suit)
  (black? && other_suit.black?) || (red? && other_suit.red?)
end

#to_sObject

Returns plural string representation of a suit. It asks class to give its full name, exscinding everything but its actual name.



50
51
52
# File 'lib/patience/suit.rb', line 50

def to_s
  "#{self.class.name.demodulize}s"
end