[Ruby] Various types of each

Introduction

As I was studying, various kinds of each came out. I will write it so as not to forget it. : sunny:

each method

  1. Methods provided for arrays and range objects. Extracts the elements contained in the object.
fruits = [ "apple", "orange","banana"]
 
fruits.each do |fruit|
  p fruit
end

#=>"apple"
#  "orange"
#  "banana"
  1. If you want to retrieve and use the key and value of the array at the same time, set two variables.
fruits = { apple: 100, orange: 200, banana: 300}

fruits.each do |key, value|
  puts "#{key}The price of#{value}It is a circle."
end

#=>"The price of apple is 100 yen."
#  "The price of orange is 200 yen."
#  "The price of banana is 300 yen."

It can also be described as follows. (If you like. When writing a long block, write ⇧ like before, if you want to write compactly, ⇩)

fruits = { apple: 100, orange: 200, banana: 300}

fruits.each { |key, value| puts "#{key}The price of#{value}It is a circle."}

#=>"The price of apple is 100 yen."
#  "The price of orange is 200 yen."
#  "The price of banana is 300 yen."

each_key method

It is used when you want to retrieve only the key.

fruits = { apple: 100, orange: 200, banana: 300}

fruits.each_key {|key|  p "#{key}Price" }

#=>"Apple price"
#  "Price of orange"
#  "banana price"

each_value method

It is used when you want to retrieve only value.

fruits = { apple: 100, orange: 200, banana: 300}

fruits.each_value { |value| p "#{value}Circle"}

#=>"100 yen"
#  "200 yen"
#  "300 yen"

each_with_index method

  1. Use this when you want to use index numbers other than array elements.
fruits = [ "apple", "orange","banana"]
 
fruits.each_with_index do |a,i|
  p "#{i}Of#{a}there is."
end

#=>"There are 0 apples."
#  "There is one orange."
#  "There are two bananas."
  1. You can specify a numerical value to start by setting the initial value of index in the argument. (When you want to start from number ◯.)
fruits = [ "apple", "orange","banana"]
 
fruits.each.with_index(5) do |a,i|
  p "#{i}Of#{a}there is."
end

#=>"There are 5 apples."
#  "There are 6 oranges."
#  "There are 7 bananas."

each_slice method

Specify the number in the argument. By doing so, you can slice the specified number as the name suggests and retrieve the element.

fruits = [ "apple", "orange", "banana", "lemon","lime"]

fruits.each_slice(2) do |fruit|
  p fruit
end

#=>["apple", "orange"]
#  ["banana", "lemon"]
#  ["lime"]

each_line method

It can be taken out separately at the line break.

fruits = "apple\norange\nbanana\nlemon"
fruits.each_line {|line| p line.chomp }

#=>[:apple, :orange, :banana, :lemon]

each_with_object method

Evaluates the object and element of the argument and finally returns the object of the argument.


arr = [1, 2, 3]

result = arr.each_with_object({}) do |item, memo|
  memo[item] = item + 1
end

p result
#=>{1=>2, 2=>3, 3=>4}

`This is further as Fukahori. .. ``

-What is the difference between inject and each_with_object?

Summary

I found that there are various types of friends in each. Let's expand the range that can be remembered little by little ...! !! : fire:

Recommended Posts

[Ruby] Various types of each
[Ruby] Review about nesting of each
[Ruby] each nested
Basics of Ruby
[Ruby] Nesting each
Nested structure of each
definition of ruby method
Various Ruby string operations
[Ruby] How to find the sum of each digit
Basic methods of Ruby hashes
[Swift] Types of types-Basic knowledge-
Basic methods of Ruby arrays
About types of code coverage
Ruby About various iterative processes
[Ruby] List of basic commands
Judgment of fractions in Ruby
Java Learning 1 (learning various data types)
Review of Ruby basic grammar
Basics of sending Gmail in Ruby
Various methods of Java String class
Implementation of ls command in Ruby
Extraction of "ruby" double hash * Review
[Ruby] See the essence of ArgumentError
Basic knowledge of Ruby on Rails
Ruby standard input and various methods
Various methods of the String class
Ruby 5 or higher sum of integers
What is it? ~ 3 types of "no" ~
[Spring Boot] Role of each class
Basics of Ruby ~ Review of confusing parts ~
Ruby / Rust linkage (6) Extraction of morphemes
Impressions of making BlackJack-cli with Ruby
Ruby memorandum (acquisition of key value)
[Rails] Types of associations (one-to-many / many-to-many)
List of types added in Java 9
Explanation about Array object of Ruby
Ruby Basics 2 ~ Review of confusing parts ~
[Ruby] Display the contents of variables
Types of exceptions in business systems
Iterative processing of Ruby using each method (find the sum from 1 to 10)