Can anyone shed some light!

Can anyone shed some light on this?

It looks like some sort of abstraction. But I a having trouble understanding how this works. Also the following code doesn't make any sense to me.


Furhtermore, these decorators can be re-applied multiple times to the same receiver:

cup = # or even.. cup = milk, sprinkles, sprinkles

cup.cost.should == 2.10 cup.should be_instance_of(Coffee) cup.injectors.should == [:milk, :sprinkles, :sprinkles


Isn't this sort of thing already possible already. Can someone explain?



Yes, what you are looking at here is a code sample of what is possible with Jackbox closures. You see normally decorators in ruby either suffer from class identity loss or limit the times they can be applied to an object to 1(one) time.

Here a a couple of articles on that:

So with our closures you can apply the same pattern to an object over and over,

    cup = milk, sprinkles, sprinkles

but the instance remains being what it is.

    cup.should be_instance_of(Coffee)

What's more you can have the class introspect on the decorators it possesses.

    cup.injectors.should == [:milk, :sprinkles, :sprinkles ]

Furthermore you can add new facets to your decorators:

    user_input = 'extra red sprinkles'     sprinkles do       define_method :appearance do         user_input       end     end

    cup.enrich(sprinkles)     cup.appearance.should == 'extra red sprinkles'     cup.cost.should == 2.25

And then latter:

    user_input = 'cold milk'     milk do       define_method :temp do         user_input.split.first       end     end

    cup.enrich milk     cup.temp.should == 'cold'     cup.cost.should == 2.55

Decorators are useful in graphical environments including HTML, in steam processing, command processors to name a few.

Thank you, kindly.