0

I'm trying to track every class variable's history by metaprogramming. I'm not a fan of asking such questions but It took me 5 hours to be able to write these and from now on I have no idea how to proceed (I'm new to ruby, and this is the first time I'm playing with metaprogramming).

In my understanding; when attr_accessor_with_history initializes in a class, it should execute the code it is containing. Thus, every time this method initializes, by the merits of metaprogramming every class is going to have its own method for the problem I described.

In the code I submitted, readers are initialized properly but I can't say the same about the code in class_eval part. I need clarification about why the code isn't working, and metaprogramming in general.

class Class
  def attr_accessor_with_history(attr_name)
    attr_name = attr_name.to_s
    attr_reader attr_name
    attr_reader attr_name + "_history"

    class_eval "%Q{
    @#{attr_name}_history=[nil] 
    def #{attr_name}=(value)
        #{attr_name}=value
        #{attr_name}_history.push(value)
    end
    }
    "
  end
end

class Klass
  attr_accessor_with_history :kamil
  def initialize(value)
    kamil = value
  end
end

a = Klass.new(5)
a.kamil = 1
puts "#{a.kamil_history}"
1
  • 2
    I don't believe you should be wrapping your %Q in quotes for the class_eval bit. %Q{...} evaluates to an interpolated string. Commented Mar 3, 2012 at 17:54

3 Answers 3

4

Saas course, huh? You should know that instance variables should start with the @ sign. So for example in your initialize method what's kamil? If it is a instance variable it should be @kamil. I would also suggest that you revise your class_eval argument with respect to this consideration.

EDIT:

@#{attr_name}_history=[nil].

I would also put this code to some method because it's not very good to initialize your instance variable out of any method.

Sign up to request clarification or add additional context in comments.

Comments

1

The code inside your method 'def attr_accessor_with_history(attr_name)' is invoked every time you call this method. You call it in your class when wrote class Klass attr_accessor_with_history :kamil ..

When Ruby process this line 'attr_accessor_with_history :kamil' it will actually run the code from the method Class.attr_accessor_with_history. A string inside class_eval is interpreted as code as it was written by you directly.

Finally, your interpreted code will be like this:

class Klass ..

@kamil_history=[nil]

def kamil=(value)
    kamil=value
    kamil_history.push(value)
end

See the problem? it must be @kamil=value, otherwise it will call the method 'kamil=' again, not accessing the instance variable @kamil.

Similarly, it must be '@kamil_history.push(..)'.

You can find the working code here: http://maxivak.com/ruby-metaprogramming-and-own-attr_accessor/

Comments

0

To call a setter method on self, you'll need to write self.foo = bar. If you just write foo = bar it will just create local variable named bar and not call any method. So you'll need to change lines 11 and 23 accordingly.

Also by using %Q{} inside quotes, your whole eval will actually just evaluate to a string. You should use %Q{} or quotes - not both. In fact you probably shouldn't use a string at all, but call class_eval with a block and use define_method inside the block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.