Undefined Method confusion - Ruby Newbie (I know it rhymes, don't hurt me)

Discussion in 'Ruby' started by Kaldrenon, Jul 17, 2007.

  1. Kaldrenon

    Kaldrenon Guest

    Hi all.

    I recently got into challenging myself with some of the tasks over at
    Project Euler (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/ 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
    Kaldrenon, Jul 17, 2007
    #1
    1. Advertising

  2. Kaldrenon

    Alex LeDonne Guest

    On 7/17/07, Kaldrenon <> wrote:
    > Hi all.
    >
    > I recently got into challenging myself with some of the tasks over at
    > Project Euler (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/ 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


    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
    Alex LeDonne, Jul 17, 2007
    #2
    1. Advertising

  3. Re: Undefined Method confusion - Ruby Newbie (I know it rhym

    Kaldrenon wrote:
    > Hi all.
    >
    > I recently got into challenging myself with some of the tasks over at
    > Project Euler (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/ 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

    --
    Posted via http://www.ruby-forum.com/.
    Daniel Lucraft, Jul 17, 2007
    #3
  4. 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.


    --
    Ist so, weil ist so
    Bleibt so, weil war so
    Sebastian Hungerecker, Jul 17, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DP
    Replies:
    0
    Views:
    1,130
  2. Julia
    Replies:
    1
    Views:
    319
    =?Utf-8?B?S29zdGFkaW4gS29zdG92?=
    Feb 28, 2005
  3. Q&A
    Replies:
    4
    Views:
    517
  4. Glenn
    Replies:
    21
    Views:
    277
    Michael campbell
    May 2, 2004
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    221
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page