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 => [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 ={ |n| n.to_i }

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


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

intArray =

# 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"]!{ |n| n.to_i }

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

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

Reference site

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 count even or odd numbers in an array
[Ruby] How to generate a random alphabet string
Multiplication in a Ruby array
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)
[Java] How to convert one element of a String type array to an Int type
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
Convert a Java byte array to a string in hexadecimal notation
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 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
How to create a query using variables in GraphQL [Using Ruby on Rails]
[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
Code to escape a JSON string 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
How to solve an Expression Problem in Java
How to build the simplest blockchain in Ruby
How to build an executable jar in Maven
How to write Java String # getBytes in Kotlin?
How to implement Pagination in GraphQL (for ruby)
How to automatically generate a constructor in Eclipse
[Spring Boot] How to get properties dynamically from a string contained in a URL
How to ZIP a JAVA CSV file and manage it in a Byte array
How to get an arbitrary digit from a number of 2 or more digits! !!