How to Get Class Instances in Ruby

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

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 get the name of a Ruby class?

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

result.class.name

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

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"]

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]

How to add an instance of a class into an array of another class (Ruby)

You can use the ObjectSpace module with each_object method inside your Car class to define method to keep track for each living instances, like this:

class Car

def self.all
ObjectSpace.each_object(self).to_a
end
end
require_relative 'car' # Makes car class accessible in garage.rb 

class Garage
def initialize
@capacity = Car.all
end

attr_accessor :capacity
end
car1 = Car.new        
car2 = Car.new
car3 = Car.new

garage = Garage.new

print garage.capacity # => [#<Car:0x0000563fa14128c8>, #<Car:0x0000563fa1412918>, #<Car:0x0000563fa1412990>]

Also note, I have used require_relative to include car.rb class which is in the same directory.


Edit for comment question and Cary Swoveland suggestion:

class Car
end
require_relative 'car' # Makes car class accessible in garage.rb 

class Garage
def initialize; end

def capacity
ObjectSpace.each_object(Car).to_a
end
end

car1 = Car.new
garage = Garage.new
car2 = Car.new

print garage.capacity # => [#<Car:0x000055d7bd236a00>, #<Car:0x000055d7bd236b18>]

Also note, I removed attr_accessor :capacity, since you don't need it in this case.

How to get the class name from an instance variable in Rails?

Use @commentable.class.name to find out the variable's class name.

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


Related Topics



Leave a reply



Submit