…From Foo instance, it is possible to get the array instance using
self, such with:
Class Foo
def test
puts “the array is: #{self}”
end
end
my_array[8].test # => the array is: (something like fooooobaaaar)
…But is that possible to get its current indice (which is 8) where Foo
is stored? In other words, how can I get the current indice of the array
from Foo instance?
…From Foo instance, it is possible to get the array instance using
…But is that possible to get its current indice (which is 8) where Foo
is stored? In other words, how can I get the current indice of the array
from Foo instance?
An object doesn’t have any built-in notion of what other objects are
referencing it. In your example the array would have a reference to the
Foo instance but the Foo instance doesn’t automatically have any reverse
reference to the array. If you want/need that sort of thing you’ll have
to incorporate it into your implementation of Foo.
It is possible to search an array for a particular object and to return
the index: