2

How do I use strings or symbols to create new variables || objects? Say I want 5 unique objects of an already made item class;

for x in 1..5
    item_x = item.new()   #where x is obviously the number value of the iterator
end

I have tried using eval() in this manner:

for x in 1..5
    eval( "item_" << x << "= item.new()")
end

Hoping that putting the string I want executed into eval would have it executed as if I put it into the code.

I have searched for dynamic object creation and have not found anyone with this problem, sorry if this is mundane stuff. I have found references to people using .const_get and Openstruct but these don't seem to fix my problem in a manner I can understand.

1
  • 3
    This is a PHP/Perl technique that isn't supported by Ruby, and is usually discouraged because it is really hard to maintain and debug. "Hey! Where did item_x come from!? It's not in the source code anywhere!?" I'd recommend rethinking how you can store data in a container, like an array or hash, and access the data how you need. You might want to explain the goal, and we can help you pick better structures, rather than ask for help in what isn't a good direction. Commented Jun 16, 2013 at 2:06

2 Answers 2

4

Is there any reason you can't store your objects in an array? That would make them easier to both create and access:

items = []

5.times do
  items << Item.new()
end

Then instead of item_1 through item_5, you have item[0] through item[4].

That said, if you really need/want to do the hard thing, instance_variable_set is an option if instance variables (with @) are okay:

for x in 1..5
  self.instance_variable_set("@item_#{x}", Item.new())
end

I'd recommend just sticking with the array.

Update: From your comment, it sounds like your actual use case involves desired variable names that aren't consecutive. In that case, I'd use a Hash rather than an Array.

Think of it this way: whatever string you wish to use as the name of a local variable, just use it as a key in a local Hash. I can't think of any instance where that wouldn't be better than what you're trying, even if what you're trying worked.

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

4 Comments

In that example yes. What I'm trying to do is create objects for radioactive isotopes as they become necessary, that is as other isotopes decay into them. I have a global hash that has the names of the daughter isotopes for each decaying isotope, and that is why I want to be able to create objects using strings. I could potentially create many different classes for each isotope, but that is highly impractical.
@user2489835 That still doesn't make sense to me. I think a Hash makes the most sense. (Please see my update.)
Yes to hashes. No to dynamically creating objects you then won't be able to write code about. Make your life easier for yourself.
Yes, thank you. Your comments helped me reevaluate my approach. Your time and help was much appreciated, have nice days.
2

You can use instance_variable_set to create instance variables in the manner you depict:

for x in 1..5
    variable_name = "item_#{x}"
    instance_variable_set("@#{variable_name}", "value to assign to variable")
end

puts @item_1 #=> "value assigned to item_1"

Note, however, that this will not create local variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.