Array#to_s in Ruby 1.9 Head

Mauricio Fernandez writes in http://eigenclass.org/hiki.rb?Changes+in
+Ruby+1.9+update+5
that Ruby 1.9 Head has changed Array#to_s to be an alias for
Array#to_inspect.

What is the motivation for that change?
  print a
becomes
  a.each { |x| print x }

Or (easier):
print a.join('')

···

From: gwtmp01@mac.com [mailto:gwtmp01@mac.com]

Sure. But both solutions fail with nested arrays.

I still prefer the 1.8 default behavior.

Gary Wright

···

On Oct 19, 2006, at 6:05 PM, Gavin Kistner wrote:

From: gwtmp01@mac.com [mailto:gwtmp01@mac.com]

Mauricio Fernandez writes in http://eigenclass.org/hiki.rb?Changes+in
+Ruby+1.9+update+5
that Ruby 1.9 Head has changed Array#to_s to be an alias for
Array#to_inspect.

What is the motivation for that change?
  print a
becomes
  a.each { |x| print x }

Or (easier):
print a.join('')

Gavin Kistner wrote:

What is the motivation for that change?
  print a
becomes
  a.each { |x| print x }

Or (easier):
print a.join('')

For less typing, omit the string to
  print a.join

David Vallner

Liquid depends on the current Array#to_s for speed. It builds nested
array of objects which all render their final result upon the to_s
call.

···

On 10/19/06, David Vallner <david@vallner.net> wrote:

Gavin Kistner wrote:
>> What is the motivation for that change?
>> print a
>> becomes
>> a.each { |x| print x }
>
> Or (easier):
> print a.join('')
>

For less typing, omit the string to
        print a.join

David Vallner

--
Tobi
http://shopify.com - modern e-commerce software
http://typo.leetsoft.com - Open source weblog engine
http://blog.leetsoft.com - Technical weblog

Tobias Lütke wrote:

Liquid depends on the current Array#to_s for speed. It builds nested
array of objects which all render their final result upon the to_s
call.

Perhaps then:

  print a.flatten.join

T.

Hi,

···

In message "Re: Array#to_s in Ruby 1.9 Head" on Fri, 20 Oct 2006 10:19:54 +0900, "=?ISO-8859-1?Q?Tobias_L=FCtke?=" <tobias.luetke@gmail.com> writes:

Liquid depends on the current Array#to_s for speed. It builds nested
array of objects which all render their final result upon the to_s
call.

Do you mean Array#join does not work for you this case?

              matz.

Yeah, sure, there are lots of ways to work around it but why should
Array#to_s default to generating debugging output? That is what
Array#inspect is for, right?

Gary Wright

···

On Oct 19, 2006, at 9:45 PM, Trans wrote:

Tobias Lütke wrote:

Liquid depends on the current Array#to_s for speed. It builds nested
array of objects which all render their final result upon the to_s
call.

Perhaps then:

  print a.flatten.join

Hi,

Yeah, sure, there are lots of ways to work around it but why should
Array#to_s default to generating debugging output? That is what
Array#inspect is for, right?

The official purpose of #to_s method is that returning string
representation of the object. No more, no less. So it's up to our
expectation for the string representation of the object. I changed
the behavior to make string representation of arrays more
distinguishable from other objects.

I don't think to_s _SHOULD_ work as replacement of Array#join. But
there may be another reason I missed to keep it compatible with 1.8.

              matz.

···

In message "Re: Array#to_s in Ruby 1.9 Head" on Fri, 20 Oct 2006 10:50:00 +0900, gwtmp01@mac.com writes:

OK, I grabbed ruby 1.9 so that I could actually play with it.

I always assumed that Array#join simply called Array#to_s on each element
and then constructed the final string by inserting the separator between
each element, but it actually seems to flatten the array first:

Ruby 1.8:
    a = [1,2]
    b = [3,4]
    c = [a,'foo',b]
    c.to_s # 12foo34
    c.inspect # [[1, 2], "foo", [3, 4]]
    c.join # 12foo34
    c.join('-') # 1-2-foo-3-4

When the separator is the empty string, the two algorithms are the same, but
if the separator is not empty then they give different results. The
non-flattening algorithm would produce: 12-foo-34 in the above example.

My objections to the Array#to_s behavior in 1.9 were based on thinking about
Array#join as simply calling #to_s on each element (without the flattening step),
which would make it more difficult to work around.

So in 1.9, the above example produces:
    c.to_s # [[1, 2], "foo", [3, 4]]
    c.inspect # [[1, 2], "foo", [3, 4]]
    c.join # 12foo34
    c.join('-') # 1-2-foo-3-4

I think the documentation for Array#join should mention the flattening step.
Something like:

      Returns a string created by flattening the array, converting each element of
      the result to a string via #to_s, and concatenating the strings with _sep_ as
      the separator.

Gary Wright

···

On Oct 20, 2006, at 12:57 AM, Yukihiro Matsumoto wrote:

The official purpose of #to_s method is that returning string
representation of the object. No more, no less. So it's up to our
expectation for the string representation of the object. I changed
the behavior to make string representation of arrays more
distinguishable from other objects.

I don't think to_s _SHOULD_ work as replacement of Array#join. But
there may be another reason I missed to keep it compatible with 1.8.