Help with simple meta programming

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

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 Cooper

···

On 5/30/07, mario.lopes@gmail.com <mario.lopes@gmail.com> wrote:

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

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).

···

On 5/30/07, mario.lopes@gmail.com <mario.lopes@gmail.com> wrote:

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

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