[Ruby] I thought about the difference between each_with_index and each.with_index

This article uses Ruby 2.6.5 installed on macOS Catalina 10.15.6. I've summarized them to distinguish between the two.

each_with_index --Each_with_index and __ By passing two values __ to a block variable, you can iterate as many times as there are elements in the __ array __ and __ to indicate what number that element is.

fruits = ["apple", "banana", "peach"]

fruits.each_with_index do |fruit, index|
  puts "#{index}The second fruit is#{fruit}is."
end

result

However, if this is left as it is, it will be "The 0th fruit is apple."

The 0th fruit is apple.
The first fruit is banana.
The second fruit is peach.

each.with_index --I put 1__ in the argument of __index. --Then, the number assigned to the element will start from 1.

fruits.each.with_index(1) do |fruit, index|
  puts "#{index}The second fruit is#{fruit}is."
end

result

As you can see, the order starts from 1 neatly.

The first fruit is apple.
The second fruit is banana.
The third fruit is peach.

Recommended Posts

[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
About the difference between classes and instances in Ruby
Understand the difference between each_with_index and each.with_index
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
About the difference between irb and pry
Difference between i ++ and ++ i
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
[Rails / ActiveRecord] About the difference between create and create!
[Rails] I investigated the difference between redirect_to and render.
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
[Ruby] Difference between get and post
[Ruby] Difference between is_a? And instance_of?
The difference between programming with Ruby classes and programming without it
[Java] I thought about the merits and uses of "interface"
Note: Difference between Ruby "p" and "puts"
About the difference between gets and gets.chomp (other than line breaks)
Difference between Ruby instance variable and local variable
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Ruby] Difference between print, puts and p
Easy to understand the difference between Ruby instance method and class method.
I thought about the best way to create a ValueObject in Ruby
[Rails] I studied the difference between new method, save method, build method and create method.
[iOS] Understand the difference between frame and bounds
Understand the difference between abstract classes and interfaces!
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
[Ruby] Difference between puts and return, output and return value
Difference between vh and%
About the [ruby] operator
What is the difference between a class and a struct? ?? ??
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
What is the difference between System Spec and Feature Spec?
Calculate the difference between numbers in a Ruby array
About the relationship between HTTP methods, actions and CRUD
[Rails] What is the difference between redirect and render?
Compare the difference between dockerfile before and after docker-slim
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between skip and pending? [RSpec]
[Technical memo] About the advantages and disadvantages of Ruby
What is the difference between Java EE and Jakarta EE?
[Swift] UITextField taught me the difference between nil and ""
[Java beginner] Difference between length and length () ~ I don't know ~
[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
About Ruby hashes and symbols
About the same and equivalent
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
[Swift] I thought about compare
Difference between render and redirect_to
Difference between List and ArrayList