Method not inherit?

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

Hello,

That method is inherited, but only instances can use it:

require ‘rdfmodelmodule’
class Mine < RdfModelModule::RdfModel
def something_else
self.rdf_schema
end
end

This works

Mine.new.rdf_schema
Mine.new.something_else

Daniel Gaytán
http://survey.richapplabs.com/index.php?sid=62661&lang=en

2010/9/9 Manuel M. [email protected]

Hi.

On Thu, Sep 9, 2010 at 6:23 PM, Manuel M. [email protected]
wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs