Efficient/ straight forward way to get integer array from stdin in ruby

arrays, ruby

Solution

The default argument for `split` is whitespace. The idiom to do something with every element in an array and get an array as result is `map`.

puts "Enter array elements with a space"
array_as_string = gets
array = array_as_string.split.map(&:to_i)

Problem

My approach - get array elements as string with delimiter such as space or comma - split the string - Convert each element into number and push into the array The code looks like this: ``` puts 'Enter array elements with a space' array_as_string = gets if array_as_string.length > 0 input_array = [] array_as_string.split(' ').each { |x| input_array.push(x.to_i) } else puts 'Invalid input' end ``` Is there a better/ efficient alternative or a straight forward way of doing this?

Original source