How does a Ruby array work?

When i introduce

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

He will output 1.

Why is this happening?

+4
source share
1 answer
  • [2,1]creates an array with two elements ( 2, 1)
  • [[2,1],3,4]creates an array with three elements ( [2,1], 3and 4)
  • [1]indexes this array and returns the second element 3(indexing is based on 0)
  • [1]index the second bit of a number 3(represented in binary as 11) - aka1
+14
source

Source: https://habr.com/ru/post/1656548/


All Articles