# Undefined Method confusion - Ruby Newbie (I know it rhymes,

**URL:** <https://www.ruby-forum.com/t/undefined-method-confusion-ruby-newbie-i-know-it-rhymes/109681>\
**Category:** Ruby\
**Created:** [July 17, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/undefined-method-confusion-ruby-newbie-i-know-it-rhymes/109681 "2007-07-17T21:27:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaldrenon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@Kaldrenon](https://www.ruby-forum.com/u/Kaldrenon)\
**Post date:** [July 17, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/undefined-method-confusion-ruby-newbie-i-know-it-rhymes/109681/1 "2007-07-17T21:27:21Z")

</div>

Hi all.

I recently got into challenging myself with some of the tasks over at  
Project Euler ([www.projecteuler.net](http://www.projecteuler.net)), and while working on the  
solution for one of them (# 14) I came across a Ruby problem. Now, I’m  
pretty sure that this is just a dumbness on my part, but I don’t know  
how to fix this problem because I see no differences between my code  
and the examples at [http://www.whytheluckystiff.net/ruby/pickaxe/](http://www.whytheluckystiff.net/ruby/pickaxe/) for  
declaring and calling methods.

Here’s the code (it’s tiny so I’ll copy the whole thing):

class Test  
def series(n,x)  
if (n == 1)  
return x  
elsif (n % 2 == 0 )  
return sequence(n/2, x + 1)  
else  
return sequence(3\*n + 1, x + 1)  
end  
end

max\_num = 1  
max\_len = 1  
for i in 2…1\_000\_000 do  
my\_len = series(i,1)  
if (my\_len \> max\_len)  
max\_len = my\_len  
max\_num = i  
end  
end  
print max\_num , ": chain of length " , max\_len  
end

The error I’m getting is this:  
undefined method `series' for Test:Class (NoMethodError) from euler14.rb:14:in `each’

I see no clear reason why I’m getting this error. Can you un-dumb me?

Thanks in advance,  
Andrew

---

<div class="post-metadata">

**Author:** ![Kaldrenon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@Kaldrenon](https://www.ruby-forum.com/u/Kaldrenon)\
**Post date:** [July 17, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/undefined-method-confusion-ruby-newbie-i-know-it-rhymes/109681/2 "2007-07-17T21:41:21Z")

</div>

Kaldrenon wrote:

> Hi all.
> 
> I recently got into challenging myself with some of the tasks over at  
> Project Euler ([www.projecteuler.net](http://www.projecteuler.net)), and while working on the  
> solution for one of them (# 14) I came across a Ruby problem. Now, I’m  
> pretty sure that this is just a dumbness on my part, but I don’t know  
> how to fix this problem because I see no differences between my code  
> and the examples at [http://www.whytheluckystiff.net/ruby/pickaxe/](http://www.whytheluckystiff.net/ruby/pickaxe/) for  
> declaring and calling methods.
> 
> Here’s the code (it’s tiny so I’ll copy the whole thing):
> 
> class Test  
> def series(n,x)  
> if (n == 1)  
> return x  
> elsif (n % 2 == 0 )  
> return sequence(n/2, x + 1)  
> else  
> return sequence(3\*n + 1, x + 1)  
> end  
> end
> 
> max\_num = 1  
> max\_len = 1  
> for i in 2…1\_000\_000 do  
> my\_len = series(i,1)  
> if (my\_len \> max\_len)  
> max\_len = my\_len  
> max\_num = i  
> end  
> end  
> print max\_num , ": chain of length " , max\_len  
> end
> 
> The error I’m getting is this:  
> undefined method `series' for Test:Class (NoMethodError) from euler14.rb:14:in `each’
> 
> I see no clear reason why I’m getting this error. Can you un-dumb me?
> 
> Thanks in advance,  
> Andrew

I enjoy Project Euler too. The error you are getting here is because the  
method ‘series’ is an instance method, i.e.:

> a = Test.new  
> a.series #=\> calls method

> Test.series #=\> gives missing method error

The ‘for’ loop is executing in the context of the class definition, so  
it is looking for a class method. The error message indicates that it is  
looking for the method on Test:Class.

You can either turn ‘series’ into a class method:

class Test  
def self.series  
…

or you can move the for loop out of the class and create a test object:

class Test  
def series  
…  
end  
end

t = Test.new  
max\_num = 1  
max\_len = 1  
for i in 2…1\_000\_000 do  
my\_len = t.series(i, 1)  
…

Incidentally, in the code you gave there’s no method called ‘sequence’.  
Is it possible you meant ‘series’ there too?

best,  
Dan

---

<div class="post-metadata">

**Author:** ![Kaldrenon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@Kaldrenon](https://www.ruby-forum.com/u/Kaldrenon)\
**Post date:** [July 17, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/undefined-method-confusion-ruby-newbie-i-know-it-rhymes/109681/3 "2007-07-17T21:46:22Z")

</div>

Kaldrenon wrote:

> The error I’m getting is this:  
> undefined method `series' for Test:Class (NoMethodError) from euler14.rb:14:in`each’
> 
> I see no clear reason why I’m getting this error. Can you un-dumb me?

You define series as an instance method, but you try to invoke it as a  
class  
method. If you do

class Bla  
def blubb() end  
end  
x=Bla.new

there will be a x.blubb but no Bla.blubb.  
If you want a Bla.blubb you have to define it with “def self.blubb”.  
Also given the fact that you never create an instance of your class, you  
don’t  
actually need to define a class.

---

<div class="post-metadata">

**Author:** ![Kaldrenon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@Kaldrenon](https://www.ruby-forum.com/u/Kaldrenon)\
**Post date:** [July 17, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/undefined-method-confusion-ruby-newbie-i-know-it-rhymes/109681/4 "2007-07-17T21:42:22Z")

</div>

On 7/17/07, Kaldrenon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here’s the code (it’s tiny so I’ll copy the whole thing):  
> end  
> print max\_num , ": chain of length " , max\_len  
> end
> 
> The error I’m getting is this:  
> undefined method `series' for Test:Class (NoMethodError) from euler14.rb:14:in `each’
> 
> I see no clear reason why I’m getting this error. Can you un-dumb me?
> 
> Thanks in advance,  
> Andrew

Andrew,

You’ve created an instance method, but you’re calling a class method.

Try defining a class method with:

def self.series(n,x)

instead.

Also, did you notice that you’re returning sequence(), rather than  
series()? 🙂

-Alex
