# How to extend a method?

**URL:** <https://www.ruby-forum.com/t/how-to-extend-a-method/113599>\
**Category:** Ruby\
**Created:** [September 2, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/how-to-extend-a-method/113599 "2007-09-02T16:35:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ye\_Dingding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/49beb7/32.png) [@Ye\_Dingding](https://www.ruby-forum.com/u/Ye_Dingding)\
**Post date:** [September 2, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/how-to-extend-a-method/113599/1 "2007-09-02T16:35:14Z")

</div>

Class A  
attr\_accessor :a  
def initialize  
self.a = ‘a’  
end  
end

I want to extend class A and add a new instance variable b. How can i do  
this?

## Class A attr\_accessor :b def initialize

How can i initialize the variable b.  
Must i rewrite the whole method, like  
self.a = ‘a’  
self.b = ‘b’

---

<div class="post-metadata">

**Author:** ![Ye\_Dingding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/49beb7/32.png) [@Ye\_Dingding](https://www.ruby-forum.com/u/Ye_Dingding)\
**Post date:** [September 2, 2007, 11:29pm UTC](https://www.ruby-forum.com/t/how-to-extend-a-method/113599/2 "2007-09-02T23:29:55Z")

</div>

Subclass C:

class B \< A  
attr\_accessor :b  
def intialize  
super # calls parent class (A) that already initializes @a  
@b = ‘b’  
end  
end

Regards  
Florian

---

<div class="post-metadata">

**Author:** ![Ye\_Dingding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/49beb7/32.png) [@Ye\_Dingding](https://www.ruby-forum.com/u/Ye_Dingding)\
**Post date:** [September 2, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/how-to-extend-a-method/113599/3 "2007-09-02T23:42:56Z")

</div>

Florian AÃŸmann schrieb:

> Regards  
> Florian

module ExtendA  
attr\_accessor :b  
def self.included(base)  
base.instance\_variable\_set :@b, ‘b’  
end  
end

class A  
include ExtendA  
end

# There are probably 10^x ways to do this…

Regards  
again

---

<div class="post-metadata">

**Author:** ![Ye\_Dingding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/49beb7/32.png) [@Ye\_Dingding](https://www.ruby-forum.com/u/Ye_Dingding)\
**Post date:** [September 3, 2007, 11:12am UTC](https://www.ruby-forum.com/t/how-to-extend-a-method/113599/4 "2007-09-03T11:12:53Z")

</div>

Thanks. Maybe using ExtendA module is a good start, 🙂

I don’t want to rewrite A class directly, but i want to extend it in my  
app.  
Indeed, i want to extend Rails::Initializer, and when i start  
a rails app, it can initialize the variable automatically.

Module R  
class A  
attr\_accessor :b

```
    def self.run
       init = new B
       init.send(:process)
    end

    def process
       initize_a
    end

    def initize_a
       # other action using a.
    end
end

class B
    attr_accessor :a

    def initialize
       self.a = 'a'
    end
end

```

end

I want to add a variable ‘b’ and initialize it when new a B. And then do  
initialize\_b when executing A#process.

Module R  
class A  
def initialize\_b  
end  
end  
end

I think Moduel ExtendB(as what you said) can be used to add a variable  
‘b’  
and set the instance value.  
But how can i directly initialize\_b when executing A#process? I used to  
get  
a A object and then invoke the initialize\_b method.  
But i didn’t like that way? Is there a better way to directly inject the  
code to the A#process?

Thank you.
