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.

···

--
Posted via http://www.ruby-forum.com/.

#<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 < abder.rahman.ali@gmail.com> wrote:

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.
--
Posted via http://www.ruby-forum.com/\.

Andrew Wagner 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 Ali <

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.

···

--
Posted via http://www.ruby-forum.com/\.

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 Ali < abder.rahman.ali@gmail.com> wrote:

Andrew Wagner 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 Ali <

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.
--
Posted via http://www.ruby-forum.com/\.

On Mon, 23 Aug 2010 09:30:11 -0500, Abder-Rahman Ali
<abder.rahman.ali@gmail.com> wrote in
<c3052cf78994d93e79706f4aef7ee99d@ruby-forum.com>:

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

···

--
Charles Calvert
Moderator - alt.computer.consultants.moderated
Submission Address: accm@celticwolf.net
Contact Address: accm_mod@celticwolf.net

Andrew Wagner 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 Ali <

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

Thanks a lot.

···

--
Posted via http://www.ruby-forum.com/\.

Thanks Charles.

···

--
Posted via http://www.ruby-forum.com/.