How to use Ruby inject method

What is the inject method?

Methods that iterate over using blocks Specifically, describe as follows

Array.inject {|initial value,element|Processing content}

Example of use

Sample code

[1,2,3].inject (0){ |sum, array_num| 
   p "array_num=#{array_num}"
   p sum+=array_num
}

Execution result

"array_num=1"
1
"array_num=2"
3
"array_num=3"
6

Recommended Posts

How to use Ruby inject method
[Ruby] How to use any? Method
How to use Ruby return
[Ruby] How to use gsub method and sub method
[Ruby on Rails] How to use session method
[Ruby basics] How to use the slice method
How to use the link_to method
How to use the include? method
How to use the form_with method
[Java] How to use join method
How to use Ruby on Rails
[Ruby on Rails] How to use CarrierWave
How to use submit method (Java Silver)
[Ruby] From the basics to the inject method
[Rails] How to use the map method
[Java] How to use the toString () method
Ruby length, size, count How to use
[Ruby] How to use slice for beginners
[Ruby on Rails] How to use redirect_to
Ruby: CSV :: How to use Table Note
[Rails] How to use helper method, confimartion
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use java.util.stream.Collector
How to use Map
Output of how to use the slice method
How to use the replace () method (Java Silver)
[Java] How to use Map
How to use Chain API
How to use java Optional
How to use JUnit (beginner)
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
Ruby algorithm (inject, method definition)
How to use Swift UIScrollView
[Ruby] How to comment out
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join