1

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?

1
  • 1
    What do sections, questions contains? and What is the expected output? Commented Nov 23, 2013 at 8:18

2 Answers 2

3

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"]
Sign up to request clarification or add additional context in comments.

Comments

2

Then do below with the help of Enumerable#zip:

topic.sections.zip(topic.questions) do |section,question|
    p section
    p question
end

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.