# Help with simple meta programming

**URL:** <https://www.ruby-forum.com/t/help-with-simple-meta-programming/105467>\
**Category:** Ruby\
**Created:** [May 30, 2007, 9:45am UTC](https://www.ruby-forum.com/t/help-with-simple-meta-programming/105467 "2007-05-30T09:45:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 30, 2007, 9:45am UTC](https://www.ruby-forum.com/t/help-with-simple-meta-programming/105467/1 "2007-05-30T09:45:37Z")

</div>

Hi,

This is a basic question of meta-programming with Ruby, which I’m  
still getting familiar with.

I want to change/create a new attr\_accessor such that it calls a kind  
of proxy for retrieving the variable (which is not actually stored in  
the object itself but rather on the database).

def variable  
invoke\_method(variable)  
…  
end

def variable=(value)  
invoke\_method(variable, value)  
…  
end

So, I catch the method name which should correspond to the variable  
(except for the set method, which includes an extra ‘=’) and pass it  
to another function.

Any hints on how to get it done the right way? Thanks!

Cheers,

Mário

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 30, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/help-with-simple-meta-programming/105467/2 "2007-05-30T12:25:26Z")

</div>

On 5/30/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def variable=(value)  
> invoke\_method(variable, value)  
> …  
> end

There are a few ways to do this, and I’m surprised you haven’t had a few  
answers yet! So… I’ll provide some pointers in code. You will need to  
tidy  
this up and structure it better, but it should at least demonstrate a  
concept…

class Module

# Why in Module and not Class? Because otherwise you couldn’t use it

# within module definitions… only classes, whereas Module lets you

cover

# both…

def attr\_proxied\_accessor(var)  
define\_method(var) do  
# At this stage, we’re defining the “x” method…  
10  
end

```
define_method(:"#{var}=") do |v|
  # At this stage, we're defining the "x=" method..
end

```

end  
end

class Something  
attr\_proxied\_accessor :x  
end

puts Something.new.x

There’s still some way to go, such as actually calling your proxy  
routines  
and doing the “instance variable set/get”, but it should at least crack  
the  
biggest problem…? If you continue to get stuck, Googling around looking  
for  
full code to attr\_accessor provides a few techniques.

Cheers,  
Peter C.

> **[Ruby Inside: The Ruby Blog](http://www.rubyinside.com/)**
>
> The biggest Ruby and Rails blog since 2006 - tutorials, news, and lots more

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 30, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/help-with-simple-meta-programming/105467/3 "2007-05-30T13:40:38Z")

</div>

On 5/30/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> invoke\_method(variable)  
> to another function.
> 
> Any hints on how to get it done the right way? Thanks!
> 
> Cheers,
> 
> Mário

Maybe you would like to have a look at my personal favorite of all  
Ruby Q.zes(\*) (well that is of course save my own - just kidding-  
there are lots of close seconds BTW):  
[http://rubyquiz.com/quiz67.html](http://rubyquiz.com/quiz67.html)  
I am quite sure that this should cover your needs.

Cheers  
Robert

(\*) although I miserably failed to find a solution (at that time).
