Skip to content

Instantly share code, notes, and snippets.

@zachgersh
Created March 1, 2013 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachgersh/5065734 to your computer and use it in GitHub Desktop.
Save zachgersh/5065734 to your computer and use it in GitHub Desktop.
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
@zachgersh
Copy link
Author

@@tickets at the end offers a return of the hash. Could have added a reader method as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment