Ruby x,y = gets.split.map &:to_i -
i confused line of ruby code. why assignment given x, y , not single variable?
x,y = gets.split.map &:to_i
this assigns first entry array x
, , second entry y
in contrast assigning single variable in case array assigned variable.
putting multiple variables on left hand side of assignment way of unpacking array separate variables. can try in irb:
irb(main):001:0> numbers = [1, 2, 3] => [1, 2, 3] irb(main):002:0> first, second = numbers => [1, 2, 3] irb(main):003:0> first => 1 irb(main):004:0> second => 2
check out this answer older question wrote gives more details , example of can useful.
Comments
Post a Comment