On 10/14/06, John Ky <newhoggy / gmail.com> wrote:
> Hi,
>
> Why is it that when I can do this:
>
> > include Math
> > puts sqrt(169)

The method sqrt is a module method of Math.

Including a module at the toplevel makes the modules module methods,
private methods of the toplevel object main.

That's why you can call sqrt(x) at the top level.  It's private
so you can't do:

    self.sqrt(169)
either.



> I can't do this?:
>
> > class X
> >   include Math
> >   puts sqrt(169)
> > end
>
> Does include do different things depending on where it is use?  Is it
> even the same include?

As I said at the top level it makes module methods available as
private instance methods of the toplevel object.

In a class or module, include makes the instance methods, constants
and module variables of the included module available in the including
class/module.

Since sqrt is a module method and not an instance method of Module, it
doesn't  become part of X. It has to be called as Math::sqrt(val), or
Math.sqrt(val).

And they appear to be two different methods:
irb(main):001:0> self.method(:include)
=> #<Method: main.include>
irb(main):002:0> Module.method(:include)
=> #<Method: Class(Module)#include>



-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/