DEV Community

n350071๐Ÿ‡ฏ๐Ÿ‡ต
n350071๐Ÿ‡ฏ๐Ÿ‡ต

Posted on

4

start each_with_index from 1 (This is good for UI)

๐Ÿ‘ Usual way

books.each_with_index do |book, index|
  puts "#{index}: #{book.title}"
end

#=> 0: a
#=> 1: b
#=> 2: c

๐Ÿฆ„ Start from 1

You can use each.with_index(1).

This is good when you use each method in .erb file. Because users want to see index from 1 usually.

books.each.with_index(1) do |book, index|
  puts "#{index}: #{title}"
end

#=> 1: a
#=> 2: b
#=> 3: c

๐Ÿ”— Parent Note

Top comments (0)

Sentry workshop image

Sick of your mobile apps crashing?

Let Simon Grimm show you how to fix them without the guesswork. Join the workshop and get to debugging.

Save your spot โ†’

๐Ÿ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay