Recently, there was a post on news:comp.lang.ruby[1] by Avdi G., who
asked if there were any conventions when using metaprogramming. He gave
this example:
require ‘memoize’
require ‘validate’
class Foo
attr_accessor :bar
memoize :bar
validate :bar do |value|
value.include? “Johnny Walker Black Label”
end
end
Disregard the fact that you rarely want to memoize a writer method.
How do I know if memoize and validate play nicely together?
There has already been a proposal that would make it very easy to add
metaprogramming capabilities without treading on each other’s toes[2],
but it would be a very big step.
A simpler solution would be to add a special method definition (an
advice), that had a `super’ that called the previous definition of
that method.
class Foo
attr_accessor :bar
advice bar
'[' + super + ']'
end
advice bar
'{' + super + '}'
end
end
foo = Foo.new
foo.bar = ‘baz’
foo.bar #=> {[baz]}
That way, the user could choose in which order the wrapper methods
should be called.
Cheers,
Daniel S.
[1]
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/e5be2e3b8a9d9614/6b3ab3e56f3a82cd
[2] http://www.rcrchive.net/rcr/show/321