Class: AddingMultiples::CLI
- Inherits:
-
Object
- Object
- AddingMultiples::CLI
- Defined in:
- lib/adding_multiples/CLI.rb
Instance Method Summary collapse
-
#calculate_least_common_multiple ⇒ Object
calculate least common multiple.
-
#calculate_sum ⇒ Object
add multiples to sum.
-
#call ⇒ Object
the order in which methods are called.
-
#get_another_integer ⇒ Object
ask user for another integer.
-
#get_input ⇒ Object
get input from the user.
-
#get_positive_integer ⇒ Object
get a positive integer from the user.
-
#initialize ⇒ CLI
constructor
greeting and directions for the user.
-
#subtract_common_multiples ⇒ Object
subtract common multiples.
-
#sum_common_multiples ⇒ Object
sum common multiples.
-
#sum_integer_multiples ⇒ Object
loop through multiples and compare to range [1-1000], consider allowing user input for range.
Constructor Details
#initialize ⇒ CLI
greeting and directions for the user
7 8 9 10 11 12 |
# File 'lib/adding_multiples/CLI.rb', line 7 def initialize puts puts "Hello! Thank you for taking the time to try this out." puts "This tool will add the multiples (up to 1,000) of any positive integers you choose." puts "In the prompt below, please enter a positive integer to get started." end |
Instance Method Details
#calculate_least_common_multiple ⇒ Object
calculate least common multiple
95 96 97 98 99 100 |
# File 'lib/adding_multiples/CLI.rb', line 95 def calculate_least_common_multiple integer_0 = @integer[0].to_i integer_1 = @integer[1].to_i @least_common_multiple = integer_0 * integer_1 end |
#calculate_sum ⇒ Object
add multiples to sum
76 77 78 |
# File 'lib/adding_multiples/CLI.rb', line 76 def calculate_sum @sum += @multiple end |
#call ⇒ Object
the order in which methods are called
15 16 17 |
# File 'lib/adding_multiples/CLI.rb', line 15 def call get_input end |
#get_another_integer ⇒ Object
ask user for another integer
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/adding_multiples/CLI.rb', line 41 def get_another_integer input = "" puts "\nPlease enter another positive integer:" @integer << gets.strip if @integer[@i].match(/\d+/) sum_integer_multiples else get_another_integer end end |
#get_input ⇒ Object
get input from the user
20 21 22 23 24 25 |
# File 'lib/adding_multiples/CLI.rb', line 20 def get_input @i = 0 @integer = [] get_positive_integer end |
#get_positive_integer ⇒ Object
get a positive integer from the user
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/adding_multiples/CLI.rb', line 28 def get_positive_integer puts "\nPlease enter a positive integer:" @integer << gets.strip if @integer[@i].match(/\d+/) @i += 1 get_another_integer else get_positive_integer end end |
#subtract_common_multiples ⇒ Object
subtract common multiples
103 104 105 |
# File 'lib/adding_multiples/CLI.rb', line 103 def subtract_common_multiples @sum -= @common_multiples_sum end |
#sum_common_multiples ⇒ Object
sum common multiples
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/adding_multiples/CLI.rb', line 81 def sum_common_multiples @range = [1, 1000] @least_common_multiple = 15 @common_multiples_sum = 0 @multiple = 0 while @multiple < @range[1] @common_multiples_sum += @multiple @multiple += @least_common_multiple end end |
#sum_integer_multiples ⇒ Object
loop through multiples and compare to range [1-1000], consider allowing user input for range
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/adding_multiples/CLI.rb', line 54 def sum_integer_multiples @range = [1, 1000] @sum = 0 @integer.each do |i| integer = i.to_i @multiple = 0 while @multiple < @range[1] calculate_sum @multiple += integer end end # sum common multiples and subtract from total sum_common_multiples subtract_common_multiples puts "The sum of the multiples of the integers #{@integer} that are less than #{@range[1]} is #{@sum}" end |