my-ruby
February 27, 2013, 10:10pm
1
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?
my-ruby
February 27, 2013, 10:20pm
2
ObjectSpace.each_object(Test).to_a
my-ruby
February 27, 2013, 10:22pm
3
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].
my-ruby
February 27, 2013, 10:37pm
4
Hans M. wrote in post #1099426:
Hash[local_variables.map {|k| [k,eval(k.to_s)]}]
Thanks.
Could you bit explain the above please?
my-ruby
February 27, 2013, 10:34pm
5
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 }
my-ruby
February 27, 2013, 10:42pm
6
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.
my-ruby
February 27, 2013, 10:50pm
7
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
my-ruby
February 27, 2013, 10:40pm
8
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
my-ruby
February 27, 2013, 10:56pm
9
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”]
my-ruby
February 27, 2013, 11:19pm
10
Interesting, try this instead:
Hash[local_variables.map {|k| [k,eval(k.to_s)]}].inspect.scan(/[{
,]:([^=]+)=/).flatten
-Ryan
my-ruby
February 27, 2013, 11:22pm
11
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
my-ruby
February 27, 2013, 11:21pm
12
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 … same as before
my-ruby
February 27, 2013, 11:40pm
13
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]