vcupsych vcupsych - 1 year ago 89
Ruby Question

Ruby trouble with arrays and sorting

looking for the right methods to split, arrange using enumerable
user_array = user_input.chars.to_a {|item| item.to_i}

arrange_array = user_array.push(num).join(",")
#need to give number before, and number after input

puts arrange_array

Answer Source
puts "Please enter some numbers:"
user_input = gets.chomp

puts "Please enter another number:"
num = gets.to_i

          .map(&:to_i)                # convert them to integers
          .partition { |n| n < num }  # split / partition by cond
          .map(&:sort)                # sort results
#⇒ [[0, 1, 2], [4, 5]]

The core of this solution is Enumerable#partition method, that splits the array by the condition provided.