Need some help on making the def fact(n)
command to work off user input. My script works great when I input something like "Ruby factorial.rb 5". However, I need this to run with prompts. The script should run something like:
puts 'Please input a non-negative integer:'
n = gets.chomp.to_i
puts "The factorial of #{n} is #{factorial}
Here's what my code looks like now. Like I said however, it does work. It's when I add n = gets.chomp.to_i
before the def fact(n)
command that I automatically receive a value of 1. It doesn't matter what number the user puts in, it always returns 1.
#!/user/bin/ruby
def fact(n)
if n==0
1
else
n * fact(n-1)
end
end
puts fact(ARGV[0].to_i)
How do I make this work with the user input? I think I'm just having a syntax issue, but don't know how to make it work.