From: Eric Hodel Date: 2006-05-02T07:42:18+09:00 Subject: Re: from block to code (also: how to get the bindings of a block) On May 1, 2006, at 12:50 PM, chiaro scuro wrote: > On 5/1/06, Eric Hodel wrote: >> >> > I would normally agree with you, but this is not a design problem >> > or a domain-conceptualization problem. >> >> I doubt your problem is really that special. > > > it's special in respect to the fact that goes against the > boundaries of the > language, so it cannot necessarily rely on beautiful 'natural' > constructs > that apply within the conceptual boundaries of the language. > > >> It's a language-twisting problem that imposes otherwise unimportant >> > constraints on what I can and cannot do. >> >> If you're doing something this ugly you're probably doing something >> very wrong. >> > > Eric, how would you do it? > > recipe "breakfast" do > eggs = 2, :big > bacon = 3 > end > > this should add to a global hash (or return directly) the following: > > { > :eggs => [2,:big], > :bacon => [3] > } def recipe(name, &block) o = Object.new o.instance_variable_set :@attrs, {} def o.method_missing(name, *args) name = name.to_s.sub(/add_/, '').intern @attrs[name] = args end o.instance_eval &block return o.instance_variable_get(:@attrs) end ingredients = recipe 'breakfast' do add_eggs 2, :big add_bacon 3 end p ingredients > I need to keep the equal signs. Why? -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com