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:
- bread
- sugar
- flour
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:
["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.