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
[Practice] Map method
Ruby build method
abbreviation for ruby method
Ruby Thread # [] = method notes
definition of ruby method
About the map method
[Ruby] Method definition summary
Integer check method with ruby
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 basics] split method and to_s method
[Ruby on Rails] Convenient helper method
How to use Ruby inject method
[Ruby] Array
to_i method
Ruby learning 5
Ruby basics
Ruby Review 2
getRequestDispatcher () method
Ruby addition
merge method
Ruby on Rails installation method [Mac edition]
[Ruby] Simplify each using map and inject
[Ruby] From the basics to the inject method
[Rails] How to use the map method
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
Java method
Class method
Ruby problem ⑦
save! method
getParameter method
Ruby learning 2
[Java] method
Ruby learning 6
Ruby settings 1
private method
Extract characters from Ruby strings slice method
rails method
Ruby learning 1
Ruby Review 1
[Java] method
[Ruby on Rails] NoMethodError undefined method `devise_for'error resolution
[Ruby] How to use gsub method and sub method
[Ruby] I want to do a method jump!
Ruby on Rails address automatic input implementation method