# 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)
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2ea8d7ac6c199bb98fabb2d00b5d9622%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment