From: Christopher Carver Date: 2009-06-16T01:56:43+09:00 Subject: Re: confusion about object method scoping Thanks! That worked. It's actually computing the factorial (sum of all digits leading up to the argument). Can you give me a term for what is going on in your example with the "<<" notation? I'd like to read about it and understand it more. I'm a bit new to Ruby. Thanks a lot for the help! Chris On Mon, Jun 15, 2009 at 12:45 PM, Robert Klemme wrote: > On 15.06.2009 18:31, Christopher Carver wrote: >> >> Hello, >> >> I am defining a class.  That class has several methods.  In one of the >> class methods I'm trying to use a previously defined method.  It's >> throwing an exception.  Here's some code that demonstrates my problem. >>  Am I really unable to have a class use some of it's own methods if >> they've already been defined?  Thanks in advance! >> >> class Calculator >> >>  private >>  def add(x,y) >>   x+y >>  end >> >>  public >>  def Calculator.factorial(x) >>   result = 0 >> >>   (1..x).each { |number| >>     result = add(result,number) >>   } >> >>   result >>  end >> >> end >> >> puts Calculator.factorial() >> > > You are mixing class and instance methods.  You need to make add also a > class method, e.g. by doing something like this > > class Calculator >  class <    def factorial(x) >      result = 0 >      (1..x).each { |number| >       result = add(result, number) >      } >      result >    end > >  private >    def add(x,y) x + y end >  end > end > > Btw, the naming "factorial" seems a bit odd for a sum. > > Kind regards > >        robert > > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ > >