Method for numbering lists

Viewed 33

what method can i use in creating a numbered list of names in ruby. Tried using the each index method but it is not working. for example i want my output to be like this:

  1. bread
  2. sugar
  3. flour
1 Answers
["bread", "sugar", "flour"].each.with_index(1) do |ingredient, idx|
  puts "#{idx}. #{ingredient}"
end

each without a block rerturns an Enumerator. Enumerators have an with_index method which allows you to specify an offset; 1 instead of the default 0 in this case.

Related