# Is possible to extend "attr\_accessor" "attribue=" method?

**URL:** <https://www.ruby-forum.com/t/is-possible-to-extend-attr-accessor-attribue-method/135579>\
**Category:** Ruby\
**Created:** [April 23, 2008, 10:50am UTC](https://www.ruby-forum.com/t/is-possible-to-extend-attr-accessor-attribue-method/135579 "2008-04-23T10:50:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 23, 2008, 10:50am UTC](https://www.ruby-forum.com/t/is-possible-to-extend-attr-accessor-attribue-method/135579/1 "2008-04-23T10:50:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 23, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/is-possible-to-extend-attr-accessor-attribue-method/135579/2 "2008-04-23T15:14:06Z")

</div>

2008/4/23, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 23, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/is-possible-to-extend-attr-accessor-attribue-method/135579/3 "2008-04-23T15:15:16Z")

</div>

> 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 🙂

def self.attr\_accessor(\*names)  
names.each { |name|  
module\_eval %{  
attr\_reader :#{name}

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

```

end

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 23, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/is-possible-to-extend-attr-accessor-attribue-method/135579/4 "2008-04-23T13:56:02Z")

</div>

On Apr 23, 4:49 am, “Iñaki Baz C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 23, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/is-possible-to-extend-attr-accessor-attribue-method/135579/5 "2008-04-23T18:25:47Z")

</div>

On Apr 23, 9:14 am, “Iñaki Baz C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> module_eval %{
> attr_reader :#{name}
> 
> def #{name}= (value)
> @#{name} = value
> @modified = true
> end
> }
> }
> end
> 
> ```

Good work 🙂

T.
