Using Decorator classes multiple times on same object

Hi , I have these three lines of code. The first 2 run perfectly, when I
add the 3rd line, it says:

initialize': undefined method description’ for #Mocha:0x85e8dfc
(NoMethodError)
from Espresso.rb:43:in new' from Espresso.rb:43:in

hb2 = Mocha.new(HouseBlend.new)
puts hb2.cost
#this doesn’t work #
hb2 = Mocha.new(hb2)## this is line 43

What can I do to have the same functionality that works in ruby?

Basically, I am trying to translate this java code into ruby:

Beverage hb2 = new Mocha(new HouseBlend());
Beverage hb2 = new Mocha(new HouseBlend());

which works in java (creating a decorator more than once on same object)

This is the github page for more info: https://github.com/tmtwd/Starfuzz

Timothy Wi wrote in post #1165113:

Hi , I have these three lines of code. The first 2 run perfectly, when I
add the 3rd line, it says:

initialize': undefined method description’ for #Mocha:0x85e8dfc
(NoMethodError)
from Espresso.rb:43:in new' from Espresso.rb:43:in

I would invite you to try our new library called Jackbox. In it we toy
with the ideas of closures as modules / ruby code Injectors. You can
install it by doing: gem install jackbox.

With it you would do the following:

class Beverage
attr_accessor :cost
attr_accessor :description
def initialize
@description = “unknown beverage”
@cost = 1.20
end
end

class Decaf < Beverage
def initialize
@description = “decaf”
end
def cost
1.50
end
end

injector :mocha do
def cost
super + 0.20
end

def description
@description = super + ’ mocha’
end
end

injector :half_n_half do
def cost
super + 0.10
end

def description
@description = super + ’ creamer’
end
end

then use like this:

cup = Decaf.new.enrich mocha, half_n_half
cup.cost.should == 1.80
cup.description.should == ‘decaf mocha creamer’

You can view a longer description at:

Thank you, kindly

I also refer you to this posting on the rails board:

https://www.ruby-forum.com/topic/6634985#new

Thanks

Ruby code Injectors: Closures as Modules: Jackbox

It toys with the ideas of closures as modules. In it you’ll find a
series of new takes on ruby that are sure to spike you interest. With
it we have come up with a solution to the decorators handicap in ruby
and introduced some new constructs for code injection.

Please feel free to use it free of charge and to send us your comments
or inquiries. Available from RubyGems.org for Linux, Mac, and Windows.
Just run: gem install jackbox.

Thank you.