Skip to main content
edited tags
Link
VLAZ
  • 29.7k
  • 9
  • 65
  • 88
added 2 characters in body
Source Link
Joshua Pinter
  • 48k
  • 23
  • 262
  • 259

Without using index I was able to destructure the first and second elements of an Array of Arrays like so:

[ [ 1, 2 ], [ 3, 4 ] ].each do |first, second|
  puts second
end
#=> 2
#=> 4

I then needed to get the index for each iteration so I used .with_index and I assumed the index would just get added as the last argument of the block:

[ [ 1, 2 ], [ 3, 4 ] ].each.with_index do |first, second, index|
  puts second
end
#=> 0
#=> 1

However, it was the indexindex. The first value is the entire Array, not destructured.

How do you use .with_index and still destructure the Array?

Without using index I was able to destructure the first and second elements of an Array of Arrays like so:

[ [ 1, 2 ], [ 3, 4 ] ].each do |first, second|
  puts second
end
#=> 2
#=> 4

I then needed to get the index for each iteration so I used .with_index and I assumed the index would just get added as the last argument of the block:

[ [ 1, 2 ], [ 3, 4 ] ].each.with_index do |first, second, index|
  puts second
end
#=> 0
#=> 1

However, it was the index. The first value is the entire Array, not destructured.

How do you use .with_index and still destructure the Array?

Without using index I was able to destructure the first and second elements of an Array of Arrays like so:

[ [ 1, 2 ], [ 3, 4 ] ].each do |first, second|
  puts second
end
#=> 2
#=> 4

I then needed to get the index for each iteration so I used .with_index and I assumed the index would just get added as the last argument of the block:

[ [ 1, 2 ], [ 3, 4 ] ].each.with_index do |first, second, index|
  puts second
end
#=> 0
#=> 1

However, it was the index. The first value is the entire Array, not destructured.

How do you use .with_index and still destructure the Array?

deleted 6 characters in body; edited tags
Source Link
jonrsharpe
  • 123.8k
  • 31
  • 281
  • 491

Without using index I was able to destructure the first and second elements of an Array of Arrays like so:

[ [ 1, 2], [ 3, 4 ] ].each do |first, second|
  puts second
end
#=> 2
#=> 4
[ [ 1, 2 ], [ 3, 4 ] ].each do |first, second|
  puts second
end
#=> 2
#=> 4

I then needed to get the index for each iteration so I used .with_index and I assumed the index would just get added as the last argument of the block:

[ [ 1, 2], [ 3, 4 ] ].each.with_index do |first, second, index|
  puts second
end
#=> 0
#=> 1
[ [ 1, 2 ], [ 3, 4 ] ].each.with_index do |first, second, index|
  puts second
end
#=> 0
#=> 1

However, it was the index. The first value is the entire Array, not destructured.

How do you use .with_index and still destructure the Array?

Without using index I was able to destructure the first and second elements of an Array of Arrays like so:

[ [ 1, 2], [ 3, 4 ] ].each do |first, second|
  puts second
end
#=> 2
#=> 4

I then needed to get the index for each iteration so I used .with_index and I assumed the index would just get added as the last argument of the block:

[ [ 1, 2], [ 3, 4 ] ].each.with_index do |first, second, index|
  puts second
end
#=> 0
#=> 1

However, it was the index. The first value is the entire Array, not destructured.

How do you use .with_index and still destructure the Array?

Without using index I was able to destructure the first and second elements of an Array of Arrays like so:

[ [ 1, 2 ], [ 3, 4 ] ].each do |first, second|
  puts second
end
#=> 2
#=> 4

I then needed to get the index for each iteration so I used .with_index and I assumed the index would just get added as the last argument of the block:

[ [ 1, 2 ], [ 3, 4 ] ].each.with_index do |first, second, index|
  puts second
end
#=> 0
#=> 1

However, it was the index. The first value is the entire Array, not destructured.

How do you use .with_index and still destructure the Array?

Source Link
Joshua Pinter
  • 48k
  • 23
  • 262
  • 259
Loading