[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Overriding to_s

kimersen

5/29/2007 8:51:00 AM

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=n
end
def to_s
self.class
end
end

n = ZNum.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

6 Answers

Robert Klemme

5/29/2007 9:04:00 AM

0

On 29.05.2007 10:51, kimersen@gmail.com 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=n
> end
> def to_s
> self.class
> end
> end
>
> n = ZNum.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?

Because puts will revert to something else (likely #inspect) if the
result of to_s is not String.

irb(main):001:0> class Foo
irb(main):002:1> def to_s; self.class.to_s end
irb(main):003:1> end
=> nil
irb(main):004:0> puts Foo.new
Foo
=> nil

Kind regards

robert

Rohan Dey

5/29/2007 10:16:00 AM

0

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=n
> end
> def to_s
> self.class
> end
> end
>
> n = ZNum.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 via http://www.ruby-....

kimersen

5/29/2007 12:51:00 PM

0

On May 29, 11:04 am, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 29.05.2007 10:51, kimer...@gmail.com 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=n
> > end
> > def to_s
> > self.class
> > end
> > end
>
> > n = ZNum.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?
>
> Because puts will revert to something else (likely #inspect) if the
> result of to_s is not String.
>
> irb(main):001:0> class Foo
> irb(main):002:1> def to_s; self.class.to_s end
> irb(main):003:1> end
> => nil
> irb(main):004:0> puts Foo.new
> Foo
> => nil
>
> Kind regards
>
> robert

My understanding of puts is that it puts the result of to_s.
puts n and puts n.to_s are the same then and should in my example
print the same.
But they don't and that is for me a bit annoying because I thought I
understood what was going on.

/kim


Robert Klemme

5/29/2007 12:55:00 PM

0

On 29.05.2007 14:50, kimersen@gmail.com wrote:
> On May 29, 11:04 am, Robert Klemme <shortcut...@googlemail.com> wrote:
>> On 29.05.2007 10:51, kimer...@gmail.com 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=n
>>> end
>>> def to_s
>>> self.class
>>> end
>>> end
>>> n = ZNum.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?
>> Because puts will revert to something else (likely #inspect) if the
>> result of to_s is not String.
>>
>> irb(main):001:0> class Foo
>> irb(main):002:1> def to_s; self.class.to_s end
>> irb(main):003:1> end
>> => nil
>> irb(main):004:0> puts Foo.new
>> Foo
>> => nil
>>
>> Kind regards
>>
>> robert
>
> My understanding of puts is that it puts the result of to_s.
> puts n and puts n.to_s are the same then and should in my example
> print the same.

No, they are not the same because to_s returns the class. You really
have "puts foo" and "puts Foo" ("puts ZNum" in your case).

> But they don't and that is for me a bit annoying because I thought I
> understood what was going on.

Please carefully reread my comment. Since you chose to make to_s return
something that is *not a String* you get the behavior that you see.

robert

Brian Candler

5/29/2007 1:03:00 PM

0

On Tue, May 29, 2007 at 09:55:04PM +0900, kimersen@gmail.com wrote:
> > Because puts will revert to something else (likely #inspect) if the
> > result of to_s is not String.
> >
> > irb(main):001:0> class Foo
> > irb(main):002:1> def to_s; self.class.to_s end
> > irb(main):003:1> end
> > => nil
> > irb(main):004:0> puts Foo.new
> > Foo
> > => nil
> >
> > Kind regards
> >
> > robert
>
> My understanding of puts is that it puts the result of to_s.
> puts n and puts n.to_s are the same then and should in my example
> print the same.
> But they don't and that is for me a bit annoying because I thought I
> understood what was going on.

Yes, but what do you expect "puts" to do when it calls to_s on an object,
but the result is not a string? Raise an exception perhaps? It's more
friendly for puts to have a fallback behaviour.

Note: there's one other special case I'm aware of. If you do
puts nil
then you get the string "nil" printed (plus newline). However, nil.to_s is
the empty string.

Regards,

Brian.

kimersen

5/29/2007 1:04:00 PM

0

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=n
> > end
> > def to_s
> > self.class
> > end
> > end
>
> > n = ZNum.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-....

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