ruby's Parallel assignment to assign same value over simple assignment -


is there difference between:

1.

> lines, codelines, total_lines, total_codelines = 0, 0, 0, 0 => [0, 0, 0, 0] 

2.

> lines, codelines, total_lines, total_codelines = 0 => 0 

if yes, of them should used in situations?

[edit] there third way:

    lines = codelines = total_lines = total_codelines = 0  > lines = codelines = total_lines = total_codelines = 0 => 0 irb(main):016:0> lines => 0 irb(main):017:0> codelines => 0 irb(main):018:0> total_lines => 0 irb(main):019:0> total_codelines 

in case of arrays

0> = b = { }; a[:a] = 6 => 6 irb(main):023:0> b = 3 => 3 irb(main):024:0> => {:a=>6} irb(main):025:0> = 10 => 10 irb(main):026:0> b => 3 > a.object_id => 21 irb(main):028:0> b.object_id => 7 

there difference.

1.

lines, codelines, total_lines, total_codelines = 0, 0, 0, 0 

has many values on right side there variables on left. each value assigned each variable.

2.

lines, codelines, total_lines, total_codelines = 0 

does not have many values on right side there variables on left. missing values supplemented nil. lines become 0, rest become nil. there no reason should use assignment this.

a typical case when want use multiple variables less number of values on right when have array not know how many elements has.

a, b, *c = some_array 

in case above, depending on length of some_array, a, b, c have different things assigned.


Comments