# Class initialization

**URL:** <https://www.ruby-forum.com/t/class-initialization/193728>\
**Category:** Ruby\
**Created:** [August 23, 2010, 4:08pm UTC](https://www.ruby-forum.com/t/class-initialization/193728 "2010-08-23T16:08:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 23, 2010, 4:08pm UTC](https://www.ruby-forum.com/t/class-initialization/193728/1 "2010-08-23T16:08:09Z")

</div>

This is an example similar to the one here:  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/56755](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.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 23, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/class-initialization/193728/2 "2010-08-23T16:26:38Z")

</div>

#\<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. \<

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 23, 2010, 4:30pm UTC](https://www.ruby-forum.com/t/class-initialization/193728/3 "2010-08-23T16:30:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 23, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/class-initialization/193728/4 "2010-08-23T16:40:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 23, 2010, 4:59pm UTC](https://www.ruby-forum.com/t/class-initialization/193728/5 "2010-08-23T16:59:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 23, 2010, 10:59pm UTC](https://www.ruby-forum.com/t/class-initialization/193728/6 "2010-08-23T22:59:06Z")

</div>

Thanks Charles.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 23, 2010, 10:50pm UTC](https://www.ruby-forum.com/t/class-initialization/193728/7 "2010-08-23T22:50:47Z")

</div>

On Mon, 23 Aug 2010 09:30:11 -0500, Abder-Rahman A.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

[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. 🙂
