How to Find Each Instance of a Class in Ruby

How to find each instance of a class in Ruby

The solution is to use ObjectSpace.each_object method like

ObjectSpace.each_object(Pokemon) {|x| p x}

which produces

<Pokemon:0x0000010098aa70>
<Pokemon:0x00000100992158>
=> 2

Details are discussed in the PickAxe book Chapter 25

How do I list all objects created from a class in Ruby?

You can use the ObjectSpace module to do this, specifically the each_object method.

ObjectSpace.each_object(Project).count

For completeness, here's how you would use that in your class (hat tip to sawa)

class Project
# ...

def self.all
ObjectSpace.each_object(self).to_a
end

def self.count
all.count
end
end

How to get class instances in Ruby?

Illustrating both alphazero's and PreciousBodilyFluids answers:

class Foo
@@instance_collector = []
def initialize
@@instance_collector << self
#other stuff
end
def self.all_offspring
@@instance_collector
end
end

a = Foo.new
b = Foo.new

p Foo.all_offspring # => [#<Foo:0x886d67c>, #<Foo:0x886d668>]
p ObjectSpace.each_object(Foo).to_a # => [#<Foo:0x886d668>, #<Foo:0x886d67c>] #order is different

Get list of a class' instance methods

You actually want TestClass.instance_methods, unless you're interested in what TestClass itself can do.

class TestClass
def method1
end

def method2
end

def method3
end
end

TestClass.methods.grep(/method1/) # => []
TestClass.instance_methods.grep(/method1/) # => ["method1"]
TestClass.methods.grep(/new/) # => ["new"]

Or you can call methods (not instance_methods) on the object:

test_object = TestClass.new
test_object.methods.grep(/method1/) # => ["method1"]

How do I check if a variable is an instance of a class?

It's almost exactly the same. You can use Object's instance_of? method:

"a".instance_of? String # => true
"a".instance_of? Object # => false

Ruby also has the is_a? and kind_of? methods (these 2 are aliases, and work exactly the same), which returns true is one of the superclasses matches:

"a".is_a? String # => true
"a".is_a? Object # => true

How to get all instances variables in ruby class?

You can use instance_variables to collect them in an Array. You will get all initialized instance variables.

class MyClass
attr_accessor :name1
attr_accessor :name2
...
attr_accessor :namen


def inner_func():
all_vars = instance_variables
res = out_func(all_vars)
do_more_stuff(res)
end
end

Look up all descendants of a class in Ruby

Here is an example:

class Parent
def self.descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
end

class Child < Parent
end

class GrandChild < Child
end

puts Parent.descendants
puts Child.descendants

puts Parent.descendants gives you:

GrandChild
Child

puts Child.descendants gives you:

GrandChild

Get all instance variables declared in class

You can use instance_variables:

A.instance_variables

but that’s probably not what you want, since that gets the instance variables in the class A, not an instance of that class. So you probably want:

a = A.new
a.instance_variables

But note that just calling attr_accessor doesn’t define any instance variables (it just defines methods), so there won’t be any in the instance until you set them explicitly.

a = A.new
a.instance_variables #=> []
a.ab = 'foo'
a.instance_variables #=> [:@ab]

Count class instances in array

You can use Enumerable#grep to fetch the instances of Car:

array.grep(Car).size

How do I get the name of a Ruby class?

You want to call .name on the object's class:

result.class.name


Related Topics



Leave a reply



Submit