From: "NARUSE, Yui" Date: 2009-11-21T16:59:10+09:00 Subject: [ruby-core:26857] Re: Internals: #to_s .vs. #to_str? % irb19 irb(main):001:0> class Foo;def to_s;"to_s";end;def to_str;"to_str";end;end => nil irb(main):002:0> "#{Foo.new}" => "to_s" This seems because all object shouold be embeded in "#{obj}". Kurt Stephens wrote: > Does "123#{xyz}456" use xyz.to_s or xyz.to_str? > > NARUSE, Yui wrote: >> Kurt Stephens wrote: >>> Is there a description of the semantic differences between #to_s and >>> #to_str? Are there any particular constraints on the results of these >>> methods? Is it considered "legal" to return frozen Strings in some >>> cases for either of the two? Is one used internal for implicit casts? >>> >>> I have some ideas that may reduce GC work and improve large String >>> concatenation. >>> >>> I do remember a discussion about the differences between #to_a and >>> #to_ary, but I don't quite remember why they are semantically different. >> >> to_int, to_str, to_ary, to_hash are fot implicit conversion. >> path.to_str is called in path of following code, if path is not String: >> open(path){|f| ... } >> >> to_i, to_s, to_a are explicit conversion. >> >> David Flanagan's Programing Language Ruby describes >> those type conversion of Ruby at 3.8.7. -- NARUSE, Yui