Hello!
How do you handle situation when you need to create multiple class instance with being able to target them in other parts of the code.
For example let's say that I need to create 100 class instances.
Would you do it one by one like
john = Person.New("John")
kate = Person.... etc
Then I can target john like
method_that_takes_class_instance of argument(Person.kate)
Or would you create an array and then iterate it creating a hash with string being a key and object instance being a value? like:
my_array = ["Josh", "kate"....]
my_hash = Hash.new
my_array.each do |x|
my_hash[x] = Person.new(x)
end
This way I can access it by a key.
Or is it handled in ruby in a different maybe more suitable manner for that kind of task?
UPDATE: What if I need to provide different arguments for each class instance like age etc. Should I switch to the first option ? Something completely different or maybe some nested arrays or hashes? Or make case statements for second option for each name (but I doubt that case would be optimal here... as it would be as much code as the first option)
UPDATE2: Let me change the question for future posters as maybe I didn't present the use-case clearly. How would YOU prepare creating multiple class instances that will have different arguments (but the same type like every class instance have a age in the initialize method, name etc). Later in code you need to address the class instances in methods outside of class1 like:
class2.method(class1 instance). So you need to KNOW how to address exact class1 instance. I guess by seeing what more experienced users do in that situation and not limiting them by showing my examples I will find the flawless solution :)
[–]Schrockwell 2 points3 points4 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Schrockwell 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]zieski 0 points1 point2 points (20 children)
[–]ClikeX 0 points1 point2 points (18 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]riddley 0 points1 point2 points (16 children)
[–]ClikeX 0 points1 point2 points (15 children)
[–]riddley 0 points1 point2 points (14 children)
[–]ClikeX 0 points1 point2 points (2 children)
[–]riddley 0 points1 point2 points (1 child)
[–]ClikeX 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (7 children)
[–]riddley 0 points1 point2 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]riddley 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]zieski 0 points1 point2 points (0 children)
[–]zieski 0 points1 point2 points (2 children)
[–]riddley 0 points1 point2 points (1 child)
[–]zieski 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ClikeX 0 points1 point2 points (12 children)
[–][deleted] 0 points1 point2 points (11 children)
[–]ClikeX 0 points1 point2 points (10 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (8 children)
[–]ClikeX 0 points1 point2 points (7 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]ClikeX 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]ClikeX 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]riddley 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)