ruby map method

What is the map method?

A method that makes changes to an array and returns the array.

How to write

object.map { |variable|
  #The process you want to execute
}

Or

object.map(&:Method name)

Execution example

In case of writing method 1

a=[2,5,3]

b=a.map{ |hoge| 

  hoge*2   
}

#Result b=[4,10,6]

First, 2 in a [0] is assigned to hoge. And with hoge * 2, 4 goes into b [0]. Then 5 in a [1] is assigned to hoge. And with hoge * 2, 10 goes into b [1]. Finally, 3 in a [2] is assigned to hoge. And with hoge * 2, 6 goes into b [2].

The result is an array b = [4,10,6].

How to write 2 Object.map (&: method name) example

a=["Apple","Spotted seal","Deer"]

b=a.map(&:length)

Result b=[3,7,2]

The length method is a method that returns the length of a string.

Recommended Posts

ruby map method
Map method
Ruby to_s method
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
[Practice] Map method
Ruby build method
Ruby Learning # 30 Initialize Method
abbreviation for ruby method
Ruby Learning # 24 Exponent Method
Stream API map method
definition of ruby method
About the map method
[Ruby] Method definition summary
Method
Ruby algorithm (inject, method definition)
[Ruby] Notes on gets method
[Ruby] Method that returns truth
[ruby] Method call with argument
Ruby design pattern template method pattern memo
[Ruby] Method to count specific characters
[Ruby] present/blank method and postfix if.
[Ruby] Extracting elements with slice method
[Ruby basics] split method and to_s method
[Ruby] How to use any? Method
[Ruby] Search problem using index method
[Ruby on Rails] Convenient helper method
[Ruby] undefined method `dark?'occurs in rqr_code
How to use Ruby inject method
Ruby learning 4
[Ruby] Array
Java method
[Ruby] Obtaining even values ​​using the even? Method
to_i method
Ruby learning 5
Ruby basics
java (method)
Ruby Review 2
getRequestDispatcher () method
Ruby addition
Refactoring Ruby
Ruby on Rails installation method [Mac edition]
[Ruby] Simplify each using map and inject
Ruby learning 3
Implemented "Floyd Cycle Detection Method" in Ruby
include method
Abstract method
Ruby print puts p printf output method
initialize method
JAVA (Map)
List method
puts method
Ruby setting 2
ruby Exercise Memo II (variable and method)
Java method
Class method
Ruby problem ⑦
save! method
Ruby learning 2
[Java] method