Forum: Ruby context of self

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-01-02 11:15
Hi
    I tried like

class First
        def initialize
                puts 'in First class'+self.inspect
        end
end
class Second < First
        def initialize
              super
              puts 'in second class'+self.inspect
        end
end

Second.new
       I got result as
in First class#<Second:0xb7f61820>
in second class#<Second:0xb7f61820>

         So is #<Second:0xb7f61820> always means from where I call the
def? I did not understand it

Thanks in advance
Sijo
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2009-01-02 11:51
(Received via mailing list)
Sijo Kg wrote:
> Second.new
>        I got result as
> in First class#<Second:0xb7f61820>
> in second class#<Second:0xb7f61820>
>
>          So is #<Second:0xb7f61820> always means from where I call the
> def? I did not understand it

self is the current object (in this case the object just constructed by
new).
The output of inspect does not change depending on where inspect is
called
from, nor does the value of self change when you call super.
In other words: if you call self.inspect and self is an object of class
Second, you'll call Second#inspect which, unless overridden, will return
"#<Second:bla>", and if you call Second.new, self will indeed be an
object
of class Second.

HTH,
Sebastian
This topic is locked and can not be replied to.