Adam Prescott wrote in post #1014254: > On Mon, Aug 1, 2011 at 8:33 PM, Marc Chanliau > <marc.chanliau / gmail.com>wrote: > >> my_fact = Factorial.new >> puts "enter a positive integer: " >> my_fact = gets.to_i >> puts "factorial: #{fact(n)}" >> > > You're relying on `n` being defined here, but it's only defined inside > Factorial#fact, which is why you get "undefined local variable or method > `n'" as an error message. You probably wanted to do this: > > puts "factorial: #{fact(my_fact)}" > > As a perhaps neater way of writing the #fact method, I'd do it like this > (although it's moot, really): > > def fact(n) > return 1 if n == 0 > > n * fact(n-1) > end Thanks, I took your suggestions into account. Now I have this class: class Factorial def fact(n) return 1 if n == 0 n * fact(n-1) end end my_fact = Factorial.new print "enter a positive integer: " my_fact = gets.to_i puts "factorial: #{fact(my_fact)}" But I still get an error: marcc$ ruby factorial7.rb enter a positive integer: 4 factorial7.rb:11:in `<main>': undefined method `fact' for main:Object (NoMethodError) thanks! -- Posted via http://www.ruby-forum.com/.