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

Snippets has posted 5883 posts at DZone. View Full User Profile

Validating Object Initialization Using Metaprogramming

05.04.2010
| 2228 views |
  • submit to reddit
        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>