Accessing functions in a module without 'include'

Discussion in 'Ruby' started by Ronald Fischer, Jun 29, 2007.

  1. Usually I work with modules like this:

    # Defining a module in one file
    module M
    def f(x)
    ....
    end
    end

    # Using the module in another file
    require 'M'
    include M
    f(45)

    Sometimes I would find it more convenient to *not* inject
    the module's namespace into the user's namespace, i.e. to
    do it without the include statement. I thought it would=20
    be easy to qualify the foreign function with the module
    name:

    # Using the module in another file
    # (this does not work)
    require 'M'
    M::f(45) # Error: undefined method 'f'

    Maybe I'm thinking to Perlish here. Can it be done what I want
    to achieve, and how?

    Ronald
    --=20
    Ronald Fischer <>
    Phone: +49-89-452133-162
    =20
     
    Ronald Fischer, Jun 29, 2007
    #1
    1. Advertising

  2. Ronald Fischer

    Trans Guest

    On Jun 29, 5:25 am, "Ronald Fischer" <>
    wrote:
    > Usually I work with modules like this:
    >

    # Defining a module in one file
    module M
    module_function # <<<<<<<<< HERE
    def f(x)
    ....
    end
    end

    T.
     
    Trans, Jun 29, 2007
    #2
    1. Advertising

  3. > > Sometimes I would find it more convenient to *not* inject
    > > the module's namespace into the user's namespace, i.e. to
    > > do it without the include statement. I thought it would=20
    > > be easy to qualify the foreign function with the module
    > > name:
    > >=20
    > > # Using the module in another file
    > > # (this does not work)
    > > require 'M'
    > > M::f(45) # Error: undefined method 'f'
    > >=20
    > > Maybe I'm thinking to Perlish here. Can it be done what I want
    > > to achieve, and how?

    >=20
    > Not sure if it's what you want, but if you define M as=20
    > follows it works:
    >=20
    > module M
    > def self.f(x)
    > ....
    > end
    > end


    Thank you, this works perfectly well!

    Ronald
     
    Ronald Fischer, Jul 2, 2007
    #3
  4. > # Defining a module in one file
    > module M
    > module_function # <<<<<<<<< HERE
    > def f(x)
    > ....
    > end
    > end


    Thank you, this is indeed one solution to my problem (though in the
    end, I'm going to stick with Michael Hollins' proposal).

    Ronald
     
    Ronald Fischer, Jul 2, 2007
    #4
  5. Ronald Fischer

    Trans Guest

    On Jul 2, 5:34 am, "Ronald Fischer" <> wrote:
    > > # Defining a module in one file
    > > module M
    > > module_function # <<<<<<<<< HERE
    > > def f(x)
    > > ....
    > > end
    > > end

    >
    > Thank you, this is indeed one solution to my problem (though in the
    > end, I'm going to stick with Michael Hollins' proposal).


    Don't mention it :) BTW, Just to make sure you know, you can't use
    Micheal's solution if you still want the option of including the
    Module elsewhere. In effect using module_function is the same as:

    module M
    def self.f(x)
    ....
    end
    def f(x)
    ....
    end
    private :f
    end

    Probably you've already figured that out, but just in case...

    T.
     
    Trans, Jul 2, 2007
    #5
  6. > BTW, Just to make sure you know, you can't use
    > Micheal's solution if you still want the option of including the
    > Module elsewhere. In effect using module_function is the same as:
    >=20
    > module M
    > def self.f(x)
    > ....
    > end
    > def f(x)
    > ....
    > end
    > private :f
    > end


    I wasn't aware that the instance functions would then go private,
    but in my case this would be no problem anyway.

    Ronald
     
    Ronald Fischer, Jul 2, 2007
    #6
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,615
    Victor Bazarov
    Jun 21, 2004
  2. Andreas Bogenberger
    Replies:
    3
    Views:
    940
    Andreas Bogenberger
    Feb 22, 2008
  3. Brian Buckley

    include Module in another Module

    Brian Buckley, Feb 19, 2007, in forum: Ruby
    Replies:
    3
    Views:
    129
    Marcello Barnaba
    Feb 20, 2007
  4. Iñaki Baz Castillo
    Replies:
    3
    Views:
    154
    Iñaki Baz Castillo
    May 2, 2008
  5. Ittay Dror

    include module in module

    Ittay Dror, Dec 8, 2008, in forum: Ruby
    Replies:
    2
    Views:
    101
    Robert Klemme
    Dec 8, 2008
Loading...

Share This Page