[Rails] How to use the map method

I learned about the map method, so I will output it.

Read this article to learn how to use the map method.

What is the map method?

"Take out the contents of the array one by one and repeat the syntax of blocking. Then collect the return values of the blocks and create a new array. "

I will explain in detail from now on.

For example, suppose you have products for 100 yen, 200 yen, and 300 yen, and you want to tax all products.


items_price = [100, 200, 300] #Array of three prices
tax = 1.1
items_add_tax = items_price.map{|item_price| item_price * tax}
#Take out the values in the array one by one, ✖️1.Do 1
put items_add_tax 
=> [110, 220, 330]
#Create a new array with taxes added

Summary

How to use the map method


Array object.map {|ele|Block processing}
  #Array elements are assigned to ele one by one
  #Block processing is repeated as many times as there are elements in the array

Recommended Posts

[Rails] How to use the map method
How to use the form_with method
How to use Map
How to use map
How to use Map
[Java] How to use the toString () method
[Rails] How to use helper method, confimartion
[Java] How to use Map
[Java] How to use Map
[Rails] How to use enum
[Rails] How to use enum
Output of how to use the slice method
How to use rails join
How to use Java Map
How to use the replace () method (Java Silver)
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Ruby on Rails] How to use session method
[Rails] How to use Scope
[Ruby basics] How to use the slice method
[rails] How to use devise helper method before_action: authenticate_user!
[Rails] I don't know how to use the model ...
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[Java] How to use join method
How to use the wrapper class
How to use Ruby on Rails
[Ruby] How to use any? Method
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use Ruby inject method
How to use the getter / setter method (in object orientation)
[Ruby on Rails] How to use CarrierWave
[Rails] How to use rails console with docker
[Java] How to use the HashMap class
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use MySQL in Rails tutorial
[Processing × Java] How to use the class
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Processing × Java] How to use the function
[Ruby on Rails] How to use kaminari
[Java] How to use the Calendar class
[Rails] How to use video_tag to display videos
How to use credentials.yml.enc introduced in Rails 5.2
I tried to understand how the rails method "redirect_to" is defined
[Rails] How to omit the display of the character string of the link_to method
I tried to understand how the rails method "link_to" is defined
[Rails] Don't use the select method just to narrow down the columns!
[Ruby on Rails] Use the resources method to automatically create routes.
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use fields_for
How to use java.util.logging
How to use collection_select
How to use Twitter4J
How to use active_hash! !!