# Confusion about object method scoping

**URL:** <https://www.ruby-forum.com/t/confusion-about-object-method-scoping/170299>\
**Category:** Ruby\
**Created:** [June 15, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/confusion-about-object-method-scoping/170299 "2009-06-15T18:31:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Carver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f19dbf/32.png) [@Christopher\_Carver](https://www.ruby-forum.com/u/Christopher_Carver)\
**Post date:** [June 15, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/confusion-about-object-method-scoping/170299/1 "2009-06-15T18:31:55Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![Christopher\_Carver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f19dbf/32.png) [@Christopher\_Carver](https://www.ruby-forum.com/u/Christopher_Carver)\
**Post date:** [June 15, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/confusion-about-object-method-scoping/170299/2 "2009-06-15T18:41:20Z")

</div>

On Mon, Jun 15, 2009 at 11:31 AM, Christopher Carver  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> private  
> }
> 
> result  
> end
> 
> end

Is it? ArgumentError: wrong number of arguments (0 for 1)  
You’re calling your method without a required argument.

> puts Calculator.factorial()

Which you defined when you wrote:

def Calculator.factorial(x)

You could send a default value if you don’t want to specify one  
everytime

def Calculator.factorial(x = 0)

---

<div class="post-metadata">

**Author:** ![Christopher\_Carver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f19dbf/32.png) [@Christopher\_Carver](https://www.ruby-forum.com/u/Christopher_Carver)\
**Post date:** [June 15, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/confusion-about-object-method-scoping/170299/3 "2009-06-15T18:44:18Z")

</div>

I just realized I made a couple of typos. I forgot to pass an  
argument to Calculator.factorial and in factorial I used the =  
operator instead of +=. But I think you see what I am trying to do.  
How can I make a class method callable by another method in that  
class?

---

<div class="post-metadata">

**Author:** ![Christopher\_Carver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f19dbf/32.png) [@Christopher\_Carver](https://www.ruby-forum.com/u/Christopher_Carver)\
**Post date:** [June 15, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/confusion-about-object-method-scoping/170299/4 "2009-06-15T18:46:04Z")

</div>

On 15.06.2009 18:31, Christopher Carver wrote:

> private  
> }
> 
> 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 \<\<self  
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

---

<div class="post-metadata">

**Author:** ![Christopher\_Carver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f19dbf/32.png) [@Christopher\_Carver](https://www.ruby-forum.com/u/Christopher_Carver)\
**Post date:** [June 15, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/confusion-about-object-method-scoping/170299/5 "2009-06-15T18:57:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christopher\_Carver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f19dbf/32.png) [@Christopher\_Carver](https://www.ruby-forum.com/u/Christopher_Carver)\
**Post date:** [June 15, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/confusion-about-object-method-scoping/170299/6 "2009-06-15T19:06:06Z")

</div>

On 15.06.2009 18:56, Christopher Carver wrote:

> 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.

It’s the singleton class of the class instance. I know this sounds a  
bit weird but every object in Ruby has this singleton class and since  
classes are objects, too, they do so as well. Methods defined in the  
singleton class methods of that instance only. So basically these two  
are equivalent:

o = … # any object  
def o.meth  
end

class \<\<o  
def meth  
end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Christopher\_Carver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f19dbf/32.png) [@Christopher\_Carver](https://www.ruby-forum.com/u/Christopher_Carver)\
**Post date:** [June 15, 2009, 8:28pm UTC](https://www.ruby-forum.com/t/confusion-about-object-method-scoping/170299/7 "2009-06-15T20:28:47Z")

</div>

On Mon, Jun 15, 2009 at 12:56 PM, Christopher Carver[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks! That worked. It’s actually computing the factorial (sum of  
> all digits leading up to the argument).

Well not exactly: [Factorial - Wikipedia](http://en.wikipedia.org/wiki/Factorial)

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Christopher\_Carver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f19dbf/32.png) [@Christopher\_Carver](https://www.ruby-forum.com/u/Christopher_Carver)\
**Post date:** [June 15, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/confusion-about-object-method-scoping/170299/8 "2009-06-15T18:51:24Z")

</div>

On Mon, Jun 15, 2009 at 12:31 PM, Christopher Carver[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> private  
> def add(x,y)  
> x+y  
> end

This defines a method add which any instances of the class Calculator  
would have, if you had any, which you don’t in this code.

> end
> 
> end

And this defines a method on the object which is the class called  
Calculator. Since Calculator isn’t an instance of Calculator it  
doesn’t have an add method.

Now how to fix it. One way is

class Calculator  
class \< self  
def factorial(x)  
result = 0  
(1…x).each { |number|  
result = add(result,number)  
}  
result  
end

```
   def add(x, y)
      x + y
   end
end

```

end

puts Calculator.factorial(5)

Another way is to actually use an instance of Calculator:

class Calculator  
def factorial(x)  
result = 0  
(1…x).each { |number|  
result = add(result,number)  
}  
result  
end

```
   def add(x, y)
      x + y
   end

```

end

puts Calculator.new.factorial(5)

BTW. I’ve not mentioned until now that your factorial is actually  
computing the sum instead of the factorial, because I think that’s a  
separate issue.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
