[Ruby] Explanation for beginners of iterative processing with subscripts such as each_with_index!

Do you know the iterative process with subscripts?

Subscripted iterations are the each_with_index and with_index methods.

By using these, you can increase the repertoire of how to use information, such as expressing arrays in an easy-to-understand manner for humans, so please understand it!

Explain each_with_index!

each_with_index is a method that allows you to add subscripts to array elements.

That said, it's confusing, so let's check the code.

fruits = ['orange', 'apple', 'melon']

 #0 for i in the argument of the lower block,1,2...And the element subscript

fruits.each_with_index {|fruit, i| puts "#{i}: #{fruits}"}

#=> 0: apple
#   1: orange
#   2: melon

First, pass an array to a variable called fruits.

Then use the each_with_index method for fruits and define the block with do and end omitted.

When it is output, the subscript is entered as defined for each element.

By defining puts and subsequent puts in the puts block well, you can output contents that are easy for humans to understand.

Explain with_index that can be combined in various ways!

In fact, there are other ways to add subscripts to array elements besides methods like the each_with_index method.

It's the with_index method.

In addition to each, this method can be applied to methods such as map and delete_if. And you can add subscripts when using those methods.

The method used is almost the same as each_with_index.

fruits = ['orange', 'apple', 'melon']

fruits.delete_if.with_index {|fruits, i| fruit.include?('a') && i.odd?}

#=> ["apple", "melon"]

As you can see, just use the with_index method for methods such as delete_if.

Summary

How was it?

You may also use such methods, so it may be useful to know.

We will continue to write articles about basic knowledge for beginners with the goal of one article per day, so thank you.

Recommended Posts

[Ruby] Explanation for beginners of iterative processing with subscripts such as each_with_index!
Explanation of Ruby on rails for beginners ①
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
[Beginner] Various iterative processing for Ruby arrays
How to insert processing with any number of elements in iterative processing in Ruby
[Ruby] Iterative processing
Ruby Iterative Processing
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
Scraping for beginners (Ruby)
Notify Slack of AWS bills daily with Lambda for Ruby
Ruby on Rails for beginners! !! Summary of new posting functions
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
[Ruby ~ Iterative processing ~] Study memo 4
[Android] Dynamically set the height of ListView with copy (for beginners)