Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Discarding Variables from Multiple Assignment from Array

Tags:

ruby

For the given variable assignment from array:

a,b,c,d,e = [ "foo","bar","discard","discard","blah" ]

Is there a way to only have a, b, and e assigned and discard c and d? Is there an undef like in perl (Perl assignment with a dummy placeholder).

like image 846
PressingOnAlways Avatar asked Feb 13 '26 01:02

PressingOnAlways


2 Answers

Yes.

a,b,_,_,e = [ "foo","bar","discard","discard","blah" ]
like image 67
HM1 Avatar answered Feb 14 '26 16:02

HM1


You can use the splat operator.

 a,b,*,e = ["foo","bar",nil,nil,"blah"]
like image 28
Arup Rakshit Avatar answered Feb 14 '26 17:02

Arup Rakshit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!