# Re: Array#to\_s in Ruby 1.9 Head

**URL:** <https://www.ruby-forum.com/t/re-array-to-s-in-ruby-1-9-head/72934>\
**Category:** Ruby\
**Created:** [October 20, 2006, 12:05am UTC](https://www.ruby-forum.com/t/re-array-to-s-in-ruby-1-9-head/72934 "2006-10-20T00:05:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 12:05am UTC](https://www.ruby-forum.com/t/re-array-to-s-in-ruby-1-9-head/72934/1 "2006-10-20T00:05:54Z")

</div>

From: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> Mauricio F. writes in [eigenclass.org](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(‘’)

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 12:35am UTC](https://www.ruby-forum.com/t/re-array-to-s-in-ruby-1-9-head/72934/2 "2006-10-20T00:35:56Z")

</div>

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

> Or (easier):  
> print a.join(’’)

Sure. But both solutions fail with nested arrays.

I still prefer the 1.8 default behavior.

Gary W.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 2:58am UTC](https://www.ruby-forum.com/t/re-array-to-s-in-ruby-1-9-head/72934/3 "2006-10-20T02:58:19Z")

</div>

Gavin K. 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 V.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 3:20am UTC](https://www.ruby-forum.com/t/re-array-to-s-in-ruby-1-9-head/72934/4 "2006-10-20T03:20:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 3:50am UTC](https://www.ruby-forum.com/t/re-array-to-s-in-ruby-1-9-head/72934/5 "2006-10-20T03:50:20Z")

</div>

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

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 W.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 3:46am UTC](https://www.ruby-forum.com/t/re-array-to-s-in-ruby-1-9-head/72934/6 "2006-10-20T03:46:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 6:58am UTC](https://www.ruby-forum.com/t/re-array-to-s-in-ruby-1-9-head/72934/7 "2006-10-20T06:58:46Z")

</div>

Hi,

In message “Re: Array#to\_s in Ruby 1.9 Head”  
on Fri, 20 Oct 2006 10:50:00 +0900, removed\_email\_address@domain.invalid writes:

|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.
```

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 21, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/re-array-to-s-in-ruby-1-9-head/72934/8 "2006-10-21T23:06:10Z")

</div>

On Oct 20, 2006, at 12:57 AM, Yukihiro M. 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.

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 W.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 6:46am UTC](https://www.ruby-forum.com/t/re-array-to-s-in-ruby-1-9-head/72934/9 "2006-10-20T06:46:45Z")

</div>

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?=” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

```
