1

If I create two objects like so:

foo = foo.new bar = bar.new 

and want to store the name of object foo inside an array in bar, how would I do this?

For example if I was passing foo to the following method in bar:

def to_array arg fooarray << arg end 

How can I get it so the method 'to_array' stores the name 'foo' inside the array, rather than the instance ID of the object?

So the array looked like this:

['foo', 'foo2', 'foo3' etc..] 

rather than

[#<foo:0x00..., #foo2:0x00..., etc..] 

I really hope this makes sense. I'm new to Ruby so I might not be making myself clear.

3
  • Maybe build off of this?: stackoverflow.com/questions/826210/… Commented Mar 22, 2015 at 17:26
  • 5
    Why do you want the names? What would you want if you had a = A.new; b = a? Someone wanting to do this sort of thing often indicates some confusion between objects and variables or that they're trying to do something strange. Commented Mar 22, 2015 at 17:41
  • 1
    What is the "name" of an object? Objects don't have names in Ruby. They have an identity, a state and a class, but no names. Commented Mar 22, 2015 at 23:57

3 Answers 3

1

To get the name of an object's class, you can use the class method. like this:

fooOne = Foo.new fooOne.class.to_s # returns the string 'Foo' 

you can define a type method on Foo to make this simpler. As for having this value return when you put a foo object in an array, I agree with the other posters that map is much better alternative.

Sign up to request clarification or add additional context in comments.

Comments

0

I am guessing your are simply trying to keep an array of the variable names? Or is 'name' an attribute of foo. If it is the former, why don't you just keep a running array that you append to at object creation i.e arg would be the variable name not the object. If it the later, 'name' is a method of foo, then you can always use map on the array of objects:

bar = [#<foo:0x00..., #foo2:0x00..., etc..].map(&:name) > ['foo', 'foo1',..., 'fooN'] 

or

def to_array arg fooarray << arg.name end 

2 Comments

You're right! Rather than store the ID in the array I want to store the variable name of the instance. What do you mean when referring to the method 'name'?
If foo is an object than it will have methods. One of those methods could be 'name'.
0

Maybe use a hash instead? It may be a little bit of a pain to add all the objects one at a time depending on how many you have, but the payoff is it will give you what (I think) you want and maybe more....in case you are not familiar with hashes (you said you're new) here is how you do it:

create hash, I will call my hash 'variables'

variables = {} 

add items to hash use: hash_name['key_name'] = 'value'.

variables['var1'] = 'something1' variables['var2'] = 'something2' variables['var3'] = 'something3' 

If you have a variable that looks like this:

foo = 'foobar' #then 'foo' would be the key_name and 'foobar' would be the value. 

Once you hash is created, to see entire hash, both keys and values:

puts variables #=> {"var1"=>"something1", "var2"=>"something2", "var3"=>"something3"} 

look at keys only:

puts variables.keys # => var1 # => var2 # => var3 

to look at values only:

puts variables.values # => something1 # => something2 # => something3 

to see what is in one particular key:

puts variables['var3'] # => something3 

If you for some reason NEEDED an array with only your key(object) names you could you could easily do that by creating an array and pushing all the keys into it at once like this:

all_vars_array = [] all_vars_array << variables.keys #pushes ALL the keys of your hash into your array. puts all_vars_array # => ['var1', 'var2', 'var3'] 

So you see, building a hash may be worth the effort depending on what your doing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.