Splat, #to_ary and #to_a

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

On 9/18/06, Yukihiro M. [email protected] 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/

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

On 9/19/06, MonkeeSage [email protected] 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/

On 9/19/06, MonkeeSage [email protected] 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/

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! :wink:

Regards,
Jordan