# Adding a new method to a class

**URL:** <https://www.ruby-forum.com/t/adding-a-new-method-to-a-class/69835>\
**Category:** Ruby\
**Created:** [September 24, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/adding-a-new-method-to-a-class/69835 "2006-09-24T21:42:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [September 24, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/adding-a-new-method-to-a-class/69835/1 "2006-09-24T21:42:28Z")

</div>

Hi,

What is called the process of adding a new method of an already defined  
class ?

For example:

class A  
def a  
end  
end

And in another file:

class A  
def b  
end  
end

And now my real question:

What if I want to declare a new class variable in the initialize method  
?  
If it’s a derived class I can call super:  
def initialize  
super  
@my\_var = “”  
end

but if it not derived ?  
How can I do this ?

Thanks,  
Mickael

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [September 24, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/adding-a-new-method-to-a-class/69835/2 "2006-09-24T22:06:31Z")

</div>

Mickael Faivre-Macon wrote:

> What is called the process of adding a new method of an already defined class ?

Hi Mickael,

I’m not sure of any official technical name but I often see “extend”  
used for a previously undefined method and “override” for redefining an  
existing method.

> What if I want to declare a new class variable in the initialize method ?  
> If it’s a derived class I can call super:  
> def initialize  
> super  
> @my\_var = “”  
> end
> 
> but if it not derived ?  
> How can I do this ?

Well a @var is an instance variable, a class variable has two @, like  
@@var. but in either case, you would just add it (in ruby you just  
assign to a variable and it comes into existence, not need to “declare”  
them any special way).

class Tree  
def initialize  
@cat = ‘in it’  
@@dog = ‘bark’  
end  
end

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [September 24, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/adding-a-new-method-to-a-class/69835/3 "2006-09-24T22:22:32Z")

</div>

Mickael Faivre-Macon wrote:

> end  
> What if I want to declare a new class variable in the initialize method ?  
> If it’s a derived class I can call super:  
> def initialize  
> super  
> @my\_var = “”  
> end
> 
> but if it not derived ?  
> How can I do this ?

You can alias the old initialize method and replace it with another one:

irb(main):014:0\> class A  
irb(main):015:1\> def initialize(x)  
irb(main):016:2\> @foo = x  
irb(main):017:2\> end  
irb(main):018:1\> end  
=\> nil  
irb(main):019:0\> a = A.new 10  
=\> #\<A:0x38e4d0 @foo=10\>  
irb(main):020:0\> class A  
irb(main):021:1\> alias initialize\_old initialize  
irb(main):022:1\> def initialize(x)  
irb(main):023:2\> initialize\_old x  
irb(main):024:2\> @bar = “foo”  
irb(main):025:2\> end  
irb(main):026:1\> end  
=\> nil  
irb(main):027:0\> b = A.new 20  
=\> #\<A:0x374610 @bar=“foo”, @foo=20\>

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [September 24, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/adding-a-new-method-to-a-class/69835/4 "2006-09-24T22:34:36Z")

</div>

Thank you MonkeeSage for your precisions.  
Thanks Robert, that’s what I was looking for.

Mickael.

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [September 24, 2006, 10:40pm UTC](https://www.ruby-forum.com/t/adding-a-new-method-to-a-class/69835/5 "2006-09-24T22:40:38Z")

</div>

On 9/24/06, Mickael Faivre-Macon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> What is called the process of adding a new method of an already defined  
> class ?

Actually that is the _only_ way to add a method to a class. In Ruby the  
class is already defined inside it’s declaration, for what I think one  
of  
the many brilliant ideas of Matz.  
Look at this code for demonstration  
def who\_am\_I \*objs  
objs.each do  
|obj|  
p "#{obj.name}:#{obj.class} @ %08x " % obj.object\_id  
end #objs.each do  
end

class A  
who\_am\_I self  
def a; 42; end  
end  
who\_am\_I A  
class A  
who\_am\_I self  
def b; 1764; end  
end

For example:

> end  
> end

which would be the same as  
class A  
def a  
def b  
end

And now my real question:

> What if I want to declare a new class variable in the initialize method ?

Robert had already answered that 1 brilliantly, so why should Robert  
answer  
that one brilliantly?

If it’s a derived class I can call super:

> 

Cheers  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein
