# Metaprogramming Conventions

**URL:** <https://www.ruby-forum.com/t/metaprogramming-conventions/94160>\
**Category:** Ruby\
**Created:** [April 23, 2006, 2:10pm UTC](https://www.ruby-forum.com/t/metaprogramming-conventions/94160 "2006-04-23T14:10:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [April 23, 2006, 2:10pm UTC](https://www.ruby-forum.com/t/metaprogramming-conventions/94160/1 "2006-04-23T14:10:38Z")

</div>

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](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/e5be2e3b8a9d9614/6b3ab3e56f3a82cd)  
[2] [http://www.rcrchive.net/rcr/show/321](http://www.rcrchive.net/rcr/show/321)

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [April 23, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/metaprogramming-conventions/94160/2 "2006-04-23T14:22:54Z")

</div>

Hi –

On Sun, 23 Apr 2006, Daniel S. wrote:

> validate :bar do |value|  
> would be a very big step.
> 
> called.  
> “super” is not the best term, though: it means “next highest up in the  
> method lookup path” (i.e., in a different class or module), whereas  
> you’re describing something that’s lexically above but semantically on  
> the same level. It’s more an alias-related operation than a super  
> one, though not exactly that either.

Also, it seems a little fragile to me. It would introduce a whole set  
of constraints on the order of code and even the order of  
file-loading.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!
