0

I'm trying to do a program that asks the user for three numbers one at a time, store them in array, then print the list of the numbers and the total.

Please explain.

Here is what I have so far:

numbers = Array.new
numbers = []
puts "Enter first number: "
first = gets.to_i
puts "Enter second number: "
second = gets.to_i
puts "Enter third number: "
third = gets.to_i
def sum(numbers)
  return 0 if numbers.length < 1
  result = 0
  numbers.each { |num| result += num }
  result
end
6
  • 2
    Perhaps you need to add them to the array as well? Commented Sep 6, 2015 at 6:58
  • why are you making two numbers arrays, which are identical? Commented Sep 6, 2015 at 7:00
  • 1
    I'm new on ruby so I'm not sure what I'm doing Commented Sep 6, 2015 at 7:08
  • For the sum, just use numbers.sum If there might be nils, use numbers.compact.sum Commented Sep 6, 2015 at 9:58
  • @DavidAldridge #sum method is available only via ActiveSupport. It isn't present in Plain Ol' Ruby. Commented Sep 6, 2015 at 17:37

4 Answers 4

2

You can also use Array's reduce method.

http://ruby-doc.org/core-2.1.0/Enumerable.html#method-i-reduce

#!/usr/bin/ruby
numbers = Array.new
# numbers = [] #this is same as above
puts "Enter first number: "
first = gets.to_i
numbers<<first
puts "Enter second number: "
second = gets.to_i
numbers<<second
puts "Enter third number: "
third = gets.to_i
numbers<<third
puts numbers.reduce {|sum, n| sum + n } #here
Sign up to request clarification or add additional context in comments.

3 Comments

There's no need to define temporary variables first, second and third; just write numbers << gets.to_i three times. Also, you can calculate the sum as numbers.reduce(:+).
@CarySwoveland you can even use 3.times.map { gets.to_i }.reduce(:+) as times returns Enumerator if there is no block given.
@Hauleth, almost. See Wand's answer.
2

Here is one more way of doing this:

sum = 3.times.collect{ |i| puts "Enter number #{i + 1}:"; gets.chomp.to_i }.inject(:+)
puts sum

Could also be written like below:

read_num = lambda{|i| puts "Enter number #{i}"; gets.chomp.to_i}
sum = 3.times.map(&read_num).reduce(:+)
puts sum

2 Comments

..or %w|first second third|.collect { |w| puts "Enter #{w} number";.... I suggest you reformat to avoid the need for horizontal scrolling.
@CarySwoveland - Yeah that's one possibility - thought it may be good idea to have flexibility of doing N.times.
0

You haven't pushed any of your inputs into the array, you can either use the push function or the << to add elements to your array

  #!/usr/bin/ruby
numbers = Array.new
numbers = []
puts "Enter first number: "
first = gets.to_i
numbers<<first
puts "Enter second number: "
second = gets.to_i
numbers<<second
puts "Enter third number: "
third = gets.to_i
numbers<<first
def sum(someArray)
  return 0 if someArray.length < 1
  result = 0
  someArray.each { |num| result += num }
  result
end

hope that helps

Comments

0

There are two problems here:

  1. You aren't pushing the numbers you read into the array
  2. You defined the sum function properly, but aren't calling it anywhere


#!/usr/bin/ruby
numbers = Array.new # note the second, redundant, initialization on numbers was removed
puts "Enter first number: "
(numbers ||= []) << gets.to_i # Pushing read value into the array (issue 1)
puts "Enter second number: "
(numbers ||= []) << gets.to_i # Here too
puts "Enter third number: "
(numbers ||= []) << gets.to_i # And here too
def sum(numbers)
  return 0 if numbers.length < 1
  result = 0
  numbers.each { |num| result += num }
  result
end

puts sum(numbers) # Calling sum (issue 2)

2 Comments

Considering that you've already initialized numbers to be an empty array, why (numbers ||= []) << gets.to_i rather than simply numbers << gets.to_i? Also, one normally uses Hash#reduce to calculate simple sums.
I don't get need for ||= either.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.