Object track llist for a particular class

Say I have created more than one instances from a particular class as
below:

class Test

def show

p ‘hi’

end

end

Now say I have created he below objects :

Foo = Test.new
boo = Test.new
loo = Test.new

Now Is there a way to get the list of current objects created on a
particular class say Test here?

ObjectSpace.each_object(Test).to_a

No, It would give me as below :

class Test
end
=> nil

a = Test.new
=> #Test:0x00000001df2f40

b = Test.new
=> #Test:0x00000001e0e0d8

ObjectSpace.each_object(Test).to_a
=> [#Test:0x00000001df2f40, #Test:0x00000001e0e0d8]

But I want as [a,b].

Hans M. wrote in post #1099426:

Hash[local_variables.map {|k| [k,eval(k.to_s)]}]

Thanks.

Could you bit explain the above please?

Hash[local_variables.map {|k| [k,eval(k.to_s)]}]

irb(main):001:0> class Test
irb(main):002:1> end
=> nil
irb(main):003:0> a = Test.new
=> #Test:0x00000002025790
irb(main):004:0> b = Test.new
=> #Test:0x00000001f73d88
irb(main):005:0> Hash[local_variables.map {|k| [k,eval(k.to_s)]}]
=> {:b=>#Test:0x00000001f73d88, :a=>#Test:0x00000002025790}

Hans M. wrote in post #1099429:

why? what is your problem? local_variables returns an Array of symbols,
which are need to be turned into string for eval

the other functions can be googled or with using “ri”, learn how to use
it

Yes, I understood and just checked it from the docs.

Thank you very much. :slight_smile:

Hans,

While your solution is almost perfect, I did notice that he wanted the
output to be in the form of [a,b], so I modified it slightly (forgive my
Regex, it was a quick hack because I know the question urgently needed
an answer):

Hash[local_variables.map {|k| [k,eval(k.to_s)]}].inspect.scan(/(?:{|
):([^=]+)=/).flatten

Love U Ruby, that should return for you the instances in the form of
[“a”,“b”] in your example.

-Ryan

why? what is your problem? local_variables returns an Array of symbols,
which are need to be turned into string for eval

the other functions can be googled or with using “ri”, learn how to use
it

Ryan V. wrote in post #1099432:

Hans,

Hash[local_variables.map {|k| [k,eval(k.to_s)]}].inspect.scan(/(?:{|
):([^=]+)=/).flatten

Love U Ruby, that should return for you the instances in the form of
[“a”,“b”] in your example.

-Ryan

It gives the below:

class Test
end
=> nil

foo = Test.new
=> #Test:0x00000001cdd240

bar = Test.new
=> #Test:0x00000001cdfe50

Hash[local_variables.map {|k| [k,eval(k.to_s)]}].inspect.scan(/(?:{|
):([^=]+)=/).flatten
=> [“bar”]

Not [“foo”,“bar”]

Interesting, try this instead:

Hash[local_variables.map {|k| [k,eval(k.to_s)]}].inspect.scan(/[{
,]:([^=]+)=/).flatten

-Ryan

wh do you do an inspect of an Hash? what does not make any sense!

and your regex matchs toomuch, thats why you get the wrong output

Ryan V. wrote in post #1099438:

Interesting, try this instead:

Hash[local_variables.map {|k| [k,eval(k.to_s)]}].inspect.scan(/[{
,]:([^=]+)=/).flatten

-Ryan

no … :frowning: same as before

Maybe:

local_variables.select{|k| eval(k.to_s).is_a?(Test) }

?

irb(main):001:0> class Test
end
nil
irb(main):003:0> a = Test.new
#Test:0x93f0b0c
irb(main):004:0> b = Test.new
#Test:0x93ecfc0
irb(main):005:0> c = String.new
“”
irb(main):006:0> d = Array.new
[]
irb(main):007:0> local_variables.select{|k| eval(k.to_s).is_a?(Test) }
[:b, :a]