Subclasses: Class
A Module
is a collection of methods and constants. The methods in a module may be instance methods or module methods. Instance methods appear as methods in a class when the module is included, module methods do not. Conversely, module methods may be called without creating an encapsulating object, while instance methods may not. See Module#module_function
.
In the descriptions that follow, the parameter aSymbol refers to a symbol, which is either a quoted string or a Symbol
(such as :name
).
module Mod
include Math
CONST = 1
def meth
# ...
end
end
Mod.type → Module
Mod.constants → ["CONST", "E", "PI"]
Mod.instance_methods → ["meth"]
p Module.constants.sort[1..5]
produces:
["ARGV", "ArgumentError", "Array", "Bignum", "Binding"]
Modules
nested at the point of call.
module M1
module M2
$a = Module.nesting
end
end
$a → [M1::M2, M1]
$a[0].name → "M1::M2"
relop
aModule → true
or false
module Mixin
end
module Parent
include Mixin
end
module Unrelated
end
Parent > Mixin → false
Parent < Mixin → true
Parent <= Parent → true
Parent < Unrelated → false
Parent > Unrelated → false
true
or false
true
if anObject is an instance of mod
or one of mod's descendents. Of limited use for modules, but
can be used in case
statements to classify objects by class.
module Mod
include Math
include Comparable
end
Mod.ancestors → [Mod, Comparable, Math]
Math.ancestors → [Math]
Module.module_eval
.
class One
@@var1 = 1
end
class Two < One
@@var2 = 2
end
One.class_variables → ["@@var1"]
Two.class_variables → ["@@var2", "@@var1"]
m = Math.clone → Math
m.constants → ["E", "PI"]
m == Math → false
true
or false
true
if a constant with the given name is defined by mod.
Math.const_defined? "PI" → true
Math.const_get :PI → 3.141592654
Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) → 3.142857143
Math::HIGH_SCHOOL_PI - Math::PI → 0.001264489267
module Mixin
end
module Outer
include Mixin
end
Mixin.included_modules → []
Outer.included_modules → [Mixin]
false
) → anArrayfalse
, the instance methods in mod are returned, otherwise the methods in mod and mod's superclasses are returned.
module A
def method1() end
end
class B
def method2() end
end
class C < B
def method3() end
end
A.instance_methods → ["method1"]
B.instance_methods → ["method2"]
C.instance_methods → ["method3"]
C.instance_methods(true).length → 39
true
or false
true
if the named method is defined by mod (or its included modules and, if mod is a class, its ancestors).
module A
def method1() end
end
class B
def method2() end
end
class C < B
include A
def method3() end
end
A.method_defined? :method1 → true
C.method_defined? "method1" → true
C.method_defined? "method2" → true
C.method_defined? "method3" → true
C.method_defined? "method4" → false
module_eval
returns the result of evaluating its argument.
class Thing
end
a = %q{def hello() "Hello there!" end}
Thing.module_eval(a) → nil
Thing.new.hello() → "Hello there!"
nil
new
.
class SimpleSingleton # Not thread safe
private_class_method :new
def SimpleSingleton.create(*args, &block)
@me = new(*args, &block) if ! @me
@me
end
end
false
) → anArrayfalse
, the methods of any ancestors are included.
module Mod
def method1() end
private :method1
def method2() end
end
Mod.instance_methods → ["method2"]
Mod.private_instance_methods → ["method1"]
false
) → anArrayfalse
, the methods of any ancestors are included. nil
false
) → anArrayfalse
, the methods of any ancestors are included.The following methods are used mainly during the definition of classes and modules.
module Mod
alias_method :origExit, :exit
def exit(code=0)
print "Exiting with code #{code}\n"
origExit(code)
end
end
include Mod
exit(99)
produces:
Exiting with code 99
append_features
in this module, passing it the receiving module in aModule. Ruby's default implementation is to add the constants, methods, and module variables of this module to aModule if this module has not already been added to aModule or one of its ancestors. See also Module#include
. false
) → nil
id2name
, creating an instance variable (@name) and a corresponding access method to read it. If the optional writable argument is true
, also creates a method called name=
to set the attribute.
module Mod
attr :size, true
end
module Mod
def size
@size
end
def size=(val)
@size = val
end
end
nil
attr
aSymbol, true
” on each aSymbol in turn.
module Mod
attr_accessor(:one, :two)
end
Mod.instance_methods.sort → ["one", "one=", "two", "two="]
nil
attr
:name” on each name in turn. nil
.id2name
.
Object#extend
.
module Picky
def Picky.extend_object(o)
if String === o
print "Can't add Picky to a String\n"
else
print "Picky added to ", o.type, "\n"
super
end
end
end
(s = Array.new).extend Picky # Call Object.extend
(s = "quick brown fox").extend Picky
produces:
Picky added to Array
Can't add Picky to a String
Module.append_features
on each parameter in turn. module Chatty
def Chatty.method_added(id)
print "Adding ", id.id2name, "\n"
end
def one() end
end
module Chatty
def two() end
end
produces:
Adding one
Adding two
module Mod
def one
"This is one"
end
module_function :one
end
class Cls
include Mod
def callOne
one
end
end
Mod.one → "This is one"
c = Cls.new
c.callOne → "This is one"
module Mod
def one
"This is the new one"
end
end
Mod.one → "This is one"
c.callOne → "This is the new one"
module Mod
def a() end
def b() end
private
def c() end
private :a
end
Mod.private_instance_methods → ["a", "c"]
Module.undef_method
.
remove_method
, which deletes the method from the particular class; Ruby will still search superclasses and mixed-in modules for a possible receiver.
class Parent
def hello
print "In parent\n"
end
end
class Child < Parent
def hello
print "In child\n"
end
end
c = Child.new
c.hello
class Child
remove_method :hello # remove from child, still in parent
end
c.hello
class Child
undef_method :hello # prevent any calls to 'hello'
end
c.hello
produces:
In child
In parent
prog.rb:23: undefined method `hello' for #<Child:0x401b5928> (NameError)
Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).
Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.