Does the variable know about the array?

If the variable is inside an array, can you only find that array from that variable? Or does the variable not know about the data structure inside?

Just wondering if the following is possible (the only pseudocode is the method v

its_array

):

array = [:a, :b]
array.object_id #=> 11709100 
array_from_inside = []
array.map do |v|
    v.its_array.object_id #=> 11709100 11709100
    array_from_inside = v.its_array #=> [:a, :b] [:a, :b]
end
array_from_inside.object_id #=> 11709100
array_from_inside #=> [:a, :b]

      

+3


source to share


1 answer


First, you don't put variables in arrays. You put objects into arrays, and variables are not objects in Ruby, so you cannot put them into arrays. In fact, since variables are not objects in Ruby, there is almost nothing you can do about them. Ruby is an object oriented language, when you want to do something with something or something, you do it by calling a method on an object or passing an object as an argument to a method. But you can only call methods on objects, and you can only pass objects as arguments, and variables are not objects, so you really can't do that. You can do exactly two things with variables: assign them and dereference them.

Secondly, no, in fact, the object does not and should not know about any container in which it is placed. This more or less negates the whole point of containers: you can put arbitrary objects in them.



Plus, what would you return its_array

for an object without an array? Or is it in two arrays? Or is it in an array that is in an array that is in an array?

+3


source







All Articles