Reputation:
I have a class named Fraction that calculates the harmonic sum. Basically, I want to be able to input a value, and then the program be able to calculate the harmonic sum up to that number. For example, if a user inputs 3 then I want the output to be "harmonic 1: 1/1, harmonic 2: 3/2, harmonic 3: 11/6". This is what I have for the method so far.
print "Enter a value for your harmonic sum: "
x = gets.chomp.to_i
def harmonic_sum(x)
(1..x).inject(Fraction.new(0)) do |a, i|
harmonic = Fraction.new(1, i)
puts "h#{i}: #{harmonic + a}"
a + harmonic
end
end
puts harmonic_sum
I keep getting an error that my argument is wrong. Any help would be appreciated!
Upvotes: 0
Views: 53
Reputation: 26768
Right here you get an input:
x = gets.chomp.to_i
Then you define the method which requires one argument:
def harmonic_sum(x)
but here you call it with no arguments:
puts harmonic_sum
Instead, use puts harmonic_sum(x)
Upvotes: 1