From: Stefano Crocco Date: 2013-02-23T18:06:52+09:00 Subject: Re: range is not assigning to the splat variable. On Saturday 23 February 2013 Love U Ruby wrote > 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 > #=> [] > > -- > 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