Method: Menu#parse_answer

Defined in:
lib/menu.rb

#parse_answer(array, input) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/menu.rb', line 27

def parse_answer(array, input)
  return array if input.strip == '*'
  result = []
  input.split(/\s*,\s*/).each do |e|
    if e[/(\d+)-(\d+)/]
      min, max = $1, $2
      slice_min = min.to_i - 1
      result.push(*array.slice(slice_min, max.to_i - min.to_i + 1))
    elsif e[/^(\d+)$/]
      index = $1.to_i - 1
      result.push(array[index]) if array[index]
    else
      abort("`#{e}' is an invalid choice.")
    end
  end
  result
end