grinferno
grinferno

Reputation: 534

Ruby: How to pass the object name to an array rather than the ID

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.

Upvotes: 1

Views: 1386

Answers (3)

HolyMoly
HolyMoly

Reputation: 2080

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.

Upvotes: 0

raorao
raorao

Reputation: 156

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.

Upvotes: 1

joncon
joncon

Reputation: 105

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

Upvotes: 0

Related Questions