Last active
August 29, 2015 14:05
-
-
Save glassbead0/a6abce6794bb913429b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Card | |
attr_reader :suit | |
attr_reader :value | |
def initialize(value, suit) | |
get_suit(suit) | |
get_value(value) | |
end | |
def get_suit(suit) # gets the suit, but rejects invalid suits | |
if [:♠, :♦, :♣, :♥].include?(suit) | |
@suit = suit | |
else | |
fail(ArgumentError, 'not a valid suit') | |
end | |
end | |
def get_value(value) # gets the value, but rejects invalid values. | |
@value = value | |
@value = :A if value == 1 | |
@value = :J if value == 11 | |
@value = :Q if value == 12 | |
@value = :K if value == 13 | |
if value < 1 || value > 13 | |
fail(ArgumentError, 'not a valid value for a card') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment