Method not inherit?

Discussion in 'Ruby' started by Manuel Manuelson, Sep 10, 2010.

  1. Hi,

    I'm a newbie in Ruby and created this module:

    require 'couchrest_model'
    module RdfModelModule

    class RdfModel < CouchRest::Model::Base
    def rdf_schema
    p 'hello'
    end
    end

    end


    When I create a new class based no this class like this:

    require 'rdfmodelmodule'
    class Mine < RdfModelModule::RdfModel
    rdf_schema
    end

    I'm getting this error:
    NoMethodError: undefined method `rdf_schema' for Mine:Class


    This is a bit confusing! Why is rdf_schema not inherit by the 'Mine'
    class??

    Cheers,
    Manuel
    --
    Posted via http://www.ruby-forum.com/.
     
    Manuel Manuelson, Sep 10, 2010
    #1
    1. Advertising

  2. Hi.

    On Thu, Sep 9, 2010 at 6:23 PM, Manuel Manuelson <> wro=
    te:
    > Hi,
    >
    > I'm a newbie in Ruby and created this module:
    >
    > require 'couchrest_model'
    > module RdfModelModule
    >
    > =A0class RdfModel < CouchRest::Model::Base
    > =A0 =A0def rdf_schema
    > =A0 =A0 =A0p 'hello'
    > =A0 =A0end
    > =A0end
    >
    > end
    >
    >
    > When I create a new class based no this class like this:
    >
    > require 'rdfmodelmodule'
    > class Mine < RdfModelModule::RdfModel
    > =A0rdf_schema
    > end
    >
    > I'm getting this error:
    > NoMethodError: undefined method `rdf_schema' for Mine:Class
    >
    >
    > This is a bit confusing! Why is rdf_schema not inherit by the 'Mine'
    > class??


    It's because you are sending the "rdf_schema" message (trying to
    invoke the method) on the *class* Mine. When you right code in a class
    definition that isn't part of a method, as you did, that code is
    executed in the context of the class; i.e. any methods you invoke will
    be sent to the class object, instead of an instance of that class.
    rdf_schema is an instance method, so you need an instance of Mine to
    invoke it.

    You could make rdf_schema a class method, and then your code would work:

    class RdfModel < CouchRest::Model::Base
    def self.rdf_schema # note "self."
    p 'hello'
    end
    end

    I'm not sure if that's what you want your code to do, though. The
    other way would be to change the code in Mine so that it's inside an
    instance method, as Daniel G. wrote.
     
    Eric Christopherson, Sep 10, 2010
    #2
    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. =?Utf-8?B?Q2hyaXN0b3BoZXIgQXR0YXJk?=

    Inherit Page_Load method

    =?Utf-8?B?Q2hyaXN0b3BoZXIgQXR0YXJk?=, Feb 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,566
    =?Utf-8?B?Q2hyaXN0b3BoZXIgQXR0YXJk?=
    Feb 3, 2004
  2. chris
    Replies:
    2
    Views:
    330
    Nicodemus
    Mar 1, 2004
  3. fdb
    Replies:
    0
    Views:
    364
  4. Paul McGuire
    Replies:
    5
    Views:
    373
  5. Jean-Michel Pichavant

    Re: Override a method but inherit the docstring

    Jean-Michel Pichavant, Jul 27, 2009, in forum: Python
    Replies:
    2
    Views:
    282
    Jean-Michel Pichavant
    Jul 28, 2009
Loading...

Share This Page