Answer the question
In order to leave comments, you need to log in
How to loop through two arrays in ruby?
I'm learning ruby in a course.
There is a task:
Написать программу, которая печатает список товаров и их цены в виде чека. Товары и
их цены хранятся в программе в двух массивах одинаковой длины:
items = ["Chocolate", "Cheese", "Milk", "Bread";]
prices = [3, 15, 4.5, 2.15]
Пример вывода:
Name Price
Chocolate 3.00
Cheese 15.00
Milk 4.50
Bread 2.15
goods = ["Chocolate", "Cheese", "Milk", "Bread"]
prices = [3, 15, 4.50, 2.15]
puts " Name" + " " + "Price"
goods.each do |g|
prices.each do |p|
# puts sprintf("%20s", g.to_s) + " " + sprintf("%6i", p)
puts g.to_s + " " + p.to_s
break
end
end
Name Price
Chocolate 3
Cheese 3
Milk 3
Bread 3
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question