Help with simple meta programming

M

mario.lopes

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
 
R

Robert Dober

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=3D(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 '=3D') and pass it
to another function.

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

Cheers,

M=E1rio
Maybe you would like to have a look at my personal favorite of all
Ruby Quizzes(*) (well that is of course save my own - just kidding-
there are lots of close seconds BTW):
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).

--=20
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top