Robert Klemme
12/25/2006 7:37:00 PM
On 25.12.2006 19:37, Spitfire wrote:
> 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?
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