# Injector declaration
jack :StringRefinements do
lets String do # Our re-classing
with singleton_class do
def new *args, &code
super(*args, &code) + ' is a special string'
end
end
end
end
class OurClass
include StringRefinements()
def foo_bar
String('foo and bar')
end
end
c = OurClass.new
c.foo_bar.class.should == String
c.foo_bar.should == 'foo and bar is a special string'
StringRefinements do
String() do # Adding more stuff
def extra
:extra
end
end
end
c.foo_bar.should == 'foo and bar is a special string'
c.foo_bar.extra.should == :extra
SR = StringRefinements do
lets String do # New Version
def to_s
super + '****'
end
end
end
# c is still the same
c.foo_bar.should == 'foo and bar is a special string'
c.foo_bar.extra.should == :extra
class OurOtherClass
include SR # Another class application
def foo_bar
String('foo and bar')
end
end
d = OurOtherClass.new
d.foo_bar.to_s.should == 'foo and bar****'
expect{ d.extra }.to raise_error(NoMethodError)