Include inside and outside classes

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

On 10/14/06, John Ky [email protected] 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/

John Ky schrieb:

end
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

On 10/16/06, Pit C. [email protected] wrote:

puts sqrt(169)
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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs