# [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.