# Splat, #to\_ary and #to\_a

**URL:** <https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005>\
**Category:** Ruby\
**Created:** [September 18, 2006, 4:11am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005 "2006-09-18T04:11:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 4:11am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/1 "2006-09-18T04:11:36Z")

</div>

Hi!

I suppose this just qualifies as unexpected (by me) behaviour  
but an odd thing happened when I wrote code like this:

[\*“foo\nbar”] # =\> [“foo\n”, “bar”]

Mocking out the call sequence for the splat, it seems that  
it first checks for #respond\_to? :to\_ary and, if so, sends  
it to the object–the surprising part was that if it does  
NOT respond to :to\_ary, the splat simply calls #to\_a on its  
subject.

The idea behind the code, of course, is to have a single  
expression that either constructs an Array or reproduces  
the current one from whatever object we are dealing with.  
This behaviour makes doing so a tad less concise 🙂

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 4:20am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/2 "2006-09-18T04:20:27Z")

</div>

Eero S. wrote:

> Hi!
> 
> I suppose this just qualifies as unexpected (by me) behaviour  
> but an odd thing happened when I wrote code like this:
> 
> [\*“foo\nbar”] # =\> [“foo\n”, “bar”]

On the same topic, this is a strange parsing:

stream.puts ([] \<\< obj).join("\n")

Above is the same as writing:

stream.puts([] \<\< obj).join("\n")

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 4:40am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/3 "2006-09-18T04:40:50Z")

</div>

Hi,

In message “Re: Splat, #to\_ary and #to\_a”  
on Mon, 18 Sep 2006 11:12:43 +0900, Eero S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|I suppose this just qualifies as unexpected (by me) behaviour  
|but an odd thing happened when I wrote code like this:  
|  
| [\*“foo\nbar”] # =\> [“foo\n”, “bar”]

It’s fixed in 1.9. 1.8 will remain as it is now for the sake of  
compatibility.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 4:54am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/4 "2006-09-18T04:54:32Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: Splat, #to\_ary and #to\_a”  
> on Mon, 18 Sep 2006 11:12:43 +0900, Eero S.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> |I suppose this just qualifies as unexpected (by me) behaviour  
> |but an odd thing happened when I wrote code like this:  
> |  
> | [\*“foo\nbar”] # =\> [“foo\n”, “bar”]
> 
> It’s fixed in 1.9. 1.8 will remain as it is now for the sake of  
> compatibility.

Thank you for clarification!

> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 8:15am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/5 "2006-09-18T08:15:08Z")

</div>

On 9/18/06, Eero S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> NOT respond to :to\_ary, the splat simply calls #to\_a on its  
> subject.
> 
> The idea behind the code, of course, is to have a single  
> expression that either constructs an Array or reproduces  
> the current one from whatever object we are dealing with.  
> This behaviour makes doing so a tad less concise 🙂

Can’t you just test whether the object is an Array (or if it supports  
the array methods you need)?

x = “foo\nbar”  
(x.is\_a?(Array) ? x : [x]) # =\> [“foo\nbar”]  
x = %w(a b c)  
(x.is\_a?(Array) ? x : [x]) # =\> [“a”, “b”, “c”]

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 5:28am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/6 "2006-09-18T05:28:57Z")

</div>

On 9/17/06, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It’s fixed in 1.9. 1.8 will remain as it is now for the sake of  
> compatibility.

Hmmmm,

Just what is the fix in 1.9.

Are you saying that  
[\*“foo\nbar”] #=\> [“foo\nbar”]  
in 1.9?

How about:  
[\*(1…4)]  
which in 1.8.x produces [1, 2, 3, 4]

and which of these will change in 1.9?

def a(\*arg)  
p arg  
end

a(“foo\nbar”)  
which in 1.8.4 prints [“foo\nbar”]

a((1…3))  
prints [1, 2, 3, 4]  
a(1…3)  
prints [1…3]

a(1…3, “foo\nbar”)  
prints [1…3, “foo\nbar”]

a(\*“foo\nbar”)  
prints [“foo\n”, “bar”]

a(\*(1…4))  
prints [1, 2, 3, 4]

\*a = “foo\nbar”  
a gets [“foo\nbar”]  
\*a = (1…4)  
a gets [1…4]  
\*a = “foo\nbar”, (1…3)  
a gets [“foo\nbar”, 1…3]

I’m not sure that I see a consistent pattern here. But I have to say  
that I think that the 1.8.x interpretations of

a(_“foo\nbar”)  
and  
a(_(1…4))

both make sense since the caller is ASKING for the splat, and that  
these two seem very similar to the  
[\*“foo\nbar”]  
case which started this.

I’ve also always been surprised that String#to\_a didn’t produce an  
array of single character strings, instead of splitting the string  
into lines, but that’s probably just a personal surprise, and I  
wouldn’t expect that to change.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 9:27am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/7 "2006-09-18T09:27:12Z")

</div>

Hi,

In message “Re: Splat, #to\_ary and #to\_a”  
on Mon, 18 Sep 2006 12:28:18 +0900, “Rick DeNatale”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Just what is the fix in 1.9.  
|  
|Are you saying that  
|[\*“foo\nbar”] #=\> [“foo\nbar”]  
|in 1.9?

Yes. And if you are curious you can try it by yourself.

|How about:  
|[\*(1…4)]  
|which in 1.8.x produces [1, 2, 3, 4]

Currently,

[1…4]

This may be an issue.

|and which of these will change in 1.9?  
|  
|def a(\*arg)  
| p arg  
|end  
|  
|a(“foo\nbar”)  
|which in 1.8.4 prints [“foo\nbar”]

[“foo\nbar”]

|a((1…3))  
| prints [1, 2, 3, 4]

[1…3]

|a(1…3)  
| prints [1…3]

[1…3]

|a(1…3, “foo\nbar”)  
|prints [1…3, “foo\nbar”]

[1…3, “foo\nbar”]

|a(\*“foo\nbar”)  
|prints [“foo\n”, “bar”]

[“foo\nbar”]

|a(\*(1…4))  
|prints [1, 2, 3, 4]

[1…4]

| \*a = “foo\nbar”  
| a gets [“foo\nbar”]

[“foo\nbar”]

| \*a = (1…4)  
| a gets [1…4]

[1…4]

| \*a = “foo\nbar”, (1…3)  
| a gets [“foo\nbar”, 1…3]

[“foo\nbar”, 1…3]

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 11:01am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/8 "2006-09-18T11:01:18Z")

</div>

Hi –

On Mon, 18 Sep 2006, Yukihiro M. wrote:

> |in 1.9?
> 
> This may be an issue.

I’ve always thought it was convenient to have [\*x] do the expanding  
on x (including strings and ranges). But if it’s going to disappear,  
I think it should disappear consistently, so that \* doesn’t start  
having a totally different meaning depending on the operand’s class.

David

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 10:55am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/9 "2006-09-18T10:55:18Z")

</div>

On 9/18/06, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> |[\*“foo\nbar”] #=\> [“foo\nbar”]  
> |in 1.9?
> 
> Yes. And if you are curious you can try it by yourself.

Is 1.9.0 good enough?, because I get

> 

ruby1.9 -ve ‘p [\*“foo\nbar”]’  
–\>ruby 1.9.0 (2006-04-21) [i486-linux]  
–\>[“foo\n”, “bar”]

☹

Robert

> ```
> matz.
> 
> ```

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 11:53am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/10 "2006-09-18T11:53:24Z")

</div>

On 9/18/06, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> prints [1, 2, 3, 4]  
> Does the following happen only to me?

irb(main):001:0\> def a(\*arg)  
irb(main):002:1\> p arg  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> a((1…3))  
[1…3]  
=\> nil

Using ruby 1.8.4 (2006-04-14) [i386-mswin32].

Thanks,  
Alvim.

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 11:49am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/11 "2006-09-18T11:49:23Z")

</div>

On 9/18/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > |which in 1.8.x produces [1, 2, 3, 4]  
> > having a totally different meaning depending on the operand’s class.  
> > I have found, and it seems it was not a common concern, that Range and  
> > \<=\>  
> > and #succ  
> > are too closly coupled, but that is OT,

I think it would be a nice opportunity to define the protocol for \*,  
cleanly  
and giving the community the opportunity to express the fors and  
againsts  
about potential implementations, well the good ol’ ML stuff 😉

Personally I thought the #to\_a protocol is perfect, but #to\_a is not.

Cheers  
Robert

David

> –  
> David A. Black | removed\_email\_address@domain.invalid  
> Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
> DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 12:41pm UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/12 "2006-09-18T12:41:30Z")

</div>

We could always have a “hyper-fatsplat” \>\>\*\<\< [1]. Then it could do  
everything at once, plus feed your cats when you go on vacation. 😉

[1] see Perl6’s “hyper-fatarrow” -  
[http://www.nntp.perl.org/group/perl.perl6.language/25946](http://www.nntp.perl.org/group/perl.perl6.language/25946)

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 12:25pm UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/13 "2006-09-18T12:25:26Z")

</div>

Marcelo A. wrote:

> > a((1…3))  
> > =\> nil
> 
> Using ruby 1.8.4 (2006-04-14) [i386-mswin32].

No, it the way it’s supposed to be.

a (1…3) #=\> outputs: [1…3]  
a \*(1…3) #=\> outputs: [1, 2, 3]

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 12:47pm UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/14 "2006-09-18T12:47:31Z")

</div>

On 9/18/06, MonkeeSage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> We could always have a “hyper-fatsplat” \>\>\*\<\< [1]. Then it could do  
> everything at once, plus feed your cats when you go on vacation. 😉

Noo, noo, Matz please do not listen to him, I have two dogs.  
Make it feed dogs, PLEASE

Robert

[1] see Perl6’s “hyper-fatarrow” -

> [Re: Pair of lists =\> list of pairs? - nntp.perl.org](http://www.nntp.perl.org/group/perl.perl6.language/25946)
> 
> Regards,  
> Jordan

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/15 "2006-09-18T18:36:34Z")

</div>

Yukihiro M. wrote:

> When we talk about 1.9, the date matters. And we encourage to try the  
> most recent version from CVS HEAD. It’s 2006-09-18 today.
> 
> ```
> matz.
> 
> ```

So does this mean that Why’s excellent talk at RailsConf Europe is  
already out of date?

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 18, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/16 "2006-09-18T16:46:15Z")

</div>

Hi,

In message “Re: Splat, #to\_ary and #to\_a”  
on Mon, 18 Sep 2006 17:54:32 +0900, “Robert D.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Is 1.9.0 good enough?, because I get  
|\>  
|ruby1.9 -ve ‘p [\*“foo\nbar”]’  
|–\>ruby 1.9.0 (2006-04-21) [i486-linux]

When we talk about 1.9, the date matters. And we encourage to try the  
most recent version from CVS HEAD. It’s 2006-09-18 today.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 19, 2006, 6:21am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/17 "2006-09-19T06:21:25Z")

</div>

unknown wrote:

> Hi –
> 
> On Mon, 18 Sep 2006, Yukihiro M. wrote:
> 
> > |in 1.9?
> > 
> > This may be an issue.
> 
> I’ve always thought it was convenient to have [\*x] do the expanding  
> on x (including strings and ranges). But if it’s going to disappear,  
> I think it should disappear consistently, so that \* doesn’t start  
> having a totally different meaning depending on the operand’s class.

I dunno–you have to use Range#to\_a explicitly elsewhere so I  
feel that would be consistent here also. I think my preferred  
solution would be for splat to use #to\_ary and leave everything  
else explicit.

Of course this is a very limited application of the splat operator.

> David

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 19, 2006, 4:17am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/18 "2006-09-19T04:17:35Z")

</div>

Hi,

In message “Re: Splat, #to\_ary and #to\_a”  
on Tue, 19 Sep 2006 01:36:22 +0900, “J2M” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|\> When we talk about 1.9, the date matters. And we encourage to try the  
|\> most recent version from CVS HEAD. It’s 2006-09-18 today.

|So does this mean that Why’s excellent talk at RailsConf Europe is  
|already out of date?

I don’t know. I haven’t had a luck to hear his “excellent talk” yet.  
Could anybody elaborate?

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 19, 2006, 7:02am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/19 "2006-09-19T07:02:03Z")

</div>

Hi –

On Tue, 19 Sep 2006, Eero S. wrote:

> > on x (including strings and ranges). But if it’s going to disappear,  
> > I think it should disappear consistently, so that \* doesn’t start  
> > having a totally different meaning depending on the operand’s class.
> 
> I dunno–you have to use Range#to\_a explicitly elsewhere so I  
> feel that would be consistent here also.

My point is that I don’t want to have to use Range#to\_a explicitly but  
not String#to\_a, nor the other way around. In other words, I don’t  
want \*(0…10) to do one thing and \*“a\nb” to do another. It’s not  
that I’m hung up on consistency per se (lots of cool Ruby things come  
from inconsistency 🙂 but I don’t like things where there are two  
cases and no clear reason why, so you have to memorize them  
individually.

David

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [September 19, 2006, 10:28am UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/20 "2006-09-19T10:28:23Z")

</div>

On 9/18/06, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> |–\>ruby 1.9.0 (2006-04-21) [i486-linux]
> 
> When we talk about 1.9, the date matters. And we encourage to try the  
> most recent version from CVS HEAD. It’s 2006-09-18 today.
> 
> ```
> matz.
> 
> ```

Heading right for it 🙂  
Thx  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

[Next page](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005.md?page=2)
