to_s and concatenation 
Author Message
 to_s and concatenation


irb(main):001:0> class Wtf
irb(main):002:1> def to_s
irb(main):003:2> 'fnord'
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0>
irb(main):007:0* puts "#{Wtf.new}"
fnord
=> nil
irb(main):008:0> puts Wtf.new
fnord
=> nil
irb(main):009:0> puts "foo #{Wtf.new}"
foo fnord
=> nil
irb(main):010:0> puts "foo" + Wtf.new
TypeError: failed to convert Wtf into String
        from (irb):10:in `+'
        from (irb):10
irb(main):011:0> exit

How come one works and the other doesn't? Is it a precedence thing?

--
Rasputin :: Jack of All Trades - Master of Nuns



Tue, 15 Nov 2005 21:48:35 GMT  
 to_s and concatenation

R> irb(main):010:0> puts "foo" + Wtf.new
R> TypeError: failed to convert Wtf into String

 String#+ want an object which respond to #to_str

Guy Decoux



Tue, 15 Nov 2005 21:52:41 GMT  
 to_s and concatenation

Quote:

> R> irb(main):010:0> puts "foo" + Wtf.new
> R> TypeError: failed to convert Wtf into String

>  String#+ want an object which respond to #to_str

Right, but when do you use one and when the other?
Pickaxe says:

  to_str         str.to_str -> str

Synonym for String#to_s . to_str is used by methods such as
String#concat to convert their arguments to a string. Unlike to_s, which
is supported by almost all classes, to_str is normally implemented only
by those classes that act like strings. Of the built-in classes, only
Exception and String implement to_str.

supports what you just said (although the 'synonym for' sentence is a
bit misleading), but what else would you use to_s for but to
print to stdout?

I know this is probably basic stuff, but there doesn't seem to be
anywhere else to ask...

--
Rasputin :: Jack of All Trades - Master of Nuns



Wed, 16 Nov 2005 00:06:49 GMT  
 to_s and concatenation

R> supports what you just said (although the 'synonym for' sentence is a
R> bit misleading), but what else would you use to_s for but to
R> print to stdout?

svg% ruby -e 'class A;def to_s()"A#object";end;end; p [A.new, "aa"].join(":")'"A#object:aa"
svg%

Guy Decoux



Wed, 16 Nov 2005 00:16:40 GMT  
 to_s and concatenation

Quote:

> Synonym for String#to_s . to_str is used by methods such as
> String#concat to convert their arguments to a string. Unlike to_s, which
> is supported by almost all classes, to_str is normally implemented only
> by those classes that act like strings. Of the built-in classes, only
> Exception and String implement to_str.

> supports what you just said (although the 'synonym for' sentence is a
> bit misleading), but what else would you use to_s for but to
> print to stdout?

Interpolation into strings - "#{foo}" - uses to_s

Regards,

Brian.



Wed, 16 Nov 2005 00:29:38 GMT  
 to_s and concatenation

Quote:


>>Synonym for String#to_s . to_str is used by methods such as
>>String#concat to convert their arguments to a string. Unlike to_s, which
>>is supported by almost all classes, to_str is normally implemented only
>>by those classes that act like strings. Of the built-in classes, only
>>Exception and String implement to_str.

>>supports what you just said (although the 'synonym for' sentence is a
>>bit misleading), but what else would you use to_s for but to
>>print to stdout?

> Interpolation into strings - "#{foo}" - uses to_s

> Regards,

> Brian.

hmm... it seems to me that the real question is this: why have both to_s
and to_str when the return value of them should at all concievable (to
me at least) times be the same?

--
dc -e
4dd*od3*dddn1-89danrn10-dan3+ann6*dan*2*an13dn1+dn2-dn3+5*ddan2/9+an13nap



Wed, 16 Nov 2005 04:56:54 GMT  
 to_s and concatenation

Quote:

> hmm... it seems to me that the real question is this: why have both to_s
> and to_str when the return value of them should at all concievable (to
> me at least) times be the same?

I think the rationale is something like this:

You have a method for explicitly converting an object to a string for
display purposes:
    5.to_s   #>>  "5"

But you might not want to have an automatic, implicit conversion to a
string in all circumstances:

    "10" + 5      #>> should this be "105" or "15" or 15 ?
    5 + "10"      #>> what about this: "510" or "15" or 15 ?

So I believe that to_s is for cases where you explicitly require a
conversion to be made, such as foo.to_s or "#{foo}"

On the other hand, to_str is for cases where an implicit, automatic
conversion should take place. If you don't define it, then such automatic
conversions do not take place, so you get an exception instead.

irb(main):001:0> "10" + 5
TypeError: failed to convert Fixnum into String
        from (irb):1:in +'
        from (irb):1
irb(main):002:0> class Fixnum; def to_str; to_s; end; end
=> nil
irb(main):003:0> "10" + 5
=> "105"

The same distinction doesn't seem to hold for to_int and to_i though - you
get a different exception. I don't understand the mechanics of coercion of
numeric types.

Regards,

Brian.



Wed, 16 Nov 2005 05:34:33 GMT  
 to_s and concatenation
Hi --

Quote:

> The same distinction doesn't seem to hold for to_int and to_i though - you
> get a different exception. I don't understand the mechanics of coercion of
> numeric types.

Just one addendum: it does hold for to_a/to_ary -- that is, you can
define to_ary on an object and that's how the object will represent
itself in cases where an array is expected, while to_a will return
at least something for every object.

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Wed, 16 Nov 2005 05:48:07 GMT  
 to_s and concatenation

Quote:

> > hmm... it seems to me that the real question is this: why have both to_s
> > and to_str when the return value of them should at all concievable (to
> > me at least) times be the same?

Exactly, but I think Brians explanation makes sense to me now.
I wasa thrown by to_s/to_str in String being synonymous, whereas that's
not the case in general. Thanks for the help.

Quote:
> So I believe that to_s is for cases where you explicitly require a
> conversion to be made, such as foo.to_s or "#{foo}"

> On the other hand, to_str is for cases where an implicit, automatic
> conversion should take place. If you don't define it, then such automatic
> conversions do not take place, so you get an exception instead.

--
Rasputin :: Jack of All Trades - Master of Nuns


Wed, 16 Nov 2005 09:16:20 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Confused about to_s in Ruby / irb

2. nil.to_s

3. Float#to_s ???

4. Strange String bug (around type.to_s from mod_ruby)?

5. Total newbie, to_s method and format strings

6. Bug in latest snapshot, regarding Float#to_s

7. Regexp#to_s not very useful

8. to_s and <<

9. Current dataset in a concatenation?

10. String Concatenation in Dolphin?

11. string concatenation

12. concatenation problem

 

 
Powered by phpBB® Forum Software