首先
所有的右值被评估,从左到右,并且收集到一个数组中(除非它已经是数组),这个数组将是整个赋值的返回结果
然后
开始检查左边,如果只有一个元素,那么这个数组就是最终的结果
ex: a = 1,2,3,4 # a=[1,2,3,4] => [1,2,3,4] b= [1,2,3,4] # b=[1,2,3,4] =>[1,2,3,4]
如果左边包含逗号,那么他们的值是按顺序对应的
a,b = 1,2,3,4,5 #a=1,b=2 #=>[1,2,3,4] a, = 1,2,3,4,5 #a=1 #=>[1,2,3,4]
星号的赋值
If Ruby sees any splats on the right side of an assignment (that is, rvalues preceded by an asterisk), each will be expanded inline into its constituent values during the evaluation of the rvalues and before the assignment to lvalues starts:
a, b, c, d, e = *(1..2), 3, *[4, 5] # a=1, b=2, c=3, d=4, e=5
如果左边的值带有星号,那么它将会是一个数组,而且它会包含尽可能多的对应右边的值,如果它是位于最后一个的话,那么它将包含已经赋值给前面的值之外的所有值
a,*b = 1,2,3 # a=1 b=[2,3] a,*b = 1 #a=1 b=[]
如果左边带星号的不是最后一个,那么他将得到所有其他左边赋值完成之后剩余的右边的值
*a,b = 1 # a=[],b=1 *a,b = 1,2,3 #a=[1,2],b=3 a,b,*c,d = 1,2,3 #a=1,b=2,c=[],d=3 a,b,c*,d = 1,2,3,4 #a=1 b=2, c=[3],d=4
作为方法参数 使用单个*号可以忽略一些值
a,*,b = 1,2,3,4,5 #a=1 b=5