Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Method not inherit? (http://www.velocityreviews.com/forums/t864364-method-not-inherit.html)

Manuel Manuelson 09-09-2010 11:23 PM

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
--
Posted via http://www.ruby-forum.com/.


Eric Christopherson 09-10-2010 12:08 AM

Re: Method not inherit?
 
Hi.

On Thu, Sep 9, 2010 at 6:23 PM, Manuel Manuelson <b1180132@bofthew.com> 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.



All times are GMT. The time now is 03:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57