Class: Module
Overview
*********************************************************************
A <code>Module</code> 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 <code>Module#module_function</code>)
In the descriptions that follow, the parameter <i>syml</i> refers
to a symbol, which is either a quoted string or a
<code>Symbol</code> (such as <code>:name</code>).
module Mod
include Math
CONST = 1
def meth
# ...
end
end
Mod.class #=> Module
Mod.constants #=> ["E", "PI", "CONST"]
Mod.instance_methods #=> ["meth"]
Direct Known Subclasses
Class Method Summary collapse
-
.constants ⇒ Array
Returns an array of the names of all constants defined in the system.
-
.nesting ⇒ Array
Returns the list of
Modules
nested at the point of call.
Instance Method Summary collapse
-
#<(other) ⇒ true, ...
Returns true if mod is a subclass of other.
-
#<=(other) ⇒ true, ...
Returns true if mod is a subclass of other or is the same as other.
-
#<=>(other_mod) ⇒ -1, ...
Comparison—Returns -1 if mod includes other_mod, 0 if mod is the same as other_mod, and +1 if mod is included by other_mod or if mod has no relationship with other_mod.
-
#== ⇒ Object
Equality—At the
Object
level,==
returnstrue
only if obj and other are the same object. -
#===(obj) ⇒ Boolean
Case Equality—Returns
true
if anObject is an instance of mod or one of mod’s descendents. -
#>(other) ⇒ true, ...
Returns true if mod is an ancestor of other.
-
#>=(other) ⇒ true, ...
Returns true if mod is an ancestor of other, or the two modules are the same.
-
#alias_method(new_name, old_name) ⇒ self
private
Makes new_name a new copy of the method old_name.
-
#ancestors ⇒ Array
Returns a list of modules included in mod (including mod itself).
-
#append_features(mod) ⇒ Object
private
When this module is included in another, Ruby calls
append_features
in this module, passing it the receiving module in mod. -
#attr(symbol, writable = false) ⇒ nil
private
Defines a named attribute for this module, where the name is symbol.
id2name
, creating an instance variable (@name
) and a corresponding access method to read it. -
#attr_accessor(symbol, ...) ⇒ nil
private
Equivalent to calling “
attr
symbol, true
” on each symbol in turn. -
#attr_reader(symbol, ...) ⇒ nil
private
Creates instance variables and corresponding methods that return the value of each instance variable.
-
#attr_writer(symbol, ...) ⇒ nil
private
Creates an accessor method to allow assignment to the attribute aSymbol
.id2name
. -
#autoload(name, filename) ⇒ nil
Registers filename to be loaded (using
Kernel::require
) the first time that name (which may be aString
or a symbol) is accessed in the namespace of mod. -
#autoload?(name) ⇒ String?
Returns filename to be loaded if name is registered as
autoload
in the namespace of mod. -
#class_eval ⇒ Object
Evaluates the string or block in the context of mod.
-
#class_variable_defined?(symbol) ⇒ Boolean
Returns
true
if the given class variable is defined in obj. -
#class_variable_get(symbol) ⇒ Object
private
Returns the value of the given class variable (or throws a
NameError
exception). -
#class_variable_set(symbol, obj) ⇒ Object
private
Sets the class variable names by symbol to object.
-
#class_variables ⇒ Array
Returns an array of the names of class variables in mod and the ancestors of mod.
-
#const_defined?(sym) ⇒ Boolean
Returns
true
if a constant with the given name is defined by mod. -
#const_get(sym) ⇒ Object
Returns the value of the named constant in mod.
-
#const_missing(sym) ⇒ Object
Invoked when a reference is made to an undefined constant in mod.
-
#const_set(sym, obj) ⇒ Object
Sets the named constant to the given object, returning that object.
-
#constants ⇒ Array
Returns an array of the names of the constants accessible in mod.
- #define_method ⇒ Object private
-
#extend_object(obj) ⇒ Object
private
Extends the specified object by adding this module’s constants and methods (which are added as singleton methods).
-
#extended ⇒ Object
private
Not documented.
-
#freeze ⇒ Object
Prevents further modifications to mod.
-
#include ⇒ self
private
Invokes
Module.append_features
on each parameter in turn. -
#include? ⇒ Boolean
Returns
true
if module is included in mod or one of mod’s ancestors. -
#included ⇒ Object
private
Not documented.
-
#included_modules ⇒ Array
Returns the list of modules included in mod.
-
#initialize ⇒ Object
constructor
Creates a new anonymous module.
-
#initialize_copy ⇒ Object
:nodoc:.
-
#instance_method(symbol) ⇒ Object
Returns an
UnboundMethod
representing the given instance method in mod. -
#instance_methods(include_super = true) ⇒ Array
Returns an array containing the names of public instance methods in the receiver.
-
#method_added ⇒ Object
private
Not documented.
-
#method_defined?(symbol) ⇒ Boolean
Returns
true
if the named method is defined by mod (or its included modules and, if mod is a class, its ancestors). -
#method_removed ⇒ Object
private
Not documented.
-
#method_undefined ⇒ Object
private
Not documented.
-
#module_eval ⇒ Object
Evaluates the string or block in the context of mod.
-
#module_function(symbol, ...) ⇒ self
private
Creates module functions for the named methods.
-
#name ⇒ String
Returns the name of the module mod.
-
#private ⇒ Object
private
With no arguments, sets the default visibility for subsequently defined methods to private.
-
#private_class_method(symbol, ...) ⇒ Object
Makes existing class methods private.
-
#private_instance_methods(include_super = true) ⇒ Array
Returns a list of the private instance methods defined in mod.
-
#private_method_defined?(symbol) ⇒ Boolean
Returns
true
if the named private method is defined by _ mod_ (or its included modules and, if mod is a class, its ancestors). -
#protected ⇒ Object
private
With no arguments, sets the default visibility for subsequently defined methods to protected.
-
#protected_instance_methods(include_super = true) ⇒ Array
Returns a list of the protected instance methods defined in mod.
-
#protected_method_defined?(symbol) ⇒ Boolean
Returns
true
if the named protected method is defined by mod (or its included modules and, if mod is a class, its ancestors). -
#public ⇒ Object
private
With no arguments, sets the default visibility for subsequently defined methods to public.
-
#public_class_method(symbol, ...) ⇒ Object
Makes a list of existing class methods public.
-
#public_instance_methods(include_super = true) ⇒ Array
Returns a list of the public instance methods defined in mod.
-
#public_method_defined?(symbol) ⇒ Boolean
Returns
true
if the named public method is defined by mod (or its included modules and, if mod is a class, its ancestors). -
#remove_class_variable(sym) ⇒ Object
private
Removes the definition of the sym, returning that constant’s value.
-
#remove_const(sym) ⇒ Object
private
Removes the definition of the given constant, returning that constant’s value.
-
#remove_method(symbol) ⇒ self
private
Removes the method identified by symbol from the current class.
-
#to_s ⇒ String
Return a string representing this module or class.
-
#undef_method(symbol) ⇒ self
private
Prevents the current class from responding to calls to the named method.
Constructor Details
#new ⇒ Object #new {|mod| ... } ⇒ Object
Creates a new anonymous module. If a block is given, it is passed the module object, and the block is evaluated in the context of this module using module_eval
.
Fred = Module.new do
def meth1
"hello"
end
def meth2
"bye"
end
end
a = "my string"
a.extend(Fred) #=> "my string"
a.meth1 #=> "hello"
a.meth2 #=> "bye"
1490 1491 1492 |
# File 'object.c', line 1490
static VALUE
rb_mod_initialize(module)
VALUE module;
|
Class Method Details
.constants ⇒ Array
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 |
# File 'eval.c', line 1978
static VALUE
rb_mod_s_constants()
{
NODE *cbase = ruby_cref;
void *data = 0;
while (cbase) {
if (!NIL_P(cbase->nd_clss)) {
data = rb_mod_const_at(cbase->nd_clss, data);
}
cbase = cbase->nd_next;
}
if (!NIL_P(ruby_cbase)) {
data = rb_mod_const_of(ruby_cbase, data);
}
return rb_const_list(data);
}
|
.nesting ⇒ Array
Returns the list of 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"
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 |
# File 'eval.c', line 1948
static VALUE
rb_mod_nesting()
{
NODE *cbase = ruby_cref;
VALUE ary = rb_ary_new();
while (cbase && cbase->nd_next) {
if (!NIL_P(cbase->nd_clss)) rb_ary_push(ary, cbase->nd_clss);
cbase = cbase->nd_next;
}
if (ruby_wrapper && RARRAY(ary)->len == 0) {
rb_ary_push(ary, ruby_wrapper);
}
return ary;
}
|
Instance Method Details
#<(other) ⇒ true, ...
Returns true if mod is a subclass of other. Returns nil
if there’s no relationship between the two. (Think of the relationship in terms of the class definition: “class A<B” implies “A<B”).
1359 1360 1361 |
# File 'object.c', line 1359 static VALUE rb_mod_lt(mod, arg) VALUE mod, arg; |
#<=(other) ⇒ true, ...
Returns true if mod is a subclass of other or is the same as other. Returns nil
if there’s no relationship between the two. (Think of the relationship in terms of the class definition: “class A<B” implies “A<B”).
1314 1315 1316 |
# File 'object.c', line 1314 VALUE rb_class_inherited_p(mod, arg) VALUE mod, arg; |
#<=>(other_mod) ⇒ -1, ...
Comparison—Returns -1 if mod includes other_mod, 0 if mod is the same as other_mod, and +1 if mod is included by other_mod or if mod has no relationship with other_mod. Returns nil
if other_mod is not a module.
1425 1426 1427 |
# File 'object.c', line 1425 static VALUE rb_mod_cmp(mod, arg) VALUE mod, arg; |
#==(other) ⇒ Boolean #equal?(other) ⇒ Boolean #eql?(other) ⇒ Boolean
Equality—At the Object
level, ==
returns true
only if obj and other are the same object. Typically, this method is overridden in descendent classes to provide class-specific meaning.
Unlike ==
, the equal?
method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b)
iff a
is the same object as b
).
The eql?
method returns true
if
<i>obj</i> and <i>anObject</i> have the
same value. Used by Hash
to test members for equality. For objects of class Object
, eql?
is synonymous with ==
. Subclasses normally continue this tradition, but there are exceptions. Numeric
types, for example, perform type conversion across ==
, but not across eql?
, so:
1 == 1.0 #=> true
1.eql? 1.0 #=> false
93 94 95 |
# File 'object.c', line 93 static VALUE rb_obj_equal(obj1, obj2) VALUE obj1, obj2; |
#===(obj) ⇒ Boolean
Case Equality—Returns 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.
1295 1296 1297 |
# File 'object.c', line 1295 static VALUE rb_mod_eqq(mod, arg) VALUE mod, arg; |
#>(other) ⇒ true, ...
Returns true if mod is an ancestor of other. Returns nil
if there’s no relationship between the two. (Think of the relationship in terms of the class definition: “class A<B” implies “B>A”).
1406 1407 1408 |
# File 'object.c', line 1406 static VALUE rb_mod_gt(mod, arg) VALUE mod, arg; |
#>=(other) ⇒ true, ...
Returns true if mod is an ancestor of other, or the two modules are the same. Returns nil
if there’s no relationship between the two. (Think of the relationship in terms of the class definition: “class A<B” implies “B>A”).
1380 1381 1382 |
# File 'object.c', line 1380 static VALUE rb_mod_ge(mod, arg) VALUE mod, arg; |
#alias_method(new_name, old_name) ⇒ self (private)
Makes new_name a new copy of the method old_name. This can be used to retain access to methods that are overridden.
module Mod
alias_method :orig_exit, :exit
def exit(code=0)
puts "Exiting with code #{code}"
orig_exit(code)
end
end
include Mod
exit(99)
produces:
Exiting with code 99
2198 2199 2200 |
# File 'eval.c', line 2198 static VALUE rb_mod_alias_method(mod, newname, oldname) VALUE mod, newname, oldname; |
#ancestors ⇒ Array
Returns a list of modules included in mod (including mod itself).
module Mod
include Math
include Comparable
end
Mod.ancestors #=> [Mod, Comparable, Math]
Math.ancestors #=> [Math]
500 501 502 |
# File 'class.c', line 500 VALUE rb_mod_ancestors(mod) VALUE mod; |
#append_features(mod) ⇒ Object (private)
When this module is included in another, Ruby calls append_features
in this module, passing it the receiving module in mod. Ruby’s default implementation is to add the constants, methods, and module variables of this module to mod if this module has not already been added to mod or one of its ancestors. See also Module#include
.
7547 7548 7549 |
# File 'eval.c', line 7547
static VALUE
rb_mod_append_features(module, include)
VALUE module, include;
|
#attr(symbol, writable = false) ⇒ nil (private)
Defines a named attribute for this module, where the name is symbol.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
is equivalent to:
module Mod
def size
@size
end
def size=(val)
@size = val
end
end
1697 1698 1699 |
# File 'object.c', line 1697 static VALUE rb_mod_attr(argc, argv, klass) int argc; |
#attr_accessor(symbol, ...) ⇒ nil (private)
Equivalent to calling “attr
symbol, true
” on each symbol in turn.
module Mod
attr_accessor(:one, :two)
end
Mod.instance_methods.sort #=> ["one", "one=", "two", "two="]
1768 1769 1770 |
# File 'object.c', line 1768 static VALUE rb_mod_attr_accessor(argc, argv, klass) int argc; |
#attr_reader(symbol, ...) ⇒ nil (private)
Creates instance variables and corresponding methods that return the value of each instance variable. Equivalent to calling “attr
:name” on each name in turn.
1719 1720 1721 |
# File 'object.c', line 1719 static VALUE rb_mod_attr_reader(argc, argv, klass) int argc; |
#attr_writer(symbol, ...) ⇒ nil (private)
Creates an accessor method to allow assignment to the attribute aSymbol.id2name
.
1741 1742 1743 |
# File 'object.c', line 1741 static VALUE rb_mod_attr_writer(argc, argv, klass) int argc; |
#autoload(name, filename) ⇒ nil
Registers filename to be loaded (using Kernel::require
) the first time that name (which may be a String
or a symbol) is accessed in the namespace of mod.
module A
end
A.autoload(:B, "b")
A::B.doit # autoloads "b"
8055 8056 8057 |
# File 'eval.c', line 8055 static VALUE rb_mod_autoload(mod, sym, file) VALUE mod; |
#autoload?(name) ⇒ String?
Returns filename to be loaded if name is registered as autoload
in the namespace of mod.
module A
end
A.autoload(:B, "b")
A.autoload?(:B) # => "b"
8081 8082 8083 |
# File 'eval.c', line 8081 static VALUE rb_mod_autoload_p(mod, sym) VALUE mod, sym; |
#class_eval(string[, filename [, lineno]]) ⇒ Object #module_eval { ... } ⇒ Object
Evaluates the string or block in the context of mod. This can be used to add methods to a class. module_eval
returns the result of evaluating its argument. The optional filename and lineno parameters set the text for error messages.
class Thing
end
a = %q{def hello() "Hello there!" end}
Thing.module_eval(a)
puts Thing.new.hello()
Thing.module_eval("invalid code", "dummy", 123)
produces:
Hello there!
dummy:123:in `module_eval': undefined local variable
or method `code' for Thing:Class
6770 6771 6772 |
# File 'eval.c', line 6770 VALUE rb_mod_module_eval(argc, argv, mod) int argc; |
#class_variable_defined?(symbol) ⇒ Boolean
Returns true
if the given class variable is defined in obj.
class Fred
@@foo = 99
end
Fred.class_variable_defined?(:@@foo) #=> true
Fred.class_variable_defined?(:@@bar) #=> false
2137 2138 2139 |
# File 'object.c', line 2137 static VALUE rb_mod_cvar_defined(obj, iv) VALUE obj, iv; |
#class_variable_get(symbol) ⇒ Object (private)
Returns the value of the given class variable (or throws a NameError
exception). The @@
part of the variable name should be included for regular class variables
class Fred
@@foo = 99
end
def Fred.foo
class_variable_get(:@@foo) #=> 99
end
2077 2078 2079 |
# File 'object.c', line 2077 static VALUE rb_mod_cvar_get(obj, iv) VALUE obj, iv; |
#class_variable_set(symbol, obj) ⇒ Object (private)
Sets the class variable names by symbol to object.
class Fred
@@foo = 99
def foo
@@foo
end
end
def Fred.foo
class_variable_set(:@@foo, 101) #=> 101
end
Fred.foo
Fred.new.foo #=> 101
2110 2111 2112 |
# File 'object.c', line 2110 static VALUE rb_mod_cvar_set(obj, iv, val) VALUE obj, iv, val; |
#class_variables ⇒ Array
Returns an array of the names of class variables in mod and the ancestors of mod.
class One
@@var1 = 1
end
class Two < One
@@var2 = 2
end
One.class_variables #=> ["@@var1"]
Two.class_variables #=> ["@@var2", "@@var1"]
1879 1880 1881 |
# File 'variable.c', line 1879 VALUE rb_mod_class_variables(obj) VALUE obj; |
#const_defined?(sym) ⇒ Boolean
Returns true
if a constant with the given name is defined by mod.
Math.const_defined? "PI" #=> true
1838 1839 1840 |
# File 'object.c', line 1838 static VALUE rb_mod_const_defined(mod, name) VALUE mod, name; |
#const_get(sym) ⇒ Object
Returns the value of the named constant in mod.
Math.const_get(:PI) #=> 3.14159265358979
1791 1792 1793 |
# File 'object.c', line 1791 static VALUE rb_mod_const_get(mod, name) VALUE mod, name; |
#const_missing(sym) ⇒ Object
Invoked when a reference is made to an undefined constant in
<i>mod</i>. It is passed a symbol for the undefined constant, and
returns a value to be used for that constant. The
following code is a (very bad) example: if reference is made to
an undefined constant, it attempts to load a file whose name is
the lowercase version of the constant (thus class <code>Fred</code> is
assumed to be in file <code>fred.rb</code>). If found, it returns the
value of the loaded class. It therefore implements a perverse
kind of autoload facility.
def Object.const_missing(name)
@looked_for ||= {}
str_name = name.to_s
raise "Class not found: #{name}" if @looked_for[str_name]
@looked_for[str_name] = 1
file = str_name.downcase
require file
klass = const_get(name)
return klass if klass
raise "Class not found: #{name}"
end
1251 1252 1253 |
# File 'variable.c', line 1251 VALUE rb_mod_const_missing(klass, name) VALUE klass, name; |
#const_set(sym, obj) ⇒ Object
1815 1816 1817 |
# File 'object.c', line 1815 static VALUE rb_mod_const_set(mod, name, value) VALUE mod, name, value; |
#constants ⇒ Array
Returns an array of the names of the constants accessible in mod. This includes the names of constants in any included modules (example at start of section).
1572 1573 1574 |
# File 'variable.c', line 1572 VALUE rb_mod_constants(mod) VALUE mod; |
#define_method ⇒ Object (private)
#extend_object(obj) ⇒ Object (private)
Extends the specified object by adding this module’s constants and methods (which are added as singleton methods). This is the callback method used by Object#extend
.
module Picky
def Picky.extend_object(o)
if String === o
puts "Can't add Picky to a String"
else
puts "Picky added to #{o.class}"
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
7632 7633 7634 |
# File 'eval.c', line 7632 static VALUE rb_mod_extend_object(mod, obj) VALUE mod, obj; |
#extended ⇒ Object (private)
Not documented
630 631 632 633 634 |
# File 'object.c', line 630
static VALUE
rb_obj_dummy()
{
return Qnil;
}
|
#freeze ⇒ Object
Prevents further modifications to mod.
1277 1278 1279 |
# File 'object.c', line 1277 static VALUE rb_mod_freeze(mod) VALUE mod; |
#include ⇒ self (private)
Invokes Module.append_features
on each parameter in turn.
7571 7572 7573 |
# File 'eval.c', line 7571
static VALUE
rb_mod_include(argc, argv, module)
int argc;
|
#include? ⇒ Boolean
Returns true
if module is included in mod or one of mod’s ancestors.
module A
end
class B
include A
end
class C < B
end
B.include?(A) #=> true
C.include?(A) #=> true
A.include?(A) #=> false
468 469 470 |
# File 'class.c', line 468 VALUE rb_mod_include_p(mod, mod2) VALUE mod; |
#included ⇒ Object (private)
Not documented
630 631 632 633 634 |
# File 'object.c', line 630
static VALUE
rb_obj_dummy()
{
return Qnil;
}
|
#included_modules ⇒ Array
Returns the list of modules included in mod.
module Mixin
end
module Outer
include Mixin
end
Mixin.included_modules #=> []
Outer.included_modules #=> [Mixin]
434 435 436 |
# File 'class.c', line 434 VALUE rb_mod_included_modules(mod) VALUE mod; |
#initialize_copy ⇒ Object
:nodoc:
62 63 64 |
# File 'class.c', line 62 VALUE rb_mod_init_copy(clone, orig) VALUE clone, orig; |
#instance_method(symbol) ⇒ Object
Returns an UnboundMethod
representing the given instance method in mod.
class Interpreter
def do_a() print "there, "; end
def do_d() print "Hello "; end
def do_e() print "!\n"; end
def do_v() print "Dave"; end
Dispatcher = {
?a => instance_method(:do_a),
?d => instance_method(:do_d),
?e => instance_method(:do_e),
?v => instance_method(:do_v)
}
def interpret(string)
string.each_byte {|b| Dispatcher[b].bind(self).call }
end
end
interpreter = Interpreter.new
interpreter.interpret('dave')
produces:
Hello there, Dave!
9135 9136 9137 |
# File 'eval.c', line 9135 static VALUE rb_mod_method(mod, vid) VALUE mod; |
#instance_methods(include_super = true) ⇒ Array
Returns an array containing the names of public instance methods in the receiver. For a module, these are the public methods; for a class, they are the instance (not singleton) methods. With no argument, or with an argument that is false
, 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(false) #=> ["method2"]
C.instance_methods(false) #=> ["method3"]
C.instance_methods(true).length #=> 43
660 661 662 |
# File 'class.c', line 660 VALUE rb_class_instance_methods(argc, argv, mod) int argc; |
#method_added ⇒ Object (private)
Not documented
630 631 632 633 634 |
# File 'object.c', line 630
static VALUE
rb_obj_dummy()
{
return Qnil;
}
|
#method_defined?(symbol) ⇒ Boolean
Returns true
if the named method is defined by mod (or its included modules and, if mod is a class, its ancestors). Public and protected methods are matched.
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
4250 4251 4252 |
# File 'eval.c', line 4250 static VALUE rb_mod_method_defined(mod, mid) VALUE mod, mid; |
#method_removed ⇒ Object (private)
Not documented
630 631 632 633 634 |
# File 'object.c', line 630
static VALUE
rb_obj_dummy()
{
return Qnil;
}
|
#method_undefined ⇒ Object (private)
Not documented
630 631 632 633 634 |
# File 'object.c', line 630
static VALUE
rb_obj_dummy()
{
return Qnil;
}
|
#class_eval(string[, filename [, lineno]]) ⇒ Object #module_eval { ... } ⇒ Object
Evaluates the string or block in the context of mod. This can be used to add methods to a class. module_eval
returns the result of evaluating its argument. The optional filename and lineno parameters set the text for error messages.
class Thing
end
a = %q{def hello() "Hello there!" end}
Thing.module_eval(a)
puts Thing.new.hello()
Thing.module_eval("invalid code", "dummy", 123)
produces:
Hello there!
dummy:123:in `module_eval': undefined local variable
or method `code' for Thing:Class
6770 6771 6772 |
# File 'eval.c', line 6770 VALUE rb_mod_module_eval(argc, argv, mod) int argc; |
#module_function(symbol, ...) ⇒ self (private)
Creates module functions for the named methods. These functions may be called with the module as a receiver, and also become available as instance methods to classes that mix in the module. Module functions are copies of the original, and so may be changed independently. The instance-method versions are made private. If used with no arguments, subsequently defined methods become module functions.
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"
7491 7492 7493 |
# File 'eval.c', line 7491
static VALUE
rb_mod_modfunc(argc, argv, module)
int argc;
|
#name ⇒ String
Returns the name of the module mod.
176 177 178 |
# File 'variable.c', line 176 VALUE rb_mod_name(mod) VALUE mod; |
#private ⇒ self (private) #private(symbol, ...) ⇒ self (private)
With no arguments, sets the default visibility for subsequently defined methods to private. With arguments, sets the named methods to have private visibility.
module Mod
def a() end
def b() end
private
def c() end
private :a
end
Mod.private_instance_methods #=> ["a", "c"]
7370 7371 7372 |
# File 'eval.c', line 7370
static VALUE
rb_mod_private(argc, argv, module)
int argc;
|
#private_class_method(symbol, ...) ⇒ Object
Makes existing class methods private. Often used to hide the default constructor new
.
class SimpleSingleton # Not thread safe
private_class_method :new
def SimpleSingleton.create(*args, &block)
@me = new(*args, &block) if ! @me
@me
end
end
7419 7420 7421 |
# File 'eval.c', line 7419 static VALUE rb_mod_private_method(argc, argv, obj) int argc; |
#private_instance_methods(include_super = true) ⇒ Array
Returns a list of the private instance methods defined in mod. If the optional parameter is not false
, 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"]
704 705 706 |
# File 'class.c', line 704 VALUE rb_class_private_instance_methods(argc, argv, mod) int argc; |
#private_method_defined?(symbol) ⇒ Boolean
Returns true
if the named private 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
private
def method2() end
end
class C < B
include A
def method3() end
end
A.method_defined? :method1 #=> true
C.private_method_defined? "method1" #=> false
C.private_method_defined? "method2" #=> true
C.method_defined? "method2" #=> false
4325 4326 4327 |
# File 'eval.c', line 4325 static VALUE rb_mod_private_method_defined(mod, mid) VALUE mod, mid; |
#protected ⇒ self (private) #protected(symbol, ...) ⇒ self (private)
With no arguments, sets the default visibility for subsequently defined methods to protected. With arguments, sets the named methods to have protected visibility.
7335 7336 7337 |
# File 'eval.c', line 7335
static VALUE
rb_mod_protected(argc, argv, module)
int argc;
|
#protected_instance_methods(include_super = true) ⇒ Array
Returns a list of the protected instance methods defined in mod. If the optional parameter is not false
, the methods of any ancestors are included.
678 679 680 |
# File 'class.c', line 678 VALUE rb_class_protected_instance_methods(argc, argv, mod) int argc; |
#protected_method_defined?(symbol) ⇒ Boolean
Returns true
if the named protected 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
protected
def method2() end
end
class C < B
include A
def method3() end
end
A.method_defined? :method1 #=> true
C.protected_method_defined? "method1" #=> false
C.protected_method_defined? "method2" #=> true
C.method_defined? "method2" #=> true
4365 4366 4367 |
# File 'eval.c', line 4365 static VALUE rb_mod_protected_method_defined(mod, mid) VALUE mod, mid; |
#public ⇒ self (private) #public(symbol, ...) ⇒ self (private)
With no arguments, sets the default visibility for subsequently defined methods to public. With arguments, sets the named methods to have public visibility.
7309 7310 7311 |
# File 'eval.c', line 7309
static VALUE
rb_mod_public(argc, argv, module)
int argc;
|
#public_class_method(symbol, ...) ⇒ Object
Makes a list of existing class methods public.
7393 7394 7395 |
# File 'eval.c', line 7393 static VALUE rb_mod_public_method(argc, argv, obj) int argc; |
#public_instance_methods(include_super = true) ⇒ Array
Returns a list of the public instance methods defined in mod. If the optional parameter is not false
, the methods of any ancestors are included.
722 723 724 |
# File 'class.c', line 722 VALUE rb_class_public_instance_methods(argc, argv, mod) int argc; |
#public_method_defined?(symbol) ⇒ Boolean
Returns true
if the named public 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
protected
def method2() end
end
class C < B
include A
def method3() end
end
A.method_defined? :method1 #=> true
C.public_method_defined? "method1" #=> true
C.public_method_defined? "method2" #=> false
C.method_defined? "method2" #=> true
4285 4286 4287 |
# File 'eval.c', line 4285 static VALUE rb_mod_public_method_defined(mod, mid) VALUE mod, mid; |
#remove_class_variable(sym) ⇒ Object (private)
Removes the definition of the sym, returning that constant’s value.
class Dummy
@@var = 99
puts @@var
remove_class_variable(:@@var)
puts(defined? @@var)
end
produces:
99
nil
1915 1916 1917 |
# File 'variable.c', line 1915 VALUE rb_mod_remove_cvar(mod, name) VALUE mod, name; |
#remove_const(sym) ⇒ Object (private)
Removes the definition of the given constant, returning that constant’s value. Predefined classes and singleton objects (such as true) cannot be removed.
1465 1466 1467 |
# File 'variable.c', line 1465 VALUE rb_mod_remove_const(mod, name) VALUE mod, name; |
#remove_method(symbol) ⇒ self (private)
Removes the method identified by symbol from the current class. For an example, see Module.undef_method
.
602 603 604 |
# File 'eval.c', line 602 static VALUE rb_mod_remove_method(argc, argv, mod) int argc; |
#to_s ⇒ String
Return a string representing this module or class. For basic classes and modules, this is the name. For singletons, we show information on the thing we’re attached to as well.
1245 1246 1247 |
# File 'object.c', line 1245 static VALUE rb_mod_to_s(klass) VALUE klass; |
#undef_method(symbol) ⇒ self (private)
Prevents the current class from responding to calls to the named method. Contrast this with 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
puts "In parent"
end
end
class Child < Parent
def hello
puts "In child"
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:0x401b3bb4> (NoMethodError)
2111 2112 2113 |
# File 'eval.c', line 2111 static VALUE rb_mod_undef_method(argc, argv, mod) int argc; |