[ruby-talk:00334] Re: module-class calling

From: "Michael Neumann" <neumann@...>
Date: 1999-05-22 15:47:26 UTC
List: ruby-talk #334
Hi,


> Another solution is to let `hello' be a module function by
> `module_function':
>
>   module A
>     def hello
>       print "HELLO\n"
>     end
>     module_function :hello
>   end
>
>   class B
>     def call_A
>       A.hello
>     end
>   end
>
>   x = B.new
>   x.call_A
>
> Module.html#module_function says,
>
> the module functions are the method which is
>         also the singleton method of a module (or a class).
>
> If a module is included the module functions seems not methods but
> just functions.  It is because of they are called module *functions*,
> I think. In case of calling module functions, preceding module name
> (e.g., Math or A) plays like a name space rather than a receiver.  It
> is very similar to the case of a constant with scope operator `::'
> (e.g, Math::PI) -- in deed, class methods or module functions can be
> call with :: (e.g., Math::sin(x)) in Ruby 1.3.x.
>
> hope this helps,
>
> -- gotoken
>

Thanks..

But now I have to declare also the functions, which are called by "hello" as
module_functions.
Following example does not work (without :bye):

 module A
    def bye
      print "BYE\n"
    end

    def hello
      print "HELLO\n"
      bye
    end

    module_function :hello   #, :bye
  end

  class B
    def call_A
      A.hello
    end
  end

  x = B.new
  x.call_A

Is the only solution to declare all functions inside the module, which are
called from a module-function ('hello'),
as module_function (like 'bye'), even if they are not called from outside
the module?


Michael


In This Thread