module Refinements

Public Instance Methods

ask_for_choice(choices) { |choice| ... } click to toggle source
# File lib/podjumper/refinements.rb, line 19
def ask_for_choice(choices)
  padding_max = choices.count.to_s.length
  puts choices.each_with_index.map { |choice, index|
    index += 1
    choice = yield choice if block_given?
    padding = ' ' * (padding_max - index.to_s.length)
    "#{index}. #{padding}#{choice}"
  }

  index = ask_for_index(choices.count)
  return if index.nil?

  choices[index]
end
ask_for_index(count) click to toggle source
# File lib/podjumper/refinements.rb, line 34
def ask_for_index(count)
  answer = ask("Choose from 1..#{count}, or (q)uit...") do |q|
    q.case = :downcase
    q.validate = /\A\d+|q\Z/
  end
  exit 0 if answer == 'q' # TODO: Safe exit
  index = Integer(answer)
  if index < 1
    puts 'The first choice is 1'
    ask_for_index(count)
  elsif index > count
    puts "The last choice is #{count}"
    ask_for_index(count)
  else
    index - 1
  end
end
drop_prefix(prefix) click to toggle source
# File lib/podjumper/refinements.rb, line 9
def drop_prefix(prefix)
  if start_with?(prefix)
    self[prefix.length...self.length]
  else
    self
  end
end
select_regex(regex) click to toggle source
# File lib/podjumper/refinements.rb, line 3
def select_regex(regex)
  self.select { |item| item =~ regex }
end