Created
March 1, 2013 16:19
-
-
Save zachgersh/5065734 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 LotteryTicket | |
NUMERIC_RANGE = 1..25 | |
attr_reader :picks, :purchased | |
def initialize(*picks) | |
if picks.length != 3 | |
raise ArgumentError, "You must pick three numbers" | |
elsif picks.uniq.length != 3 | |
raise ArgumentError, "You must pick three unique numbers" | |
elsif picks.detect {|p| not NUMERIC_RANGE === p} | |
raise ArgumentError, "You must pick a number between 1 and 25" | |
end | |
@picks = picks | |
@purchased = Time.now | |
end | |
def self.new_random | |
new(rand(25) + 1, rand(25) + 1, rand(25) + 1) | |
rescue ArgumentError | |
retry | |
end | |
end | |
class LotteryDraw | |
@@tickets = {} | |
def LotteryDraw.buy(customer, *tickets) | |
unless @@tickets.has_key?(customer) | |
@@tickets[customer] = [] | |
end | |
@@tickets[customer] += tickets | |
@@tickets | |
end | |
end | |
p LotteryDraw.buy 'Zachary Gershman', LotteryTicket.new_random, LotteryTicket.new_random, LotteryTicket.new_random |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@@tickets at the end offers a return of the hash. Could have added a reader method as well.