I have a DSL – like piece of Ruby code that looks like this:
Activity.new('20-06-2012') do
Eat.log do |l|
l.duration = 0.30
l.priority = 5
end
Work.log do |l|
l.duration = 2
l.priority = 3
end
end
Every time the log() method is called, a Log object is instantiated (behind the scenes) with the block passed to the method (the block is passed on to the Log object’s constructor). My question is, is there a way to collect all the returns from the log() method? In the example above, the return value of the outermost block is the last call to log(). But I want to get all the calls’ results in an array, not just the last one.
Thanks!
Your internal DSL is still too Ruby-like, you may refactor it to look something like this:
Now capture the calls in your
instance_evaland accumulate the values in an internal array, the usual DSL stuff.