# Question of self

**URL:** <https://www.ruby-forum.com/t/question-of-self/134342>\
**Category:** Ruby\
**Created:** [April 9, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/question-of-self/134342 "2008-04-09T23:08:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 9, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/question-of-self/134342/1 "2008-04-09T23:08:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 9, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/question-of-self/134342/2 "2008-04-09T23:10:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 9, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/question-of-self/134342/3 "2008-04-09T23:17:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 9, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/question-of-self/134342/4 "2008-04-09T23:20:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 9, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/question-of-self/134342/5 "2008-04-09T23:43:42Z")

</div>

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](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?
