lib/hands/card.rb in hands-0.2.1 vs lib/hands/card.rb in hands-0.3.0

- old
+ new

@@ -63,18 +63,24 @@ return end # Invalid @value = nil + raise InvalidValue, "'#{val}' is an invalid suit." end def suit=(suit) - if (suit.is_a?(String) or suit.is_a?(Symbol)) and SUITS.include?(suit.to_sym) - @suit = suit.to_sym - else + # Convert to symbol + suit = suit.to_sym if suit.is_a?(String) + + # Make sure it's valid + unless suit.is_a?(Symbol) and SUITS.include?(suit) @suit = nil + raise InvalidSuit, "'#{suit}' is an invalid suit." end + + @suit = suit end # Standard inspect # # @return [String] `super`'s implementation and the receiver's `description` if it `is_valid?` @@ -94,19 +100,28 @@ # @return [Boolean] Does the receiver contain an invalid value and suit combination def is_invalid? !self.is_valid? end + # Get a short string representation of the card + # + # Example: "♥2" + # + # @return [String] short string representation of the card + def description + return 'invalid' unless self.is_valid? + self.value.capitalize + SUIT_CHARACTERS[self.suit_index] + end + # Get a string representation of the card # + # Example: "Two of Hearts" + # # @return [String] string representation of the card - def description - if self.is_valid? - "#{VALUE_DESCRIPTIONS[self.value_index].capitalize} of #{self.suit.to_s.capitalize}" - else - 'invalid' - end + def long_description + return 'invalid' unless self.is_valid? + "#{VALUE_DESCRIPTIONS[self.value_index].capitalize} of #{self.suit.to_s.capitalize}" end # Compares the card with another card # # By default `check_suit` is `false`. If set to `true`, it will order cards that have the same value by their suit. @@ -127,19 +142,21 @@ # Mainly used for internal reasons when comparing cards. # # @return [Integer] index of the card's suit # @see SUITS def suit_index + return nil unless self.suit SUITS.index(self.suit.downcase) end # Values's index # # Mainly used for internal reasons when comparing cards. # # @return [Integer] index of the card's value # @see VALUES def value_index + return nil unless self.value VALUES.index(self.value.downcase) end end end