include inside and outside classes

J

John Ky

Hi,

Why is it that when I can do this:
include Math
puts sqrt(169)

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?

Thanks

-John
 
R

Rick DeNatale

Hi,

Why is it that when I can do this:

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?:


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>
 
P

Pit Capitain

John said:
Why is it that when I can do this:


I can't do this?:

Rick has already written about the differences between class scope and
toplevel scope. (Are you reading this, Tom?)

With #include you can call the Math methods in instance methods:

class X
include Math
def m
puts sqrt(169)
end
end

X.new.m # => 13.0

If you want to call the methods in class scope, you have to use #extend:

class X
extend Math
puts sqrt(169) # => 13.0
end

Regards,
Pit
 
R

Rick DeNatale

Rick has already written about the differences between class scope and
toplevel scope. (Are you reading this, Tom?)

With #include you can call the Math methods in instance methods:

class X
include Math
def m
puts sqrt(169)
end
end

X.new.m # => 13.0

And the reason that this works is that these functions are module
functions which act like a combination of class methods for the
module, and private instance methods.

The first lets you say

Math.sqrt(4)

and the second lets you say

class A
include Math
def meth
sqrt(4)
end
end

And the way you make a module function in one of your own modules is
either like this:

module MyModule

def foo
end

module_function :foo
end

or

module MyModule

module_function
def foo
end
end

The module_function method works like private and protected. With
arguments it makes specific methods module_functions, without
arguments it makes subsequently defined methods module_functions.


--
Rick DeNatale

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

IPMS/USA Region 12 Coordinator
http://ipmsr12.denhaven2.com/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,215
Messages
2,571,113
Members
47,710
Latest member
HarleyMoli

Latest Threads

Top