Class initialization

This is an example similar to the one here:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/56755

class Foo
def initialize
@var = “var”
end
def to_s
“This is a customized string”
end
end

var = Foo.new

puts var

There are some scenarios I’m doing here:

1- When I remove “def to_s” method, and for “puts var”, I change it as
follows:

puts var.to_s

Result: #Foo:0x273a8

2- in “def to_s”:

def to_s
@var
end

Result: var

3- Running the code as it is shown at the top.

Result: “This is a customized string”

4- Without “def to_s”.

Result: #Foo:0x273f8

The most parts that are confusing me here are scenarios (1) and (4). Why
are the following results? And, what do those results mean by the way?

Thanks.

#<Foo…> just means “an instance of the class Foo”. That crazy hex at
the
end (0x…) is just the reference to the instance in memory, so that,
you
could tell two different instances apart.

Basically, what’s happening here is that there’s a default instance of
to_s
which knows how to look up what class the object is an instance of, look
up
the memory reference, and return that string. When you define to_s
yourself,
you’re overriding that default implementation to return something that’s
(hopefully) more useful for you.

On Mon, Aug 23, 2010 at 10:08 AM, Abder-Rahman A. <

Andrew W. wrote:

#<Foo…> just means “an instance of the class Foo”. That crazy hex at
the
end (0x…) is just the reference to the instance in memory, so that,
you
could tell two different instances apart.

Basically, what’s happening here is that there’s a default instance of
to_s
which knows how to look up what class the object is an instance of, look
up
the memory reference, and return that string. When you define to_s
yourself,
you’re overriding that default implementation to return something that’s
(hopefully) more useful for you.

On Mon, Aug 23, 2010 at 10:08 AM, Abder-Rahman A. <

Thanks a lot Andrew. It is becoming more clear now. I’m just still not
getting this point if you just can explain it further:

asically, what’s happening here is that there’s a default instance of
to_s which knows how to look up what class the object is an instance of,
look
up the memory reference, and return that string.

So somewhere (at least conceptually) there’s a definition that goes
something like this:

class Object
def to_s
class_name = self.class.name
reference = self.memory_reference
" #<#{class_name}:#{reference}>"
end
end

On Mon, Aug 23, 2010 at 10:30 AM, Abder-Rahman A. <

Andrew W. wrote:

So somewhere (at least conceptually) there’s a definition that goes
something like this:

class Object
def to_s
class_name = self.class.name
reference = self.memory_reference
" #<#{class_name}:#{reference}>"
end
end

On Mon, Aug 23, 2010 at 10:30 AM, Abder-Rahman A. <

Oh, I see, and I override it when I define to_s in the class.

Thanks a lot.

Thanks Charles.

On Mon, 23 Aug 2010 09:30:11 -0500, Abder-Rahman A.
[email protected] wrote in
[email protected]:

[snip]

asically, what’s happening here is that there’s a default instance of
to_s […]

Nitpick: “default instance” isn’t the correct phrase to use. You mean
“inherited to_s method” or “inherited implementation of to_s”.
“Instance” generally refers to an instance of a class created during
the running of a program.

I find that using precise language helps to avoid misunderstanding,
particularly in a text-only medium such as Usenet/email/web fora. That
requires knowing the correct language, hence my (hopefully helpful)
nitpicking. :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs