# 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:** 6\
**Page:** 2

<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:46pm UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/21 "2006-09-19T19:46:44Z")

</div>

Rick DeNatale wrote:

> Sorry I haven’t installed 1.9 myself, so I’m wondering if something like
> 
> (1…10).to\_a.values\_at(3…5)
> 
> still works the same way as it does in 1.8 (returning [4, 5, 6]) or  
> throws an exception saying that it can’t convert a range to an integer  
> similar to the way 1.8 does with:  
> (1…10).to\_a.values\_at([3, 4, 5])

$ ruby19 -v && ruby19 -e “p (1…10).to\_a.values\_at(3…5)”  
ruby 1.9.0 (2006-09-19) [i686-linux]  
[4, 5, 6]

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 19, 2006, 7:28pm UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/22 "2006-09-19T19:28:41Z")

</div>

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

=== A reply to my questions ===

Thanks Matz!

Summarizing Matz’ reply to my use cases:

1.9 here means the version of 1.9 as of Matz’ reply:

Use case

Splat formal parameter.

def a(\*arg)  
p arg  
end

a(“foo\nbar”)  
1.8 [“foo\nbar”]  
1.9 [“foo\nbar”]

a((1…3))  
1.8 [1…3] Note: I think I had a typo on this one in my original  
post.  
1.9 [1…3]

a(1…3)  
1.8 [1…3]  
1.9 [1…3]

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

So here, once my typo is accounted for, there’s no difference.

Parallel assignment  
\*a = “foo\nbar”  
1.8 [“foo\nbar”]  
1.9 [“foo\nbar”]  
\*a = (1…4)  
1.8 [1…4]  
1.8 [1…4]  
\*a = “foo\nbar”, (1…3)  
1.8 [“foo\nbar”, 1…3]  
1.9 [“foo\nbar”, 1…3]

Again no changes here.

Splat’s in array literal:  
[\*“foo\nbar”]  
1.8 [“foo\n”, “bar”]  
1.9 [“foo\nbar”]

[\*(1…4)]  
1.8 [1, 2, 3, 4]  
1.9 [1…4]

Here the difference seems to be that in 1.8 the splat had an effect  
but in 1.9 it doesn’t, at least for strings and ranges. Personally  
this seems like the wrong direction to me, but that’s a gut reaction.

The reason I THINK that it’s wrong is that it looks like it seems to  
ignore the explicit request represented by the \*. Now I can  
understand that it might be confusing in a case like this:

def b(str)  
[\*str]  
end

b(“a”) #=\> [“a”]  
b(“foo\nbar”) #=\> [“foo\n”, “bar”]

But I’m not convinced that it’s a net positive change. Convincable,  
but not convinced.

method call with splat argument

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

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

Here again, it looks like an explicit request to splat the parameter  
is being ignored in 1.9.

Note that this very case came up recently in a discussion of expanding  
an array in the arguments to Array#values\_at like the method does  
itself for range arguments.

Sorry I haven’t installed 1.9 myself, so I’m wondering if something like

(1…10).to\_a.values\_at(3…5)

still works the same way as it does in 1.8 (returning [4, 5, 6]) or  
throws an exception saying that it can’t convert a range to an integer  
similar to the way 1.8 does with:  
(1…10).to\_a.values\_at([3, 4, 5])

–  
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 19, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/23 "2006-09-19T20:00:48Z")

</div>

Just for posterity, it may be worth pointing what Eero hinted at. If  
you add a to\_ary method to Range / String in 1.9, then you get the old  
behavior for the use cases that show differences (e.g., [\*(1…10)] ):

class Range  
def to\_ary  
self.inject([]) { |ary, item| ary \<\< item }  
end  
end

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 19, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/24 "2006-09-19T19:54:46Z")

</div>

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

> $ ruby19 -v && ruby19 -e “p (1…10).to\_a.values\_at(3…5)”  
> ruby 1.9.0 (2006-09-19) [i686-linux]  
> [4, 5, 6]
> 
> Regards,  
> Jordan

Thanks, I guess I’m going to have to find the time to install 1.9.

Those darn clients take up too much time with their problems! And in  
this case it’s problems with a big wad o’php I just inherited. A sorry  
mess which is a prime candidate for takeover by assimilation by ruby  
code, once I understand enough of what it’s supposed to do.

–  
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 20, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/25 "2006-09-20T19:23:20Z")

</div>

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

> Just for posterity, it may be worth pointing what Eero hinted at. If  
> you add a to\_ary method to Range / String in 1.9, then you get the old  
> behavior for the use cases that show differences (e.g., [\*(1…10)] ):

Yes, I somehow missed the TITLE of this thread!

–  
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 19, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/splat-to-ary-and-to-a/69005/26 "2006-09-19T20:06:49Z")

</div>

Rick DeNatale wrote:

> Those darn clients take up too much time with their problems! And in  
> this case it’s problems with a big wad o’php I just inherited. A sorry  
> mess which is a prime candidate for takeover by assimilation by ruby  
> code, once I understand enough of what it’s supposed to do.

“Resistance is futile; we will take your knowledge and add it to the  
collective. You will be assimilated” hehe! 😉

Regards,  
Jordan

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