Class initialization

A

Abder-Rahman Ali

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.
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

#<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 Ali <
 
A

Abder-Rahman Ali

Andrew said:
#<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.
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.
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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
 
A

Abder-Rahman Ali

Andrew said:
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
Oh, I see, and I override it when I define to_s in the class.

Thanks a lot.
 
C

Charles Calvert

[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. :)
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top