Question of self

module FunFunModule

def self.included(base)
base.extend ClassMethods

class << base # <- Is this the instance of ActiveRecord?
  ...
end

end

module ClassMethods

end

end

The above code is included into ActiveRecord via…

ActiveRecord::Base.send :include, FunFunModule

Is the class << base on line 6 the actual instance of the AR model?

Oops. Forgot to add that I logged the output of…

“>>> Self is: #{ self } -> #{ self.class }”

… and got…

Self is: #Class:ActiveRecord::Base -> Class

I recognize the #Class:... bit as a singleton, or metaclass, but is
that also the instance? I was under the impression there was only one
metaclass per object.

Hi –

On Thu, 10 Apr 2008, Daniel W. wrote:

module ClassMethods

end

end

The above code is included into ActiveRecord via…

ActiveRecord::Base.send :include, FunFunModule

Is the class << base on line 6 the actual instance of the AR model?

No; it’s the singleton class of AR::Base.

David

Hi –

On Thu, 10 Apr 2008, Daniel W. wrote:

metaclass per object.
There is. I’m not sure what you mean by the instance. You haven’t
instantiated ActiveRecord::Base in your example, so there aren’t any
instances of it around, and in any case they’d be unrelated to the
singleton class of their class.

I’m not sure what’s leading you to interpret it that way, so I’m not
sure I’m explaining it adequately.

David

Thanks for the quick reply!

David A. Black wrote:

You haven’t instantiated ActiveRecord::Base in your
example, so there aren’t any instances of it around…

That seems so obvious now that you say that.

That said, I have better-defined my goal: to conditionally create an
instance-level method.

Here’s my plugin:
http://pastie.caboo.se/178169

Note the recommend method towards the bottom. That method should only be
available to objects who recommend (who are ‘recommenders’).

I understand class definitions are active (to use your language, I
believe ;), but it feels dirty to specify a conditional outside a
method.

Even if I did use a conditional, I wouldn’t have access to the
information passed in by the including object. E.g…

class Category

acts_as_recommendation :recommender => true

end

I suppose I could break the plugin in two: acts_as_recommender and
acts_as_recommendable… =/

Thoughts?

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