# Array#to\_s in Ruby 1.9 Head

**URL:** <https://www.ruby-forum.com/t/array-to-s-in-ruby-1-9-head/72930>\
**Category:** Ruby\
**Created:** [October 19, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/array-to-s-in-ruby-1-9-head/72930 "2006-10-19T23:35:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 19, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/array-to-s-in-ruby-1-9-head/72930/1 "2006-10-19T23:35:51Z")

</div>

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 }

It seems like an unnecessary change that takes away a very nice  
default behavior.

I have a situation where I represent a tree of objects as nested  
arrays. I can convert the  
entire tree to a string via a pre-order traversal as:

```
text = tree.to_s

```

I like that.

Gary W.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 21, 2006, 1:25am UTC](https://www.ruby-forum.com/t/array-to-s-in-ruby-1-9-head/72930/2 "2006-10-21T01:25:55Z")

</div>

On Fri, 2006-10-20 at 06:34 +0900, removed\_email\_address@domain.invalid wrote:

> It seems like an unnecessary change that takes away a very nice  
> default behavior.

FWIW I find I need ‘puts ary.inspect’ much more often than ‘puts ary’,  
so I think this is a good change.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 21, 2006, 2:05am UTC](https://www.ruby-forum.com/t/array-to-s-in-ruby-1-9-head/72930/3 "2006-10-21T02:05:59Z")

</div>

Ross B. wrote:

> > It seems like an unnecessary change that takes away a very nice  
> > default behavior.
> 
> FWIW I find I need ‘puts ary.inspect’ much more often than ‘puts ary’,  
> so I think this is a good change.

(Why ‘puts ary.inspect’ instead of ‘p ary’ ?)

I suspect there is a fair amount of code that depends on

print a

being the same as

print \*a

This would be the case if you are thinking of an array of strings as an  
array of lines. For example, if you are writing a method whose input, a,  
is the contents of a text file _either_ as a string _or_ as an array of  
lines, then

print a

has the same behavior on all inputs.

With the change in Array#to\_a, you don’t have this nice duck-typing, but  
you get it back if you use splat:

print \*a

(that works when a is a string, at least in 1.8.4)

I’m not against the change, just thinking about damage control.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 21, 2006, 11:26am UTC](https://www.ruby-forum.com/t/array-to-s-in-ruby-1-9-head/72930/4 "2006-10-21T11:26:27Z")

</div>

On Sat, 2006-10-21 at 09:04 +0900, Joel VanderWerf wrote:

> > > a.each { |x| print x }
> > > 
> > > It seems like an unnecessary change that takes away a very nice  
> > > default behavior.
> > 
> > FWIW I find I need ‘puts ary.inspect’ much more often than ‘puts ary’,  
> > so I think this is a good change.
> 
> (Why ‘puts ary.inspect’ instead of ‘p ary’ ?)

Good point. I’m not sure where that ‘puts’ came from anyway, I blame  
late-night typing 😉  
Thinking about it now, the more common case where this would save me  
typing is string interpolation.

> is the contents of a text file _either_ as a string _or_ as an array of  
> lines, then
> 
> print a
> 
> has the same behavior on all inputs.

> With the change in Array#to\_a, you don’t have this nice duck-typing, but  
> you get it back if you use splat:
> 
> print \*a
> 
> (that works when a is a string, at least in 1.8.4)
> 
> I’m not against the change, just thinking about damage control.

Certainly, I have such code myself that would need to be changed to deal  
with this. I’m already resigned to the fact that some of my code will  
break when 1.9 becomes mainstream so I guess this would cause extra  
typing in the immediate, but I’m still sure it’d save me keystrokes in  
the long run.

Generally, MHO is that arrays (and hashes too - should this change  
encompass Hash#to\_s as well?), as data structures, should retain some  
indication of that structure when asked for a string representation. For  
me at least, the current behaviour of simply joining the elements is  
rarely useful - I almost always need to either join(’, ') or map { … }  
arrays somehow for non-debug output anyway.
