Is there a difference between:
是否有区别:
1.
1。
> lines, codelines, total_lines, total_codelines = 0, 0, 0, 0
=> [0, 0, 0, 0]
2.
2。
> lines, codelines, total_lines, total_codelines = 0
=> 0
If yes, then which of them should be used in what situations?
如果是,那么在哪些情况下应该使用哪一个呢?
[edit] there is also a 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> a = b = { }; a[:a] = 6
=> 6
irb(main):023:0> b = 3
=> 3
irb(main):024:0> a
=> {:a=>6}
irb(main):025:0> a = 10
=> 10
irb(main):026:0> b
=> 3
> a.object_id
=> 21
irb(main):028:0> b.object_id
=> 7
2 个解决方案
#1
1
There is a difference.
它们是有区别的。
1.
1。
lines, codelines, total_lines, total_codelines = 0, 0, 0, 0
has as many values on the right side as there are variables on the left. Each value will be assigned to each variable.
右边有多少个值,左边有变量。每个值都将分配给每个变量。
2.
2。
lines, codelines, total_lines, total_codelines = 0
does not have as many values on the right side as there are variables on the left. The missing values will be supplemented as nil
. lines
will become 0
, the rest will become nil
. There is no reason you should use assignment like this.
左侧没有尽可能多的值,左侧有变量。缺失值将补充为零。线条将变为0,其余线条将变为零。没有理由你应该使用这样的作业。
A typical case when you want to use multiple variables with less number of values on the right is when you have an array that you do not know how many elements it has.
当您想要在右侧使用具有较少数值的多个变量时,典型的情况是当您拥有一个不知道它有多少元素的数组时。
a, b, *c = some_array
In the case above, depending on the length of some_array
, a
, b
, c
will have different things assigned.
在上面的例子中,根据some_array的长度,a,b,c将分配不同的东西。
#2
2
The difference between those two is that the second example is only assigning 0
to lines
.
这两者之间的区别在于第二个例子只是为行分配0。
lines, codelines, total_lines, total_codelines = 0,0,0,0
codelines #=> 0
Whereas when the amount of rvalues in the parallel assignment is smaller than the amount of lvalues:
而当并行分配中的右值数量小于左值数量时:
lines, codelines, total_lines, total_codelines = 0
codelines #=> nil
The surplus values are assigned with nil
剩余值分配为nil
So, the second type of parallel assignment does not really make sense unless you need to initialize a variable with nil
in a particular scope; for example a counter outside an iteration.
因此,第二种类型的并行赋值实际上没有意义,除非您需要在特定范围内使用nil初始化变量;例如迭代之外的计数器。
#1
1
There is a difference.
它们是有区别的。
1.
1。
lines, codelines, total_lines, total_codelines = 0, 0, 0, 0
has as many values on the right side as there are variables on the left. Each value will be assigned to each variable.
右边有多少个值,左边有变量。每个值都将分配给每个变量。
2.
2。
lines, codelines, total_lines, total_codelines = 0
does not have as many values on the right side as there are variables on the left. The missing values will be supplemented as nil
. lines
will become 0
, the rest will become nil
. There is no reason you should use assignment like this.
左侧没有尽可能多的值,左侧有变量。缺失值将补充为零。线条将变为0,其余线条将变为零。没有理由你应该使用这样的作业。
A typical case when you want to use multiple variables with less number of values on the right is when you have an array that you do not know how many elements it has.
当您想要在右侧使用具有较少数值的多个变量时,典型的情况是当您拥有一个不知道它有多少元素的数组时。
a, b, *c = some_array
In the case above, depending on the length of some_array
, a
, b
, c
will have different things assigned.
在上面的例子中,根据some_array的长度,a,b,c将分配不同的东西。
#2
2
The difference between those two is that the second example is only assigning 0
to lines
.
这两者之间的区别在于第二个例子只是为行分配0。
lines, codelines, total_lines, total_codelines = 0,0,0,0
codelines #=> 0
Whereas when the amount of rvalues in the parallel assignment is smaller than the amount of lvalues:
而当并行分配中的右值数量小于左值数量时:
lines, codelines, total_lines, total_codelines = 0
codelines #=> nil
The surplus values are assigned with nil
剩余值分配为nil
So, the second type of parallel assignment does not really make sense unless you need to initialize a variable with nil
in a particular scope; for example a counter outside an iteration.
因此,第二种类型的并行赋值实际上没有意义,除非您需要在特定范围内使用nil初始化变量;例如迭代之外的计数器。