Question of self

D

Daniel Waite

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?
 
D

Daniel Waite

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

">>> Self is: #{ self } -> #{ self.class }"

... and got...

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

David A. Black

Hi --

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?

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


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.rubypal.com for details and updates!
 
D

David A. Black

Hi --

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

">>> Self is: #{ self } -> #{ self.class }"

... and got...


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.

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

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.rubypal.com for details and updates!
 
D

Daniel Waite

Thanks for the quick reply!
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top