From: Andrew Wagner Date: 2010-08-23T23:19:36+09:00 Subject: Re: Class initialization --0016e6dbe945b860ad048e7e54e0 Content-Type: text/plain; charset=UTF-8 # 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: # > > 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: # > > 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/. > > --0016e6dbe945b860ad048e7e54e0--