Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby:从数组的多个赋值中丢弃变量_Ruby - Fatal编程技术网

Ruby:从数组的多个赋值中丢弃变量

Ruby:从数组的多个赋值中丢弃变量,ruby,Ruby,对于数组中给定的变量赋值: a,b,c,d,e = [ "foo","bar","discard","discard","blah" ] 有没有办法只分配a、b和e并放弃c和d?perl()中是否有类似的undef。是 a,b,_,_,e = [ "foo","bar","discard","discard","blah" ] 您可以使用splat操作符 a,b,*,e = ["foo","bar",nil,nil,"blah"] 如果阵列较长,则这是一个备选方案: a,b,e = [

对于数组中给定的变量赋值:

a,b,c,d,e = [ "foo","bar","discard","discard","blah" ]
有没有办法只分配
a
b
e
并放弃
c
d
?perl()中是否有类似的
undef

a,b,_,_,e = [ "foo","bar","discard","discard","blah" ]
您可以使用splat操作符

 a,b,*,e = ["foo","bar",nil,nil,"blah"]

如果阵列较长,则这是一个备选方案:

a,b,e = [ "foo","bar","discard","discard","blah" ].values_at(0,1,4)

据我所知,u只是一个标准变量?这和使用任意变量有什么区别吗<代码>a,b,未定义,未定义,e?@按Nallways
只是另一个变量名。它通常用于表示未使用的变量(例如,当您需要提供一个包含不需要的参数的块时)。
\uu
不会导致有关未使用变量的警告(当在启用警告的情况下运行时;
ruby-w
),有时可以方便地在数组中捕获一些值。例如,
a,b,*c,e=[“foo”,“bar”,“discard”,“discard”,“blah”]
c#=>[“discard”,“discard”]
。@PressingOnAlways:Ruby专门为这种扔掉的情况设计的。我以前从未见过这种用法。有趣。