self.included(base) and base.class_eval problems, etc.

Discussion in 'Ruby' started by n8agrin@gmail.com, Mar 8, 2006.

  1. Guest

    I spent a whole night trying to figure out why a module I was including
    wasn't producing any results.

    I finally narrowed it down to this bit of code in the module:

    def self.included(base)
    base.send :current_user, :logged_in?, :user?
    end

    For some reason, commenting it out lets everything run fine, and it
    seems that leaving it in causes the module to fail silently.

    Can someone explain to me what this is doing? I've seen similar calls
    to self.included(base) which ended with base.extend(SomeModule).

    I searched high and low for documentation on this, but found nothing.
    My best guess is it's some quasi-magic method that's trying to allow
    ActionView to use these methods as well. Any idea why it's failing or
    where I can read up on this?

    Thanks!
     
    , Mar 8, 2006
    #1
    1. Advertising

  2. Lou Vanek Guest

    Running this command over my gems,

    >find /lib/ruby/g*/1*/g* -iname "*.rb" -print | xargs egrep "\<def\>.*included\>" |

    grep 3758 | wc
    22 74 2119

    is saying that there are about 22 places where this function has already
    been defined in rails build 3758. You are probably overwriting one of these.
    Changing your functions name should solve your problem. Or you could try calling
    'super' in your function. I guess running ruby with warnings turned on will tell
    you if you are redefining the function.



    wrote:

    > I spent a whole night trying to figure out why a module I was including
    > wasn't producing any results.
    >
    > I finally narrowed it down to this bit of code in the module:
    >
    > def self.included(base)
    > base.send :current_user, :logged_in?, :user?
    > end
    >
    > For some reason, commenting it out lets everything run fine, and it
    > seems that leaving it in causes the module to fail silently.
    >
    > Can someone explain to me what this is doing? I've seen similar calls
    > to self.included(base) which ended with base.extend(SomeModule).
    >
    > I searched high and low for documentation on this, but found nothing.
    > My best guess is it's some quasi-magic method that's trying to allow
    > ActionView to use these methods as well. Any idea why it's failing or
    > where I can read up on this?
    >
    > Thanks!
    >
    >
    >
     
    Lou Vanek, Mar 8, 2006
    #2
    1. Advertising

  3. On Mar 8, 2006, at 1:38 AM, wrote:

    > I spent a whole night trying to figure out why a module I was
    > including
    > wasn't producing any results.
    >
    > I finally narrowed it down to this bit of code in the module:
    >
    > def self.included(base)
    > base.send :current_user, :logged_in?, :user?
    > end
    >
    > For some reason, commenting it out lets everything run fine, and it
    > seems that leaving it in causes the module to fail silently.


    The default included() is what adds the methods, so you need to add a
    call to super to keep that behavior.

    James Edward Gray II
     
    James Edward Gray II, Mar 8, 2006
    #3
    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:
    612
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    613
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    421
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    397
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    420
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page