Reputation: 121
I want to do something like this,
for topic.sections.each do |section| and topic.questions.each do |question|
print section
print question
end
I want both sections and questions simultaneously, the output will be like this: section 1 question 1
section 2 question 2
I know what i did there is foolish, but, what is the exact way to do this or is this even possible?
Upvotes: 1
Views: 1044
Reputation: 118299
Then do below with the help of Enumerable#zip
:
topic.sections.zip(topic.questions) do |section,question|
p section
p question
end
Upvotes: 2
Reputation: 369454
Use Enumerable#zip
.
For example,
sections = ['a', 'b', 'c']
questions = ['q1', 'q2', 'q3']
sections.zip(questions) { |section, question|
p [section, question]
}
# => ["a", "q1"]
# => ["b", "q2"]
# => ["c", "q3"]
Upvotes: 3