vendredi 8 mai 2015

Why "array.length" returns array object when we put it inside array[] ?

ruby

rand(array.length) # returns random index
array[rand(array.length)]# returns random array object

I can't understand the logic behind. I would assume that second example also returns random index and then store it in array.

for example

kitty = [100,102,104,105]

  1. rand(kitty.length) # returns index, for example 3 ( or 0,1,2 )
  2. array[rand(kitty.length)] # returns random array object, for example 104 ( or 100,102,105)



Aucun commentaire:

Enregistrer un commentaire