How to change a string in an array to a number in Ruby

array = ["1", "2", "3", "4", "5"]

I will write a method to change the character string of the array to a number when there is a numerical value in the character string in the array like.

for statement method

array = ["1", "2", "3", "4", "5"]

intArray = []
for n in array
  intArray.push(n.to_i)
end

# intArray => [1, 2, 3, 4, 5]

Create an array ʻintArray = [] that is different from ʻarray. The process of getting the value of ʻarray with n, changing the character string to a numerical value with to_i, and assigning the value to ʻintArray with push is repeated.

map method method

array = ["1", "2", "3", "4", "5"]

intArray = array.map{ |n| n.to_i }

# intArray => [1, 2, 3, 4, 5]

Or

array = ["1", "2", "3", "4", "5"]

intArray = array.map(&:to_i)

# intArray => [1, 2, 3, 4, 5]

The map method iteratively processes the array ʻarray, assigns the value to the variable n, and executes the process n.to_i or &: to_i as the process you want to execute. Substitute the executed process into ʻintArray. In this case, it is not necessary to define in advance like ʻintArray = [] `.

You can also use map!, in this case

array = ["1", "2", "3", "4", "5"]

array.map!{ |n| n.to_i }

# array => [1, 2, 3, 4, 5]

You can rewrite the array of ʻarray` like this.

Reference site

https://techacademy.jp/magazine/19868 https://uxmilk.jp/21695

Recommended Posts

How to change a string in an array to a number in Ruby
How to start a subscript from an arbitrary number in Ruby iterative processing
How to retrieve the hash value in an array in Ruby
[Ruby] How to batch convert strings in an array to numbers
[Ruby] How to count even or odd numbers in an array
[Ruby] I want to put an array in a variable. I want to convert to an array
[Ruby] How to generate a random alphabet string
Multiplication in a Ruby array
Count the number of occurrences of a string in Ruby
How to use an array for a TreeMap key
How to launch another command in a Ruby program
How to convert a file to a byte array in Java
[Swift] How to get the number of elements in an array (super basic)
How to check if an instance variable is defined in a Ruby class
How to make a judgment method to search for an arbitrary character in an array
[Ruby] Count an even number in an array using the even? Method
How to iterate infinitely in Ruby
How to divide a two-dimensional array into four with ruby
How to install Bootstrap in Ruby
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to store a string from ArrayList to String in Java (Personal)
How to display a graph in Ruby on Rails (LazyHighChart)
How to add the same Indexes in a nested array
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
How to make a Java array
[Ruby] How to extract a specific value from an array under multiple conditions [select / each]
How to output the value when there is an array in the array
Cast an array of Strings to a List of Integers in Java
How to change app name in rails
How to get date data in Ruby
How to insert a video in Rails
How to add a new hash / array
Notation to put a variable in a string
How to publish a library in jCenter
About the method to convert a character string to an integer / decimal number (cast data) in Java
[Java] How to search for a value in an array (or list) with the contains method
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
[Personal memo] How to interact with a random number generator in Java
[Java] How to turn a two-dimensional array with an extended for statement
How to get the current date as a string in yyyyMMdd format
How to change the maximum and maximum number of POST data in Spark
How to change the value of a variable at a breakpoint in intelliJ
I want to ForEach an array with a Lambda expression in Java
[Java] [For beginners] How to insert elements directly in a 2D array
How to insert processing with any number of elements in iterative processing in Ruby
Convert to a tag to URL string in Rails
[Android] How to convert a character string to resourceId
How to display a web page in Java
Convert a string to a character-by-character array with swift
How to create pagination for a "kaminari" array
How to run a djUnit task in Ant
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to implement a like feature in Rails
How to easily create a pull-down in Rails
http: // localhost: How to change the port number
How to solve an Expression Problem in Java
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
How to build the simplest blockchain in Ruby
How to build an executable jar in Maven