------ art_203438_29963508.1180475870726
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
the result of foo.class is not of type String, but of type Class. when you
say puts foo.to_s, the puts method is calling to_s on the class that you've
returned from calling to_s. To get the expected result, define to_s like
this:
def to_s
self.class.to_s
end
OR
def to_s
"#{self.class}" #this is not as efficient, but you're guaranteed to get
a String result
end
-dave
On 5/29/07, kimersen / gmail.com <kimersen / gmail.com> wrote:
>
> On May 29, 12:15 pm, Rohan Dey <rohan... / gmail.com> wrote:
> > unknown wrote:
> > > Hi,
> >
> > > I'm playing around with ruby trying to understand whats going on, and
> > > here is something I don't understand.
> >
> > > class ZNum
> > > def initialize(n)
> > > @n > > > end
> > > def to_s
> > > self.class
> > > end
> > > end
> >
> > > n Num.new(1234)
> >
> > > puts n # prints #<ZNum:0xb75dfcc8>
> > > puts n.to_s # prints ZNum
> >
> > > In both cases it uses ZNum#to_s, but the results are different. Why?
> >
> > > /kim
> >
> > Not able to understand what you are actually asking. Can you put little
> > detail on this.
> >
> > --
> > Posted viahttp://www.ruby-forum.com/.
>
> I'm working on an interface for some legacy data.
> The legacy data will be stored in objects as ruby numbers (fixnum,
> bignum and float), but I need to override ruby's number formating. My
> example above has no practical value, it's just some odd behavior that
> I would like to understand before I design my legacy interface.
>
> /kim
>
>
>
------ art_203438_29963508.1180475870726--