dynamic changes!

S

Spitfire

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?
 
R

Robert Klemme

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
 
S

Spitfire

Robert said:
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?
 
R

Robert Klemme

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
 
S

Spitfire

Robert said:
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
Sorry, didn't observe the IRB session closely. It's clear, now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,221
Messages
2,571,136
Members
47,749
Latest member
DomingaNow

Latest Threads

Top