Class: PrintSquare::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/print_square/command_runner.rb

Class Method Summary collapse

Class Method Details

.is_square?(number) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/print_square/command_runner.rb', line 48

def is_square?(number)
  return true if number == 1
  position = 2
  spread = 1
  until spread == 0
    current_square = position*position
    return true if current_square == number
    if number < current_square
      spread >>= 1
      position -= spread
    else
      spread <<= 1
      position += spread
    end
  end
  false
end


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/print_square/command_runner.rb', line 9

def print_square(number)
  size = Math.sqrt(number).to_i
  n = number
  x = PrintSquare::Vector.new size.even? ? 1 : -1, size, size.even? ? 1 : 0
  y = PrintSquare::Vector.new 0, size
  print = PrintSquare::Printer.new size

  x.turn = proc do
    y.offset += 1 if x.direction == 1
    y.direction = x.direction
    x.direction = 0
  end

  y.turn = proc do
    if y.direction == -1
      x.size -= 1
      y.size -= 1
      x.offset += 1
    end
    x.direction = y.direction * -1
    y.direction = 0
  end

  until n == 0
    print.set x, y, n
    y.direction == 0 ? x.next : y.next
    n -= 1
  end

  print.out
end

.run(args) ⇒ Object



4
5
6
7
# File 'lib/print_square/command_runner.rb', line 4

def run(args)
  validate_args(args)
  print_square(args[0].to_i)
end

.usage(error_type) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/print_square/command_runner.rb', line 66

def usage(error_type)
  error = case error_type
  when :no_args then 'Missing argument'
  when :invalid_arg then 'Argument must be a number'
  when :too_many_args then 'Too many arguments'
  when :not_square then "Argument is not a square number"
  end
  puts <<-USAGE
    #{error}

    print_square [square_number]
  USAGE

  exit(-1)
end

.validate_args(args) ⇒ Object



41
42
43
44
45
46
# File 'lib/print_square/command_runner.rb', line 41

def validate_args(args)
  usage(:no_args) if args.count == 0
  usage(:too_many_args) if args.count > 1
  usage(:invalid_arg) unless (Integer(args[0]) rescue false)
  usage(:not_square) unless is_square?(args[0].to_i)
end