[Ruby] How to batch convert strings in an array to numbers

Introduction

When I tried to convert the numbers in the array, I stumbled and checked how to write I will make a note for myself as a memorandum.

problem

When trying to convert the number of strings in an array I got an error.

array.rb


Traceback (most recent call last):
array.rb:3:in `<main>': undefined method `to_i' for ["1", "2", "3", "4", "5"]:Array (NoMethodError)
Did you mean?  to_s
               to_a
               to_h

This is a summary of how to write. ↓ ↓ ↓

while method

array.rb


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

i = 0
while i <= 4
  num[i] = num[i].to_i
  i += 1
end

p num
#=>[1, 2, 3, 4, 5]

for statement

array.rb


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

int = []
for i in num
  int.push(i.to_i)
end

p int
#=>[1, 2, 3, 4, 5]

map method

array.rb


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

int = num.map{ |i| i.to_i }

p int
#=>[1, 2, 3, 4, 5]

array.rb


num = [ "1", "2", "3", "4", "5" ]
int = num.map(&:to_i)

p int
#=>[1, 2, 3, 4, 5]

Summary

I couldn't convert it very well, so I took this opportunity to investigate. This seems to be a little easier to understand.

Recommended Posts

[Ruby] How to batch convert strings in an array to numbers
Convert an array of strings to numbers
[Ruby] How to count even or odd numbers in an array
How to retrieve the hash value in an array in Ruby
[Ruby] I want to put an array in a variable. I want to convert to an array
How to convert an array of Strings to an array of objects with the Stream API
How to convert a file to a byte array in Java
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
How to iterate infinitely in Ruby
How to install Bootstrap in Ruby
How to concatenate strings in java
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 get date data in Ruby
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
[Ruby] How to convert CSV file to Yaml (Yml)
How to solve an Expression Problem in Java
[Ruby on Rails] How to install Bootstrap in Rails
How to build the simplest blockchain in Ruby
How to build an executable jar in Maven
Ruby How to convert between uppercase and lowercase
How to implement Pagination in GraphQL (for ruby)
[Swift] How to get the number of elements in an array (super basic)
If the parameter is an array, how to include it in Stopara's params.permit
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
How to start a subscript from an arbitrary number in Ruby iterative processing
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
How to concatenate strings
How to make an image partially transparent in Processing
How to install Ruby on an EC2 instance on AWS
[Ruby] How to use standard output in conditional branching
How to use an array for a TreeMap key
[Ruby on Rails] How to write enum in Japanese
How to write to apply gem Pagy (pagination) to an array
Calculate the difference between numbers in a Ruby array
[Ruby On Rails] How to reset DB in Heroku
How to launch another command in a Ruby program
How to output standard from an array with forEach
How to use CommandLineRunner in Spring Batch of Spring Boot
How to convert A to a and a to A using AND and OR in Java
How to handle TSV files and CSV files in Ruby
How to query Array in jsonb with Rails + postgres
How to write an external reference key in FactoryBot
How to use In-Memory Job repository in Spring Batch
[Rails] How to display an image in the view
How to resolve SSL_connect error in PayPal Ruby SDK
[Ruby] How to extract a specific value from an array under multiple conditions [select / each]
How to create an application
[Ruby] Count an even number in an array using the even? Method
[Ruby] Searching for elements in an array using binary search
How to pass an object to Mapper in MyBatis without arguments
How to divide a two-dimensional array into four with ruby
Convert to Ruby Leet string
How to specify an array in the return value / argument of the method in the CORBA IDL file
[For Ruby beginners] Explain how to freely delete array elements!
[Ruby] How to comment out
Multiplication in a Ruby array
Learning Ruby with AtCoder 13 How to make a two-dimensional array
What happened in "Java 8 to Java 11" and how to build an environment
Ruby: How to use cookies