3

I just started learning Ruby today and learned to create a class and create objects that belong to that class. I'd like to know if there is a way to retrieve all of the names of the instances I've created in said class. Ex)

class Clothes
  attr_accessor:color
end

shirt=Clothes.new
#=> #<Clothes:0x007f89208cd330> 
pants=Clothes.new
#=> #<Clothes:0x007f89208c18f0> 
socks=Clothes.new
#=> #<Clothes:0x007f8920861748> 

Is there a command that will list all the object belonging to class Clothes (shirt,pants,socks) by the names I've given them? Thanks!

Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
zmerilla
  • 55
  • 4
  • possible duplicate of [How to get class instances in Ruby?](http://stackoverflow.com/questions/6365638/how-to-get-class-instances-in-ruby) – tokland Feb 27 '13 at 21:14
  • It's not a dupe, he wants to get the names of the variables, not all instances of the class. – Patrick Oscity Feb 27 '13 at 21:18
  • If you've found one of these answers particularly helpful, please don't forget to accept the answer. – wvm2008 Feb 27 '13 at 23:42

5 Answers5

2

Disclaimer: what you're trying to do can be most likely solved much more elegantly.

I would simply store the objects i want to remember in an array. Nevertheless, here's a solution to your question:

local_variables.select{|v| eval(v.to_s).class == Clothes }
#=> [:socks, :pants, :shirt]
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
1

I'd recommend using a class variable to collect the names of all your instances, and also passing in some kind of symbol in the initialize method.

class Clothes
    attr_accessor :color

    @@instances = []
    def initialize(name)
        @@instances << name
    end

    def all
       puts @@instances
    end
end

shirt = Clothes.new(:shirt)
pants = Clothes.new(:pants)

puts pants.all
# outputs the names
wvm2008
  • 2,864
  • 4
  • 21
  • 25
0

No there isn't. However you can achieve something similar, by modifying new method so that it stores all new instances of this class in a class variable. This will give you easy access to all objects of this class, but not to variable names (for which I can't really find a need anyway)

Mchl
  • 61,444
  • 9
  • 118
  • 120
  • 1
    you can iterate over the `Clothes` objects with `ObjectSpace.each_object(Clothes)` but I don't think you can really do what the OP wants. – Kyle Feb 27 '13 at 21:04
  • I'm just starting out so I don't really have a necessity for anything I'm doing haha, just trying to learn the language. Thanks for your input. – zmerilla Feb 27 '13 at 21:15
0

You can use ruby ObjectSpace to collect all instance/objects of class the each_object method will help you do this

eg

all = []
ObjectSpace.each_object Clothes {|cloth| all << cloth }
AnkitG
  • 6,438
  • 7
  • 44
  • 72
  • it will return all *instances* but not the *names of the variables* – Patrick Oscity Feb 27 '13 at 21:10
  • @padde yeah it will return all Objects that's what he asked. If he wants to have clothes types may be he should encapsulate and make a cloth type attribute – AnkitG Feb 27 '13 at 21:13
  • Okay thanks ankit. @padde from what I'm understanding reading previous posts is what I'm asking isn't possible, and it doesn't have much use. I'm just starting out so I don't have a real objective, just trying to figure things out for now. – zmerilla Feb 27 '13 at 21:13
0

You can do it with instance variables:

class Clothes; end
@a = Clothes.new
@b = Clothes.new
@c = 1

p instance_variables.select{|v|instance_variable_get(v).class == Clothes}
#=>[:@a, :@b]
steenslag
  • 79,051
  • 16
  • 138
  • 171