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