I don’t know how exactly to word this, but I am trying to define many variables and then redefine them without rewriting each of the many variables and creating redundant code in each new block I write. The variables are defining array elements from multiple databases. Here is a downsized sample what I am working with:
def lots_of_vars(array)
name = array[1]
membership = array[2]
spouse = array[3]
....
lap12 = array[36]
end
def second_block
#database1 => [ "Randy", true, "Nancy", 2, 17, false...
lots_of_vars(database1)
return unless membership
puts "Lap progress for #{name} and #{spouse}: #{lap1}, #{lap2}... #{lap12}..."
end
def third_block
#database2 => [ "Steven", true, nil, 0, 5, false...
lots_of_vars(database2)
return unless spouse.empty? or spouse.nil?
puts "Weekly progress for #{name}: #{lap1}, #{lap5}, #{lap6}, #{lap10}..."
end
The second and third block need all the variables defined from the first block/method. But how do I pass all these variables? One example I read suggested I pass them all as parameters like:
def second_block(name, membership, spouse...)
but this would make just as much of a mess as defining each variable twice in both blocks. What is the simple, dry approach to such a situation?
Please let me know if I need to clarify anything in my question, Thanks.
What you want is to create a Struct, which is a simple class to represent a datastructure. A struct will take its arguments in by position, which is exactly what you want, since you can splat the array into the method call (turn an array into an argument list)
So