lib/nmax.rb in nmax_cli-0.0.0 vs lib/nmax.rb in nmax_cli-0.0.2

- old
+ new

@@ -1,51 +1,64 @@ module Nmax class << self def run - parameter_check + warning return if wrong_args? + return if not_presence_stdin? + @size = ARGV[0].to_i @numbers = [] + strings_processing - $stdin.each do |line| - add_nums_to_result nums(line) - end - puts '' if @numbers.empty? - puts @numbers + puts numbers end private - def parameter_check + attr_reader :size + attr_accessor :numbers + + def warning + warn 'Not reading from stdin' if not_presence_stdin? warn 'Nmax must receive one argument (number). Example: nmax 100' if wrong_args? end + def not_presence_stdin? + $stdin.tty? + end + def wrong_args? - ARGV.size != 1 || first_arg_not_number? + ARGV.size != 1 || argument_not_number? end - def first_arg_not_number? + def argument_not_number? !ARGV[0].match?(/^\d+$/) end - def nums(line) + def strings_processing + $stdin.each { |line| add_nums search_numbers(line) } + end + + def search_numbers(line) line.scan(/\d+/).map(&:to_i) end - def add_nums_to_result(nums) + def add_nums(nums) return if nums.empty? - nums.each { |n| add(n) } + nums.each { |n| add_number(n) } end - def add(num) - insert_at = @numbers.bsearch_index { |n| num >= n } - @numbers.insert(insert_at, num) if insert_at - @numbers << num unless insert_at - shorten + def add_number(num) + return if numbers.include?(num) + + insert_at = numbers.bsearch_index { |n| num > n } + numbers.insert(insert_at, num) if insert_at + numbers << num unless insert_at + fix_overflow end - def shorten - @numbers.pop if @numbers.size > @size + def fix_overflow + numbers.pop if numbers.size > size end end end