martini-bonanza
martini-bonanza

Reputation: 1591

How can I list all Ruby classes instantiated?

I'd like to list all classes instantiated during one Ruby on Rails API query.

Is there a benchmarking tool or a profiler I could use?

Upvotes: 1

Views: 88

Answers (2)

stefkin
stefkin

Reputation: 679

As an addition to ndn's answer:

ObjectSpace#each_object allows you to iterate through all instances of a specific class. For example:

ObjectSpace.each_object(String) do |object|
  p object
end

ObjectSpace#count_objects shows number of instances of each class.

p ObjectSpace.count_objects

# Result:
# {:TOTAL=>30163,
#  :FREE=>1007,
#  :T_OBJECT=>39,
#  :T_CLASS=>534,
#  :T_MODULE=>24,

Check out this wonderful repository for more useful ruby tricks.

Upvotes: 3

ndnenkov
ndnenkov

Reputation: 36110

Check out Object_space#each_object:

ObjectSpace.each_object.to_a

Upvotes: 3

Related Questions