Sunday, 29 September 2013

nested assignment in ruby

nested assignment in ruby

Why do these assignment happen?
a,(b,c),d = 1,2,3,4 # a=1, b=2,c=nil, d=3
why is d=3 and c=nil?
yet on this
a,(b,c),d = 1,[2,3],4 # a=1, b=2,c=3, d=4
c=3 and d=4?
this is ok since its 2 arugments vs 3 values
a,(b,c),d = 1,[2,3,4],5 # a=1, b=2,c=3, d=5
and this seems logical, since its with a splat
a,(b,*c),d = 1,[2,3,4],5 # a=1, b=2,c=[3,4],d=5

No comments:

Post a Comment