Is possible to extend "attr_accessor" "attribue=" method?

Hi, I’d like to use an “attr_accessor” but when its attribute is
modified it should be executed more code than just an assignament.
I handle it in this way (not cool):

class MyClass1 < Father

attr_reader :my_param

def  my_param= (value)
  @my_param = value
  @modified = true
  ...more code...
end

end

As you see I can’t use “attr_accessor :my_param” since if I use it I
should also redefine “my_param=” method.
I have lots of MyClassX classes inheriting from “Father” and I’want
@modified to be an attribute of Father and each soon class has its own
attributes, and when any attribute is modified I want @modified to
become true, so I’m looking for something as:

class Father

# Attributes: @modified (false by default)

def **any_attribute**= (value)
  @**ny_attribute** = value
  @modified = true
end

end

class MyClass1 < Father
attr_accessor :my_param1, :my_param2
end

Is it possible in saome way? mybe using “no_method_name” method?

2008/4/23, Trans [email protected]:

            attr_reader :#{name}
    end

It’s really cool !!!

Just an issue: the above code don’t allow multiple attributes
definition:

attr_accessor :atr, atr2

=> `attr_accessor’: wrong number of arguments (2 for 1) (ArgumentError)

I don’t remember now what must I do to take all the possible arguments
as an array, could you point it to me please?

thanks a lot.

Just an issue: the above code don’t allow multiple attributes definition:

attr_accessor :atr, atr2

=> `attr_accessor’: wrong number of arguments (2 for 1) (ArgumentError)

Done :slight_smile:

def self.attr_accessor(*names)
names.each { |name|
module_eval %{
attr_reader :#{name}

    def #{name}= (value)
      @#{name} = value
      @modified = true
    end
  }
}

end

On Apr 23, 4:49 am, “Iñaki Baz C.” [email protected] wrote:

Hi, I’d like to use an “attr_accessor” but when its attribute is
modified it should be executed more code than just an assignament.
I handle it in this way (not cool):

    class MyClass1 < Father
         def self.attr_accessor(name)
           module_eval %{
            attr_reader :#{name}

            def  #{name}= (value)
                    @#{name} = value
                    @modified = true
                    ...more code...
            end
           }
        end
    end

T.

On Apr 23, 9:14 am, “Iñaki Baz C.” [email protected] wrote:

                    module_eval %{
                            attr_reader :#{name}

                            def #{name}= (value)
                                    @#{name} = value
                                    @modified = true
                            end
                    }
            }
    end

Good work :slight_smile:

T.