simple module method call question

Discussion in 'Ruby' started by Tom Cloyd, Apr 17, 2009.

  1. Tom Cloyd

    Tom Cloyd Guest

    I'm making no progress on this, and so clearly have missed something basic.

    I have a class which requires a file containing module Utils_db. There
    is another module 'required' in the program which instantiates this class.

    I then successfully access in the class two constants - one from each
    module.

    Utils::MENU
    Utils_db::MENU

    Now the trouble starts.

    Right after accessing the constants, I try to call a method in one of
    the modules -

    eval("Utils.orph_chk") <= I will be replacing the string with a
    construction using a variable, in the final version

    This always fails. I've tried every trick I can think of, but always I
    get this:

    ../lib/setnet/SN.rb:579:in `proc_command': undefined method `orph_chk'
    for Utils:Module (NoMethodError)
    from ../lib/setnet/SN.rb:229:in `eval'
    from ../lib/setnet/SN.rb:579:in `proc_command'
    from ../lib/setnet/SN.rb:229:in `user_interface'
    from ../lib/setnet/SN.rb:146:in `manage'
    from ../lib/setnet/SN.rb:68:in `startup'
    from ./setnet:28

    Can someone suggest why I'm not able to get to this method?

    Here's the relevant Module, in abbreviated form:

    module Utils

    MENU = [
    ["orph_chk", "searches nodes and links for references which cannot be
    resolved, and reports them"],
    ["tree_count","starting with node 'root', locates descendants of each
    node, displaying count at each level"],
    ["showtree","starting with node 'root', locates and displays
    descendants of each node"],
    ["tree_orphans","locates links which cannot be traced to the root
    node, and displays them"]
    ]

    def orph_chk
    puts 'orph_chk'
    end

    end

    Any suggestions would be much appreciated. I cannot quite figure out
    what to try next.

    Thanks,

    Tom
     
    Tom Cloyd, Apr 17, 2009
    #1
    1. Advertising

  2. Tom Cloyd

    James Coglan Guest

    [Note: parts of this message were removed to make it a legal post.]

    > This always fails. I've tried every trick I can think of, but always I get
    > this:
    >
    > ../lib/setnet/SN.rb:579:in `proc_command': undefined method `orph_chk' for
    > Utils:Module (NoMethodError)
    > from ../lib/setnet/SN.rb:229:in `eval'
    > from ../lib/setnet/SN.rb:579:in `proc_command'
    > from ../lib/setnet/SN.rb:229:in `user_interface'
    > from ../lib/setnet/SN.rb:146:in `manage'
    > from ../lib/setnet/SN.rb:68:in `startup'
    > from ./setnet:28
    >
    > Can someone suggest why I'm not able to get to this method?
    >
    > Here's the relevant Module, in abbreviated form:
    >
    > module Utils
    >
    > MENU = [
    > ["orph_chk", "searches nodes and links for references which cannot be
    > resolved, and reports them"],
    > ["tree_count","starting with node 'root', locates descendants of each
    > node, displaying count at each level"],
    > ["showtree","starting with node 'root', locates and displays descendants
    > of each node"],
    > ["tree_orphans","locates links which cannot be traced to the root node,
    > and displays them"]
    > ]
    >
    > def orph_chk
    > puts 'orph_chk'
    > end
    >
    > end




    You've defined an instance method, not a module method. The method as you
    define it will appear in classes that use 'include Utils'. To get a module
    method, define it on self:

    module Utils
    MENU = [ ... ]

    def self.orph_chk
    # ...
    end
    end

    --
    James Coglan
    http://github.com/jcoglan
     
    James Coglan, Apr 17, 2009
    #2
    1. Advertising

  3. Tom Cloyd

    Ben Lovell Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Fri, Apr 17, 2009 at 1:00 PM, Tom Cloyd <> wrote:

    >
    > eval("Utils.orph_chk") <= I will be replacing the string with a
    > construction using a variable, in the final version
    >
    > This always fails. I've tried every trick I can think of, but always I get
    > this:
    >
    > ../lib/setnet/SN.rb:579:in `proc_command': undefined method `orph_chk' for
    > Utils:Module (NoMethodError)
    > from ../lib/setnet/SN.rb:229:in `eval'
    > from ../lib/setnet/SN.rb:579:in `proc_command'
    > from ../lib/setnet/SN.rb:229:in `user_interface'
    > from ../lib/setnet/SN.rb:146:in `manage'
    > from ../lib/setnet/SN.rb:68:in `startup'
    > from ./setnet:28
    >
    > Can someone suggest why I'm not able to get to this method?
    > Any suggestions would be much appreciated. I cannot quite figure out what
    > to try next.
    >
    > Thanks,
    >
    > Tom
    >


    Hi,

    Why are you eval'ing this? This works:

    eval("include Utils; Utils.orph_chk")

    I would think it has something to do with your binding when you eval.

    Ben
     
    Ben Lovell, Apr 17, 2009
    #3
  4. Tom Cloyd

    Tom Cloyd Guest

    James Coglan wrote:
    >> This always fails. I've tried every trick I can think of, but always I get
    >> this:
    >>
    >> ../lib/setnet/SN.rb:579:in `proc_command': undefined method `orph_chk' for
    >> Utils:Module (NoMethodError)
    >> from ../lib/setnet/SN.rb:229:in `eval'
    >> from ../lib/setnet/SN.rb:579:in `proc_command'
    >> from ../lib/setnet/SN.rb:229:in `user_interface'
    >> from ../lib/setnet/SN.rb:146:in `manage'
    >> from ../lib/setnet/SN.rb:68:in `startup'
    >> from ./setnet:28
    >>
    >> Can someone suggest why I'm not able to get to this method?
    >>
    >> Here's the relevant Module, in abbreviated form:
    >>
    >> module Utils
    >>
    >> MENU = [
    >> ["orph_chk", "searches nodes and links for references which cannot be
    >> resolved, and reports them"],
    >> ["tree_count","starting with node 'root', locates descendants of each
    >> node, displaying count at each level"],
    >> ["showtree","starting with node 'root', locates and displays descendants
    >> of each node"],
    >> ["tree_orphans","locates links which cannot be traced to the root node,
    >> and displays them"]
    >> ]
    >>
    >> def orph_chk
    >> puts 'orph_chk'
    >> end
    >>
    >> end
    >>

    >
    >
    >
    > You've defined an instance method, not a module method. The method as you
    > define it will appear in classes that use 'include Utils'. To get a module
    > method, define it on self:
    >
    > module Utils
    > MENU = [ ... ]
    >
    > def self.orph_chk
    > # ...
    > end
    > end
    >
    > --
    > James Coglan
    > http://github.com/jcoglan
    >
    >

    Well. Live and learn. Very simple solution, but I sure wasn't looking in
    THAT corner of the universe.

    Thank you very much.

    t.

    --

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
    Bellingham, Washington, U.S.A: (360) 920-1226
    << >> (email)
    << TomCloyd.com >> (website)
    << sleightmind.wordpress.com >> (mental health weblog)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
    Tom Cloyd, Apr 17, 2009
    #4
  5. Tom Cloyd

    Tom Cloyd Guest

    Ben Lovell wrote:
    > On Fri, Apr 17, 2009 at 1:00 PM, Tom Cloyd <> wrote:
    >
    >
    >> eval("Utils.orph_chk") <= I will be replacing the string with a
    >> construction using a variable, in the final version
    >>
    >> This always fails. I've tried every trick I can think of, but always I get
    >> this:
    >>
    >> ../lib/setnet/SN.rb:579:in `proc_command': undefined method `orph_chk' for
    >> Utils:Module (NoMethodError)
    >> from ../lib/setnet/SN.rb:229:in `eval'
    >> from ../lib/setnet/SN.rb:579:in `proc_command'
    >> from ../lib/setnet/SN.rb:229:in `user_interface'
    >> from ../lib/setnet/SN.rb:146:in `manage'
    >> from ../lib/setnet/SN.rb:68:in `startup'
    >> from ./setnet:28
    >>
    >> Can someone suggest why I'm not able to get to this method?
    >> Any suggestions would be much appreciated. I cannot quite figure out what
    >> to try next.
    >>
    >> Thanks,
    >>
    >> Tom
    >>
    >>

    >
    > Hi,
    >
    > Why are you eval'ing this?

    Because the name of one of my two modules isn't known until class
    instantiation time. It could literally be anything. That said, I'm not
    at all sure my approach is the best. It's just the only one I could
    think of. It's also my very first use of eval, and it well may be poorly
    chosen.
    > This works:
    >
    > eval("include Utils; Utils.orph_chk")
    >

    Hmm. I tried that, and got some kind of method error on the "include". I
    studied it for a good long while and simply couldn't see the problem.
    > I would think it has something to do with your binding when you eval.
    >

    Yeah, me two. I've never even thought about binding issues before, but I
    sure am now!

    Thanks for your thoughts.

    t.

    --

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
    Bellingham, Washington, U.S.A: (360) 920-1226
    << >> (email)
    << TomCloyd.com >> (website)
    << sleightmind.wordpress.com >> (mental health weblog)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
    Tom Cloyd, Apr 17, 2009
    #5
  6. James Coglan wrote:
    > You've defined an instance method, not a module method. The method as
    > you define it will appear in classes that use 'include Utils'. To get a
    > module method, define it on self:
    >
    > module Utils
    > MENU = [ ... ]
    >
    > def self.orph_chk
    > # ...
    > end
    > end


    Alternatively, you could use module_function() to make the instance
    method a class method on the module:

    module Utils
    def orph_chk
    # ...
    end

    module_function :eek:rph_chk
    end


    Now you can either call the method directly (Utils.orph_chk) or mix it
    in (include Utils; orph_chk). So you can have your cake and eat it too!
    --
    Posted via http://www.ruby-forum.com/.
     
    Suraj Kurapati, Apr 18, 2009
    #6
  7. Tom Cloyd

    Tom Cloyd Guest

    Suraj Kurapati wrote:
    > James Coglan wrote:
    >
    >> You've defined an instance method, not a module method. The method as
    >> you define it will appear in classes that use 'include Utils'. To get a
    >> module method, define it on self:
    >>
    >> module Utils
    >> MENU = [ ... ]
    >>
    >> def self.orph_chk
    >> # ...
    >> end
    >> end
    >>

    >
    > Alternatively, you could use module_function() to make the instance
    > method a class method on the module:
    >
    > module Utils
    > def orph_chk
    > # ...
    > end
    >
    > module_function :eek:rph_chk
    > end
    >
    >
    > Now you can either call the method directly (Utils.orph_chk) or mix it
    > in (include Utils; orph_chk). So you can have your cake and eat it too!
    >

    Hmmm. This is most interesting. Ruby amazes me every day. So much
    built-in cleverness. And now this, which I've never seen anywhere. I'll
    study up on it. Thanks very much for the heads up,

    Tom

    --

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
    Bellingham, Washington, U.S.A: (360) 920-1226
    << >> (email)
    << TomCloyd.com >> (website)
    << sleightmind.wordpress.com >> (mental health weblog)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
    Tom Cloyd, Apr 18, 2009
    #7
    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. Maric Michaud
    Replies:
    0
    Views:
    7,219
    Maric Michaud
    Jun 24, 2006
  2. Joshua Muheim
    Replies:
    4
    Views:
    114
    Robert Dober
    Nov 23, 2007
  3. Adrian Klingel

    How to call a module method dynamically

    Adrian Klingel, Feb 21, 2009, in forum: Ruby
    Replies:
    10
    Views:
    206
    lasitha
    Feb 22, 2009
  4. Wladimir Borsov
    Replies:
    4
    Views:
    322
    Tintin
    May 11, 2006
  5. perl Newbie
    Replies:
    2
    Views:
    937
    perl Newbie
    May 6, 2009
Loading...

Share This Page