Grox Ni Grox Ni - 2 months ago 7
Ruby Question

How does Ruby's array work?

When I input


p [[2,1],3,4][1][1]


It will output 1.

Why is that happening?

ndn ndn
Answer
  • [2,1] creates an array with two elements (2, 3)
  • [[2,1],3,4] creates an array with three elements ([2,1], 3 and 4)
  • [1] indexes that array and returns the second element 3 (indexing is 0 based)
  • [1] indexes the second bit of the number 3 (represented in binary as 11) - aka 1
Comments