Range is not assigning to the splat variable

Why splat variable couldn’t take in the below two code the “range” ->
(1…8)?

means why a gives []?

*a,b = (1…8)
#=> 1…8
b
#=> 1…8
a
#=> []

means why b gives []?

a,*b = (1…8)
#=> 1…8
a
#=> 1…8
b
#=> []

Splat is an array operation, Range is not an array.

there:

*a,b = *(1…8)#=> [1, 2, 3, 4, 5, 6, 7, 8]

On Saturday 23 February 2013 Love U Ruby wrote

#=> []

Posted via http://www.ruby-forum.com/.

In both case, the right hand side of the assignment is a single object.
When
this happens, the object can be assigned to only one of the variables on
the
left hand side of the assignment. In your first example, the * before a
tells
that a can contain any number of values (including no values). So ruby
assign
the only availlable value (the range) to b and assign an empty array to
a. In
the second case, it’s the opposite.

Arrays and objects responding to #to_ary are an exception to the rule,
as they
are automatically distributed among the variables:

*a,b = (1…8).to_a
#=> [1,2,3,4,5,6,7,8]
b
#=> 8
a
#=> [1,2,3,4,5,6,7]

You can obtain what you want without explicitly converting the range to
an
array using the splat operator:

a,b=(1…8)
#=> [1,2,3,4,5,6,7,8]
b
#=> 8
a
#=> [1,2,3,4,5,6,7]

Stefano

On Sat, 23 Feb 2013 07:08:05 +0100, Matthew K.
[email protected] wrote:

Splat is an array, Rang is not an array.

Matthew, what? This doesn’t even make sense.

On Sat, Feb 23, 2013 at 9:52 AM, Bartosz Dziewoński
[email protected] wrote:

On Sat, 23 Feb 2013 07:08:05 +0100, Matthew K. [email protected]
wrote:

Splat is an array, Rang is not an array.

Matthew, what? This doesn’t even make sense.

It does, because

irb(main):004:0> Range.instance_method :to_ary
NameError: undefined method to_ary' for class Range’
from (irb):4:in instance_method' from (irb):4 from /usr/bin/irb:12:in

and, as our friend would have learned a few days back if he followed
the advice to use set_trace_func to learn what’s going on

irb(main):015:0> o = Object.new
=> #Object:0x802a7a80
irb(main):016:0> def o.to_ary; [1,2,3,4] end
=> nil
irb(main):017:0> a, *b, c = o
=> #Object:0x802a7a80
irb(main):018:0> a
=> 1
irb(main):019:0> b
=> [2, 3]
irb(main):020:0> c
=> 4

Cheers

robert

On Sat, 23 Feb 2013 13:44:25 +0100, Matthew K.
[email protected] wrote:

I know, I know. I wasn’t paying attention while typing and didn’t realise
how bad it was. I edited the post immediately on the forum interface, but
apparently the mailing list version got sent first.

I think it now says something like “Splat is an array operation, Range is
not an array.”

But you can splat a range?

irb(main):004:0> a, b, c = *0…2
=> [0, 1, 2]
irb(main):005:0> a
=> 0
irb(main):006:0> b
=> 1
irb(main):007:0> c
=> 2

Are we misunderstanding each other?

On 23 February 2013 18:52, Bartosz Dziewoński [email protected]
wrote:

On Sat, 23 Feb 2013 07:08:05 +0100, Matthew K. [email protected]
wrote:

Splat is an array, Rang is not an array.

Matthew, what? This doesn’t even make sense.

I know, I know. I wasn’t paying attention while typing and didn’t
realise
how bad it was. I edited the post immediately on the forum interface,
but
apparently the mailing list version got sent first.

I think it now says something like “Splat is an array operation, Range
is
not an array.”

On Sat, Feb 23, 2013 at 2:00 PM, Bartosz Dziewoński
[email protected] wrote:

Are we misunderstanding each other?

You created an explicit implicit Array conversion by using “*” on the
right side. The point Matthew and I were trying to make is that you
cannot do the same with a range without the splat operator on the
right side:

irb(main):004:0> a, b, c = 0…2
=> 0…2
irb(main):005:0> a
=> 0…2
irb(main):006:0> b
=> nil
irb(main):007:0> c
=> nil

Whereas with an Array you can:

irb(main):012:0> a, b, c = [0, 1, 2]
=> [0, 1, 2]
irb(main):013:0> a
=> 0
irb(main):014:0> b
=> 1
irb(main):015:0> c
=> 2

Cheers

robert

Ah. Yes, this makes sense.