# Dynamic changes!

**URL:** <https://www.ruby-forum.com/t/dynamic-changes/80391>\
**Category:** Ruby\
**Created:** [December 25, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/dynamic-changes/80391 "2006-12-25T19:42:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [December 25, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/dynamic-changes/80391/1 "2006-12-25T19:42:05Z")

</div>

These are the sequence of actions I performed in irb.

```
class Dumber
	def sayHello
		puts "Hello!"
	end
end

db = Dumber.new()

db.sayHello
## executes sayHello method!

Class Dumber
	def sayHi
		puts "Hi"
	end
end

db.sayHi
## executes the newly added method sayHi!

```

how does the object already created aware of the newly added method?

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [December 25, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/dynamic-changes/80391/2 "2006-12-25T20:42:09Z")

</div>

On 25.12.2006 19:37, Spitfire wrote:

> ```
> db.sayHello
> 
> ```
> 
> how does the object already created aware of the newly added method?

The method is added to it’s _class_ - and that relationship does not  
change, only the class changes.

$ irb  
irb(main):001:0\> class Dumber  
irb(main):002:1\> def say\_hello() puts “hello” end  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> Dumber.instance\_methods.grep /^say\_/  
=\> [“say\_hello”]  
irb(main):005:0\> Dumber.object\_id  
=\> 1913790  
irb(main):006:0\> class Dumber  
irb(main):007:1\> def say\_hi() puts “hi” end  
irb(main):008:1\> end  
=\> nil  
irb(main):009:0\> Dumber.instance\_methods.grep /^say\_/  
=\> [“say\_hi”, “say\_hello”]  
irb(main):010:0\> Dumber.object\_id  
=\> 1913790

As you see, Dumber stays the same instance, there are no multiple  
versions of it.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [December 25, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/dynamic-changes/80391/3 "2006-12-25T20:46:10Z")

</div>

Robert K. wrote:

> > ```
> > db.sayHello
> > 
> > ```
> > 
> > how does the object already created aware of the newly added method?
> 
> The method is added to it’s _class_ - and that relationship does not  
> change, only the class changes.

It’s class? I’m sorry I don’t understand what you are saying. Can you  
please clarify?

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [December 25, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/dynamic-changes/80391/4 "2006-12-25T20:56:12Z")

</div>

On 25.12.2006 20:44, Spitfire wrote:

> > > ```
> > > ## executes the newly added method sayHi!
> > > 
> > > ```
> > > 
> > > how does the object already created aware of the newly added method?
> > 
> > The method is added to it’s _class_ - and that relationship does not  
> > change, only the class changes.
> 
> It’s class? I’m sorry I don’t understand what you are saying. Can you  
> please clarify?

Sorry, should have read “its class”. Please look closely at the IRB  
session to understand what I mean:

irb(main):001:0\> class Foo  
irb(main):002:1\> def m1() end  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> Foo.object\_id  
=\> 1917050  
irb(main):005:0\> Foo.instance\_methods.grep /m\d/  
=\> [“m1”]  
irb(main):006:0\> f=Foo.new  
=\> #Foo:0x381468  
irb(main):007:0\> f.class  
=\> Foo  
irb(main):008:0\> f.class.object\_id  
=\> 1917050  
irb(main):009:0\> f.m1  
=\> nil  
irb(main):010:0\> f.m2  
NoMethodError: undefined method `m2’ for #Foo:0x381468  
from (irb):10  
from :0  
irb(main):011:0\> class Foo  
irb(main):012:1\> def m2() end  
irb(main):013:1\> end  
=\> nil  
irb(main):014:0\> Foo.object\_id  
=\> 1917050  
irb(main):015:0\> Foo.instance\_methods.grep /m\d/  
=\> [“m2”, “m1”]  
irb(main):016:0\> f.class  
=\> Foo  
irb(main):017:0\> f.class.object\_id  
=\> 1917050  
irb(main):018:0\> f.m1  
=\> nil  
irb(main):019:0\> f.m2  
=\> nil

```
robert
```
