How to start a subscript from an arbitrary number in Ruby iterative processing


As the title says, it was personally convenient to start the subscript with an arbitrary number when repeating an array with each_with_index in Ruby, so I will leave it as it is.


Just change ʻeach_with_index to ʻeach.with_index (n). Specify the start number for n. To start the subscript from 1, write as follows.


juices = ["tea", "cola", "coffee"]
juices.each.with_index(1) do |juice, i|
    puts "#{i}Third:#{juice}"

Output result

First: tea
Second: cola
Third: coffee

With ʻeach_with_index, it is necessary to set ʻi + 1, so this method is convenient.

Recommended Posts

How to start a subscript from an arbitrary number in Ruby iterative processing
How to change a string in an array to a number in Ruby
How to insert processing with any number of elements in iterative processing in Ruby
How to get an arbitrary digit from a number of 2 or more digits! !!
How to check if an instance variable is defined in a Ruby class
How to make a judgment method to search for an arbitrary character in an array
How to make an image partially transparent in Processing
How to launch another command in a Ruby program
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
[iOS] [Objective-C] How to update a widget from an Objective-C app
How to retrieve the hash value in an array in Ruby
How to store a string from ArrayList to String in Java (Personal)
How to display a graph in Ruby on Rails (LazyHighChart)
[Ruby] How to batch convert strings in an array to numbers
[Ruby] How to extract a specific value from an array under multiple conditions [select / each]
How to install Bootstrap in Ruby
[Ruby] How to count even or odd numbers in an array
[Ruby] I want to put an array in a variable. I want to convert to an array
How to get and add data from Firebase Firestore in Ruby
How to build an Apache Flink application from scratch in 5 minutes
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
[Ruby / Refactoring] From Ruby iterative processing like Java and C language to Ruby-like iterative processing
How to get date data in Ruby
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Personal memo] How to interact with a random number generator in Java
How to develop from VScode in a remote destination environment or a remote destination container environment
How to insert a video in Rails
Iterative processing of Ruby using each method (find the sum from 1 to 10)
A story about converting character codes from UTF-8 to Shift-jis in Ruby
How to debug the processing in the Ruby on Rails model only on the console
How to implement asynchronous processing in Outsystems
How to publish a library in jCenter
How to start a Docker container with a volume mounted in a batch file
[Swift] How to get the number of elements in an array (super basic)
[Spring Boot] How to get properties dynamically from a string contained in a URL
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
Escape processing when creating a URL in Ruby
Two ways to start a thread in Java + @
How to get a heapdump from a Docker container
How to display a web page in Java
How to get Class from Element in Java
How to run a djUnit task in Ant
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to easily create a pull-down in Rails
[Ruby] How to generate a random alphabet string
How to solve an Expression Problem in Java
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
How to build the simplest blockchain in Ruby
How to build an executable jar in Maven
[ruby] How to receive values from standard input?
How to implement Pagination in GraphQL (for ruby)
How to automatically generate a constructor in Eclipse
[Ruby] Iterative processing
Ruby Iterative Processing
I want to output the character number from the left where an arbitrary character string appears.
[Java] How to get the key and value stored in Map by iterative processing
How to clear all data in a particular table
How to install Ruby on an EC2 instance on AWS
Count the number of occurrences of a string in Ruby