From: Logan Capaldo Date: 2006-05-09T03:58:17+09:00 Subject: Re: How can I write the awesome kind of code? On May 8, 2006, at 2:38 PM, Pat Maddox wrote: > I'm writing a small app to do poker simulations. I read a lot of > blogs in which the author shows off his cool DSL. I decided I want to > be able to specify a poker hand in my app like > > hand "my_hand" do > players 10 > chips 1000 > end > > my_hand.foo > > How can I do something like that? > > Pat > class Hand def initialize(name, &block) @name = name if block instance_eval(&block) end self end def players(i) @players = i end def chips(i) @chips = i end def foo puts self.inspect end end def hand(name, &block) Hand.new(name, &block) end my_hand = hand "my_hand" do players 10 chips 1000 end my_hand.foo # If you want to automatically set 'my_hand' you can put it inside an enclosing object and dynamically define the my_hand method