I learned about the map method, so I will output it.
Read this article to learn how to use 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
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