Question of self

Discussion in 'Ruby' started by Daniel Waite, Apr 9, 2008.

  1. Daniel Waite

    Daniel Waite Guest

    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?
    --
    Posted via http://www.ruby-forum.com/.
     
    Daniel Waite, Apr 9, 2008
    #1
    1. Advertising

  2. Daniel Waite

    Daniel Waite Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Daniel Waite, Apr 9, 2008
    #2
    1. Advertising

  3. Hi --

    On Thu, 10 Apr 2008, Daniel Waite wrote:

    > 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!
     
    David A. Black, Apr 9, 2008
    #3
  4. Hi --

    On Thu, 10 Apr 2008, Daniel Waite wrote:

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


    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!
     
    David A. Black, Apr 9, 2008
    #4
  5. Daniel Waite

    Daniel Waite Guest

    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?
    --
    Posted via http://www.ruby-forum.com/.
     
    Daniel Waite, Apr 9, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    628
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    628
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    433
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    406
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    427
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page