Convert an array of strings to numbers

I want to convert an array of strings to numbers!

test.rb


> "1,2,3".split(",")
=> ["1", "2", "3"]

In a certain programming test, I receive a value with standard input and convert it to an array, but when I use split I was thinking that I wanted to return it to a numerical value because it would be a character string.

I think it would be useful to look it up for the time being.

__map method __

test.rb


> "1,2,3".split(",").map(&:to_i)
=> [1, 2, 3]

https://docs.ruby-lang.org/ja/latest/method/Enumerable/i/map.html https://teleporter.hateblo.jp/entry/ruby-str-to-num

"The map method iterates over the number of elements and creates and returns an array of the block's return values."

If you think about it roughly, it feels like recreating the array under the specified conditions.

Recommended Posts

Convert an array of strings to numbers
[Ruby] How to batch convert strings in an array to numbers
How to convert an array of Strings to an array of objects with the Stream API
Cast an array of Strings to a List of Integers in Java
[Java] How to convert one element of a String type array to an Int type
Convert the array of errors.full_messages to characters and output
[Java] Convert ArrayList to array
Convert an array that may be null to a stream
Convert alphabet to 26 base + array length
[Java] Convert array to ArrayList * Caution
Convert Swift 2D array to C 2D array
[Ruby] How to count even or odd numbers in an array
I want to convert an array to Active Record Relation with Rails
[Swift5] How to get an array and the complement of arrays
[Ruby] I want to put an array in a variable. I want to convert to an array
The story of toString () starting with passing an array to System.out.println
12 of Array
Convert numbers to Roman numerals in Ruby
Compare the elements of an array (Java)
Sort an array of Ruby homebrew classes
Get the type of an array element to determine if it is an array
[Swift] How to get the number of elements in an array (super basic)
Easy way to create an implementation of java.util.stream.Stream
How to use an array for HashMap keys
Convert a string to a character-by-character array with swift
Ruby's "1101" .to_i (2) does not convert decimal numbers to decimal numbers.
Rails6 I want to make an array of values with a check box
Corresponds to 15 strings
[Algorithm] N numbers with an interval of X
How to request by passing an array to the query with HTTP Client of Ruby
How to use an array for a TreeMap key
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
How to write to apply gem Pagy (pagination) to an array
Cannot convert value of type'DocumentSnapshot' to expected argument type'QueryDocumentSnapshot'
Mandels to create an image of the Mandelbrot set
How to output standard from an array with forEach
How to convert a file to a byte array in Java
Null value is entered when assigning to an array
How to specify an array in the return value / argument of the method in the CORBA IDL file
Conditional branching of numbers
How to concatenate strings
[Java] I want to convert a byte array to a hexadecimal number
Convert a Java byte array to a string in hexadecimal notation
How to change a string in an array to a number in Ruby
How to retrieve the hash value in an array in Ruby
Initialization with an empty string to an instance of Java String type
How to add elements without specifying the length of the array
What is an immutable object? [Explanation of how to make]
Impressions of an engineer apprentice going to JJUG CCC 2018 Spring
For Java beginners: List, Map, Iterator / Array ... How to convert?
Convert 2D array to csv format with Java 8 Stream API
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
Generate Stream from an array of primitive types in Java
I wrote a code to convert numbers to romaji in TDD
How to write when you want to handle "array of C language strings" like argv [] in Ruby-FFI