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.