Core extension library
Rake extensions to Module.
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 sym 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.class #=> Module
Mod.constants #=> [:CONST, :PI, :E]
Mod.instance_methods #=> [:meth]
- #
- A
- C
- F
- I
- M
- N
- P
- R
- T
- Y
In the first form, returns an array of the names of all constants accessible from the point of call. This list includes the names of all modules and classes defined in the global scope.
Module.constants.first(4)
# => [:ARGF, :ARGV, :ArgumentError, :Array]
Module.constants.include?(:SEEK_SET) # => false
class IO
Module.constants.include?(:SEEK_SET) # => true
end
The second form calls the instance method constants
.
Source: show
static VALUE rb_mod_s_constants(int argc, VALUE *argv, VALUE mod) { const NODE *cref = rb_vm_cref(); VALUE klass; VALUE cbase = 0; void *data = 0; if (argc > 0 || mod != rb_cModule) { return rb_mod_constants(argc, argv, mod); } while (cref) { klass = cref->nd_clss; if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) && !NIL_P(klass)) { data = rb_mod_const_at(cref->nd_clss, data); if (!cbase) { cbase = klass; } } cref = cref->nd_next; } if (cbase) { data = rb_mod_const_of(cbase, data); } return rb_const_list(data); }
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"
Source: show
static VALUE rb_mod_nesting(void) { VALUE ary = rb_ary_new(); const NODE *cref = rb_vm_cref(); while (cref && cref->nd_next) { VALUE klass = cref->nd_clss; if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) && !NIL_P(klass)) { rb_ary_push(ary, klass); } cref = cref->nd_next; } return ary; }
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"
Assign the module to a constant (name starting uppercase) if you want to treat it like a regular module.
Source: show
static VALUE rb_mod_initialize(VALUE module) { if (rb_block_given_p()) { rb_mod_module_exec(1, &module, module); } return Qnil; }
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”).
Source: show
static VALUE rb_mod_lt(VALUE mod, VALUE arg) { if (mod == arg) return Qfalse; return rb_class_inherited_p(mod, arg); }
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”).
Source: show
VALUE rb_class_inherited_p(VALUE mod, VALUE arg) { VALUE start = mod; if (mod == arg) return Qtrue; if (!CLASS_OR_MODULE_P(arg) && !RB_TYPE_P(arg, T_ICLASS)) { rb_raise(rb_eTypeError, "compared with non class/module"); } arg = RCLASS_ORIGIN(arg); while (mod) { if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg)) return Qtrue; mod = RCLASS_SUPER(mod); } /* not mod < arg; check if mod > arg */ while (arg) { if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start)) return Qfalse; arg = RCLASS_SUPER(arg); } return Qnil; }
Comparison—Returns -1, 0, +1 or nil depending on whether
module
includes other_module
, they are the same,
or if module
is included by other_module
. This is
the basis for the tests in Comparable.
Returns nil
if module
has no relationship with
other_module
, if other_module
is not a module, or
if the two values are incomparable.
Source: show
static VALUE rb_mod_cmp(VALUE mod, VALUE arg) { VALUE cmp; if (mod == arg) return INT2FIX(0); if (!CLASS_OR_MODULE_P(arg)) { return Qnil; } cmp = rb_class_inherited_p(mod, arg); if (NIL_P(cmp)) return Qnil; if (cmp) { return INT2FIX(-1); } return INT2FIX(1); }
Equality — At the Object
level, ==
returns
true
only if obj
and other
are the
same object. Typically, this method is overridden in descendant classes to
provide class-specific meaning.
Unlike ==
, the equal?
method should never be
overridden by subclasses as it is used to determine object identity (that
is, a.equal?(b)
if and only if a
is the same
object as b
):
obj = "a"
other = obj.dup
a == other #=> true
a.equal? other #=> false
a.equal? a #=> true
The eql?
method returns true
if obj
and other
refer to the same hash key. This is used by Hash to test members for equality. For objects of
class Object
, eql?
is synonymous with
==
. Subclasses normally continue this tradition by aliasing
eql?
to their overridden ==
method, 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
Source: show
VALUE rb_obj_equal(VALUE obj1, VALUE obj2) { if (obj1 == obj2) return Qtrue; return Qfalse; }
Case Equality—Returns true
if anObject is an instance
of mod or one of mod's descendants. Of limited use
for modules, but can be used in case
statements to classify
objects by class.
Source: show
static VALUE rb_mod_eqq(VALUE mod, VALUE arg) { return rb_obj_is_kind_of(arg, mod); }
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”).
Source: show
static VALUE rb_mod_gt(VALUE mod, VALUE arg) { if (mod == arg) return Qfalse; return rb_mod_ge(mod, arg); }
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”).
Source: show
static VALUE rb_mod_ge(VALUE mod, VALUE arg) { if (!CLASS_OR_MODULE_P(arg)) { rb_raise(rb_eTypeError, "compared with non class/module"); } return rb_class_inherited_p(arg, mod); }
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]
Source: show
VALUE rb_mod_ancestors(VALUE mod) { VALUE p, ary = rb_ary_new(); for (p = mod; p; p = RCLASS_SUPER(p)) { if (FL_TEST(p, FL_SINGLETON)) continue; if (BUILTIN_TYPE(p) == T_ICLASS) { rb_ary_push(ary, RBASIC(p)->klass); } else if (p == RCLASS_ORIGIN(p)) { rb_ary_push(ary, p); } } return ary; }
Registers filename to be loaded (using
Kernel::require
) the first time that module (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"
Source: show
static VALUE rb_mod_autoload(VALUE mod, VALUE sym, VALUE file) { ID id = rb_to_id(sym); FilePathValue(file); rb_autoload(mod, id, RSTRING_PTR(file)); return Qnil; }
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"
Source: show
static VALUE rb_mod_autoload_p(VALUE mod, VALUE sym) { ID id = rb_check_id(&sym); if (!id) { return Qnil; } return rb_autoload_p(mod, id); }
Evaluates the string or block in the context of mod, except that
when a block is given, constant/class variable lookup is not affected. 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
Source: show
VALUE rb_mod_module_eval(int argc, VALUE *argv, VALUE mod) { return specific_eval(argc, argv, mod, mod); }
Evaluates the given block in the context of the class/module. The method defined in the block will belong to the receiver.
class Thing
end
Thing.class_exec{
def hello() "Hello there!" end
}
puts Thing.new.hello()
produces:
Hello there!
Source: show
VALUE rb_mod_module_exec(int argc, VALUE *argv, VALUE mod) { return yield_under(mod, mod, rb_ary_new4(argc, argv)); }
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
Source: show
static VALUE rb_mod_cvar_defined(VALUE obj, VALUE iv) { ID id = rb_check_id(&iv); if (!id) { if (rb_is_class_name(iv)) { return Qfalse; } else { rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name", QUOTE(iv)); } } if (!rb_is_class_id(id)) { rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name", QUOTE_ID(id)); } return rb_cvar_defined(obj, id); }
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
Fred.class_variable_get(:@@foo) #=> 99
Source: show
static VALUE rb_mod_cvar_get(VALUE obj, VALUE iv) { ID id = rb_check_id(&iv); if (!id) { if (rb_is_class_name(iv)) { rb_name_error_str(iv, "uninitialized class variable %"PRIsVALUE" in %"PRIsVALUE"", iv, rb_class_name(obj)); } else { rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name", QUOTE(iv)); } } if (!rb_is_class_id(id)) { rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name", QUOTE_ID(id)); } return rb_cvar_get(obj, id); }
Sets the class variable names by symbol to object.
class Fred
@@foo = 99
def foo
@@foo
end
end
Fred.class_variable_set(:@@foo, 101) #=> 101
Fred.new.foo #=> 101
Source: show
static VALUE rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val) { ID id = rb_to_id(iv); if (!rb_is_class_id(id)) { rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name", QUOTE_ID(id)); } rb_cvar_set(obj, id, val); return val; }
Returns an array of the names of class variables in mod. This
includes the names of class variables in any included modules, unless the
inherit parameter is set to false
.
class One
@@var1 = 1
end
class Two < One
@@var2 = 2
end
One.class_variables #=> [:@@var1]
Two.class_variables #=> [:@@var2, :@@var1]
Two.class_variables(false) #=> [:@@var2]
Source: show
VALUE rb_mod_class_variables(int argc, VALUE *argv, VALUE mod) { VALUE inherit; st_table *tbl; if (argc == 0) { inherit = Qtrue; } else { rb_scan_args(argc, argv, "01", &inherit); } if (RTEST(inherit)) { tbl = mod_cvar_of(mod, 0); } else { tbl = mod_cvar_at(mod, 0); } return cvar_list(tbl); }
Checks for a constant with the given name in mod If
inherit
is set, the lookup will also search the ancestors (and
Object
if mod is a Module
.)
Returns whether or not a definition is found:
Math.const_defined? "PI" #=> true
IO.const_defined? :SYNC #=> true
IO.const_defined? :SYNC, false #=> false
Source: show
static VALUE rb_mod_const_defined(int argc, VALUE *argv, VALUE mod) { VALUE name, recur; ID id; if (argc == 1) { name = argv[0]; recur = Qtrue; } else { rb_scan_args(argc, argv, "11", &name, &recur); } if (!(id = rb_check_id(&name))) { if (rb_is_const_name(name)) { return Qfalse; } else { rb_name_error_str(name, "wrong constant name %"PRIsVALUE, QUOTE(name)); } } if (!rb_is_const_id(id)) { rb_name_error(id, "wrong constant name %"PRIsVALUE, QUOTE_ID(id)); } return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id); }
Checks for a constant with the given name in mod If
inherit
is set, the lookup will also search the ancestors (and
Object
if mod is a Module
.)
The value of the constant is returned if a definition is found, otherwise a
NameError
is raised.
Math.const_get(:PI) #=> 3.14159265358979
This method will recursively look up constant names if a namespaced class name is provided. For example:
module Foo; class Bar; end end
Object.const_get 'Foo::Bar'
The inherit
flag is respected on each lookup. For example:
module Foo
class Bar
VAL = 10
end
class Baz < Bar; end
end
Object.const_get 'Foo::Baz::VAL' # => 10
Object.const_get 'Foo::Baz::VAL', false # => NameError
Source: show
static VALUE rb_mod_const_get(int argc, VALUE *argv, VALUE mod) { VALUE name, recur; rb_encoding *enc; const char *pbeg, *p, *path, *pend; ID id; int nestable = 1; if (argc == 1) { name = argv[0]; recur = Qtrue; } else { rb_scan_args(argc, argv, "11", &name, &recur); } if (SYMBOL_P(name)) { name = rb_sym_to_s(name); nestable = 0; } name = rb_check_string_type(name); Check_Type(name, T_STRING); enc = rb_enc_get(name); path = RSTRING_PTR(name); if (!rb_enc_asciicompat(enc)) { rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)"); } pbeg = p = path; pend = path + RSTRING_LEN(name); if (p >= pend || !*p) { wrong_name: rb_raise(rb_eNameError, "wrong constant name %"PRIsVALUE, QUOTE(name)); } if (p + 2 < pend && p[0] == ':' && p[1] == ':') { if (!nestable) goto wrong_name; mod = rb_cObject; p += 2; pbeg = p; } while (p < pend) { VALUE part; long len, beglen; while (p < pend && *p != ':') p++; if (pbeg == p) goto wrong_name; id = rb_check_id_cstr(pbeg, len = p-pbeg, enc); beglen = pbeg-path; if (p < pend && p[0] == ':') { if (!nestable) goto wrong_name; if (p + 2 >= pend || p[1] != ':') goto wrong_name; p += 2; pbeg = p; } if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) { rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module", QUOTE(name)); } if (!id) { if (!ISUPPER(*pbeg) || !rb_enc_symname2_p(pbeg, len, enc)) { part = rb_str_subseq(name, beglen, len); rb_name_error_str(part, "wrong constant name %"PRIsVALUE, QUOTE(part)); } else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) { id = rb_intern3(pbeg, len, enc); } else { part = rb_str_subseq(name, beglen, len); rb_name_error_str(part, "uninitialized constant %"PRIsVALUE"%"PRIsVALUE, rb_str_subseq(name, 0, beglen), QUOTE(part)); } } if (!rb_is_const_id(id)) { rb_name_error(id, "wrong constant name %"PRIsVALUE, QUOTE_ID(id)); } mod = RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id); } return mod; }
Check for deprecated uses of top level (i.e. in Object) uses of Rake class names. If someone tries to reference the constant name, display a warning and return the proper object. Using the –classic-namespace command line option will define these constants in Object and avoid this handler.
# File lib/rake/ext/module.rb, line 21 def const_missing(const_name) case const_name when :Task Rake.application.const_warning(const_name) Rake::Task when :FileTask Rake.application.const_warning(const_name) Rake::FileTask when :FileCreationTask Rake.application.const_warning(const_name) Rake::FileCreationTask when :RakeApp Rake.application.const_warning(const_name) Rake::Application else rake_original_const_missing(const_name) end end
Sets the named constant to the given object, returning that object. Creates a new constant if no constant with the given name previously existed.
Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
Source: show
static VALUE rb_mod_const_set(VALUE mod, VALUE name, VALUE value) { ID id = rb_to_id(name); if (!rb_is_const_id(id)) { rb_name_error(id, "wrong constant name %"PRIsVALUE, QUOTE_ID(id)); } rb_const_set(mod, id, value); return value; }
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), unless the inherit parameter is set to
false
.
IO.constants.include?(:SYNC) #=> true
IO.constants(false).include?(:SYNC) #=> false
Also see Module::const_defined?
.
Source: show
VALUE rb_mod_constants(int argc, VALUE *argv, VALUE mod) { VALUE inherit; st_table *tbl; if (argc == 0) { inherit = Qtrue; } else { rb_scan_args(argc, argv, "01", &inherit); } if (RTEST(inherit)) { tbl = rb_mod_const_of(mod, 0); } else { tbl = rb_mod_const_at(mod, 0); } return rb_const_list(tbl); }
Prevents further modifications to mod.
This method returns self.
Source: show
static VALUE rb_mod_freeze(VALUE mod) { rb_class_name(mod); return rb_obj_freeze(mod); }
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
Source: show
VALUE rb_mod_include_p(VALUE mod, VALUE mod2) { VALUE p; Check_Type(mod2, T_MODULE); for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) { if (BUILTIN_TYPE(p) == T_ICLASS) { if (RBASIC(p)->klass == mod2) return Qtrue; } } return Qfalse; }
Returns the list of modules included in mod.
module Mixin
end
module Outer
include Mixin
end
Mixin.included_modules #=> []
Outer.included_modules #=> [Mixin]
Source: show
VALUE rb_mod_included_modules(VALUE mod) { VALUE ary = rb_ary_new(); VALUE p; VALUE origin = RCLASS_ORIGIN(mod); for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) { if (p != origin && BUILTIN_TYPE(p) == T_ICLASS) { VALUE m = RBASIC(p)->klass; if (RB_TYPE_P(m, T_MODULE)) rb_ary_push(ary, m); } } return ary; }
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_char {|b| Dispatcher[b].bind(self).call }
end
end
interpreter = Interpreter.new
interpreter.interpret('dave')
produces:
Hello there, Dave!
Source: show
static VALUE rb_mod_instance_method(VALUE mod, VALUE vid) { ID id = rb_check_id(&vid); if (!id) { rb_method_name_error(mod, vid); } return mnew(mod, Qundef, id, rb_cUnboundMethod, FALSE); }
Returns an array containing the names of the public and protected instance
methods in the receiver. For a module, these are the public and protected
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
Source: show
VALUE rb_class_instance_methods(int argc, VALUE *argv, VALUE mod) { return class_instance_method_list(argc, argv, mod, 0, ins_methods_i); }
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
Source: show
static VALUE rb_mod_method_defined(VALUE mod, VALUE mid) { ID id = rb_check_id(&mid); if (!id || !rb_method_boundp(mod, id, 1)) { return Qfalse; } return Qtrue; }
Evaluates the string or block in the context of mod, except that
when a block is given, constant/class variable lookup is not affected. 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
Source: show
VALUE rb_mod_module_eval(int argc, VALUE *argv, VALUE mod) { return specific_eval(argc, argv, mod, mod); }
Evaluates the given block in the context of the class/module. The method defined in the block will belong to the receiver.
class Thing
end
Thing.class_exec{
def hello() "Hello there!" end
}
puts Thing.new.hello()
produces:
Hello there!
Source: show
VALUE rb_mod_module_exec(int argc, VALUE *argv, VALUE mod) { return yield_under(mod, mod, rb_ary_new4(argc, argv)); }
Returns the name of the module mod. Returns nil for anonymous modules.
Source: show
VALUE rb_mod_name(VALUE mod) { int permanent; VALUE path = classname(mod, &permanent); if (!NIL_P(path)) return rb_str_dup(path); return path; }
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
Source: show
static VALUE rb_mod_private_method(int argc, VALUE *argv, VALUE obj) { set_method_visibility(rb_singleton_class(obj), argc, argv, NOEX_PRIVATE); return obj; }
Makes a list of existing constants private.
Source: show
VALUE rb_mod_private_constant(int argc, VALUE *argv, VALUE obj) { set_const_visibility(obj, argc, argv, CONST_PRIVATE); return obj; }
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]
Source: show
VALUE rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod) { return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i); }
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
Source: show
static VALUE rb_mod_private_method_defined(VALUE mod, VALUE mid) { return check_definition(mod, mid, NOEX_PRIVATE); }
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.
Source: show
VALUE rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod) { return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i); }
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
Source: show
static VALUE rb_mod_protected_method_defined(VALUE mod, VALUE mid) { return check_definition(mod, mid, NOEX_PROTECTED); }
Makes a list of existing class methods public.
Source: show
static VALUE rb_mod_public_method(int argc, VALUE *argv, VALUE obj) { set_method_visibility(rb_singleton_class(obj), argc, argv, NOEX_PUBLIC); return obj; }
Makes a list of existing constants public.
Source: show
VALUE rb_mod_public_constant(int argc, VALUE *argv, VALUE obj) { set_const_visibility(obj, argc, argv, CONST_PUBLIC); return obj; }
Similar to instance_method, searches public method only.
Source: show
static VALUE rb_mod_public_instance_method(VALUE mod, VALUE vid) { ID id = rb_check_id(&vid); if (!id) { rb_method_name_error(mod, vid); } return mnew(mod, Qundef, id, rb_cUnboundMethod, TRUE); }
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.
Source: show
VALUE rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod) { return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i); }
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
Source: show
static VALUE rb_mod_public_method_defined(VALUE mod, VALUE mid) { return check_definition(mod, mid, NOEX_PUBLIC); }
Check for an existing method in the current class before extending. IF the method already exists, then a warning is printed and the extension is not added. Otherwise the block is yielded and any definitions in the block will take effect.
Usage:
class String
rake_extension("xyz") do
def xyz
...
end
end
end
Rename the original handler to make it available.
Removes the definition of the sym, returning that constant's value.
class Dummy
@@var = 99
puts @@var
remove_class_variable(:@@var)
p(defined? @@var)
end
produces:
99
nil
Source: show
VALUE rb_mod_remove_cvar(VALUE mod, VALUE name) { const ID id = rb_check_id(&name); st_data_t val, n = id; if (!id) { if (rb_is_class_name(name)) { rb_name_error_str(name, "class variable %"PRIsVALUE" not defined for %"PRIsVALUE"", name, rb_class_name(mod)); } else { rb_name_error_str(name, "wrong class variable name %"PRIsVALUE"", QUOTE(name)); } } if (!rb_is_class_id(id)) { rb_name_error(id, "wrong class variable name %"PRIsVALUE"", QUOTE_ID(id)); } if (!OBJ_UNTRUSTED(mod) && rb_safe_level() >= 4) rb_raise(rb_eSecurityError, "Insecure: can't remove class variable"); rb_check_frozen(mod); if (RCLASS_IV_TBL(mod) && st_delete(RCLASS_IV_TBL(mod), &n, &val)) { return (VALUE)val; } if (rb_cvar_defined(mod, id)) { rb_name_error(id, "cannot remove %"PRIsVALUE" for %"PRIsVALUE"", QUOTE_ID(id), rb_class_name(mod)); } rb_name_error(id, "class variable %"PRIsVALUE" not defined for %"PRIsVALUE"", QUOTE_ID(id), rb_class_name(mod)); UNREACHABLE; }
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.
Source: show
static VALUE rb_mod_to_s(VALUE klass) { ID id_defined_at; VALUE refined_class, defined_at; if (FL_TEST(klass, FL_SINGLETON)) { VALUE s = rb_usascii_str_new2("#<Class:"); VALUE v = rb_iv_get(klass, "__attached__"); if (CLASS_OR_MODULE_P(v)) { rb_str_append(s, rb_inspect(v)); } else { rb_str_append(s, rb_any_to_s(v)); } rb_str_cat2(s, ">"); return s; } refined_class = rb_refinement_module_get_refined_class(klass); if (!NIL_P(refined_class)) { VALUE s = rb_usascii_str_new2("#<refinement:"); rb_str_concat(s, rb_inspect(refined_class)); rb_str_cat2(s, "@"); CONST_ID(id_defined_at, "__defined_at__"); defined_at = rb_attr_get(klass, id_defined_at); rb_str_concat(s, rb_inspect(defined_at)); rb_str_cat2(s, ">"); return s; } return rb_str_dup(rb_class_name(klass)); }