DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Validating Object Initialization Using Metaprogramming
Source: <a href="http://stackoverflow.com/questions/613985/common-ruby-idioms">Common Ruby Idioms</a> [stackoverflow.com]
<snip>
class Ticket
@remaining = 3
def self.new
if @remaining > 0
@remaining -= 1
super
else
"IOU"
end
end
end
Ticket.new #=> Ticket
Ticket.new #=> Ticket
Ticket.new #=> Ticket
Ticket.new #=> "IOU"
</snip>





