Class: Module

Inherits:
Object show all
Defined in:
class.c

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Object) new - (Object) new {|mod| ... }

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"

Overloads:

  • - (Object) new {|mod| ... }

    Yields:

    • (mod)


# File 'object.c'

/*
 *  call-seq:
 *    Module.new                  -> mod
 *    Module.new {|mod| block }   -> mod
 *
 *  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 <code>module_eval</code>.
 *
 *     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"
 */

static VALUE
rb_mod_initialize(VALUE module)
{
    extern VALUE rb_mod_module_exec(int argc, VALUE *argv, VALUE mod);

    if (rb_block_given_p()) {
    rb_mod_module_exec(1, &module, module);
    }
    return Qnil;
}

Class Method Details

+ (Array) constants

Returns an array of the names of all constants defined in the system. This list includes the names of all modules and classes.

   p Module.constants.sort[1..5]

produces:

   ["ARGV", "ArgumentError", "Array", "Bignum", "Binding"]

Returns:



# File 'eval.c'

/*
 *  call-seq:
 *     Module.constants   -> array
 *
 *  Returns an array of the names of all constants defined in the
 *  system. This list includes the names of all modules and classes.
 *
 *     p Module.constants.sort[1..5]
 *
 *  <em>produces:</em>
 *
 *     ["ARGV", "ArgumentError", "Array", "Bignum", "Binding"]
 */

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) {
    return rb_mod_constants(argc, argv, rb_cModule);
    }

    while (cref) {
    klass = cref->nd_clss;
    if (!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);
}

+ (Array) nesting

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"

Returns:



# File 'eval.c'

/*
 *  call-seq:
 *     Module.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"
 */

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;
}

Instance Method Details

- (true, ...) <(other)

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

Returns:

  • (true, false, nil)


# File 'object.c'

/*
 * call-seq:
 *   mod < other   ->  true, false, or nil
 *
 * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
 * <code>nil</code> if there's no relationship between the two.
 * (Think of the relationship in terms of the class definition:
 * "class A<B" implies "A<B").
 *
 */

static VALUE
rb_mod_lt(VALUE mod, VALUE arg)
{
    if (mod == arg) return Qfalse;
    return rb_class_inherited_p(mod, arg);
}

- (true, ...) <=(other)

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

Returns:

  • (true, false, nil)


# File 'object.c'

/*
 * call-seq:
 *   mod <= other   ->  true, false, or nil
 *
 * Returns true if <i>mod</i> is a subclass of <i>other</i> or
 * is the same as <i>other</i>. Returns
 * <code>nil</code> if there's no relationship between the two.
 * (Think of the relationship in terms of the class definition:
 * "class A<B" implies "A<B").
 *
 */

VALUE
rb_class_inherited_p(VALUE mod, VALUE arg)
{
    VALUE start = mod;

    if (mod == arg) return Qtrue;
    switch (TYPE(arg)) {
      case T_MODULE:
      case T_CLASS:
    break;
      default:
    rb_raise(rb_eTypeError, "compared with non class/module");
    }
    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;
}

- (-1, ...) <=>(other_mod)

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. Returns nil if mod has no relationship with other_mod or if other_mod is not a module.

Returns:

  • (-1, 0, +1, nil)


# File 'object.c'

/*
 *  call-seq:
 *     mod <=> other_mod   -> -1, 0, +1, or nil
 *
 *  Comparison---Returns -1 if <i>mod</i> includes <i>other_mod</i>, 0 if
 *  <i>mod</i> is the same as <i>other_mod</i>, and +1 if <i>mod</i> is
 *  included by <i>other_mod</i>. Returns <code>nil</code> if <i>mod</i>
 *  has no relationship with <i>other_mod</i> or if <i>other_mod</i> is
 *  not a module.
 */

static VALUE
rb_mod_cmp(VALUE mod, VALUE arg)
{
    VALUE cmp;

    if (mod == arg) return INT2FIX(0);
    switch (TYPE(arg)) {
      case T_MODULE:
      case T_CLASS:
    break;
      default:
    return Qnil;
    }

    cmp = rb_class_inherited_p(mod, arg);
    if (NIL_P(cmp)) return Qnil;
    if (cmp) {
    return INT2FIX(-1);
    }
    return INT2FIX(1);
}

- (Boolean) ==(other) - (Boolean) equal?(other) - (Boolean) eql?(other)

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: 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 obj and anObject 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

Overloads:

  • - (Boolean) ==(other)

    Returns:

    • (Boolean)
  • - (Boolean) equal?(other)

    Returns:

    • (Boolean)
  • - (Boolean) eql?(other)

    Returns:

    • (Boolean)


# File 'object.c'

/*
 *  call-seq:
 *     obj == other        -> true or false
 *     obj.equal?(other)   -> true or false
 *     obj.eql?(other)     -> true or false
 *
 *  Equality---At the <code>Object</code> level, <code>==</code> returns
 *  <code>true</code> only if <i>obj</i> and <i>other</i> are the
 *  same object. Typically, this method is overridden in descendant
 *  classes to provide class-specific meaning.
 *
 *  Unlike <code>==</code>, the <code>equal?</code> method should never be
 *  overridden by subclasses: it is used to determine object identity
 *  (that is, <code>a.equal?(b)</code> iff <code>a</code> is the same
 *  object as <code>b</code>).
 *
 *  The <code>eql?</code> method returns <code>true</code> if
 *  <i>obj</i> and <i>anObject</i> have the same value. Used by
 *  <code>Hash</code> to test members for equality.  For objects of
 *  class <code>Object</code>, <code>eql?</code> is synonymous with
 *  <code>==</code>. Subclasses normally continue this tradition, but
 *  there are exceptions. <code>Numeric</code> types, for example,
 *  perform type conversion across <code>==</code>, but not across
 *  <code>eql?</code>, so:
 *
 *     1 == 1.0     #=> true
 *     1.eql? 1.0   #=> false
 */

VALUE
rb_obj_equal(VALUE obj1, VALUE obj2)
{
    if (obj1 == obj2) return Qtrue;
    return Qfalse;
}

- (Boolean) ===(obj)

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.

Returns:

  • (Boolean)


# File 'object.c'

/*
 *  call-seq:
 *     mod === obj    -> true or false
 *
 *  Case Equality---Returns <code>true</code> if <i>anObject</i> is an
 *  instance of <i>mod</i> or one of <i>mod</i>'s descendants. Of
 *  limited use for modules, but can be used in <code>case</code>
 *  statements to classify objects by class.
 */

static VALUE
rb_mod_eqq(VALUE mod, VALUE arg)
{
    return rb_obj_is_kind_of(arg, mod);
}

- (true, ...) >(other)

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 AA”).

Returns:

  • (true, false, nil)


# File 'object.c'

/*
 * call-seq:
 *   mod > other   ->  true, false, or nil
 *
 * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
 * <code>nil</code> if there's no relationship between the two.
 * (Think of the relationship in terms of the class definition:
 * "class A<B" implies "B>A").
 *
 */

static VALUE
rb_mod_gt(VALUE mod, VALUE arg)
{
    if (mod == arg) return Qfalse;
    return rb_mod_ge(mod, arg);
}

- (true, ...) >=(other)

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 AA”).

Returns:

  • (true, false, nil)


# File 'object.c'

/*
 * call-seq:
 *   mod >= other   ->  true, false, or nil
 *
 * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
 * two modules are the same. Returns
 * <code>nil</code> if there's no relationship between the two.
 * (Think of the relationship in terms of the class definition:
 * "class A<B" implies "B>A").
 *
 */

static VALUE
rb_mod_ge(VALUE mod, VALUE arg)
{
    switch (TYPE(arg)) {
      case T_MODULE:
      case T_CLASS:
    break;
      default:
    rb_raise(rb_eTypeError, "compared with non class/module");
    }

    return rb_class_inherited_p(arg, mod);
}

- (Module) alias_method(new_name, old_name)

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

Returns:



# File 'vm_method.c'

/*
 *  call-seq:
 *     alias_method(new_name, old_name)   -> self
 *
 *  Makes <i>new_name</i> a new copy of the method <i>old_name</i>. 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)
 *
 *  <em>produces:</em>
 *
 *     Exiting with code 99
 */

static VALUE
rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
{
    rb_alias(mod, rb_to_id(newname), rb_to_id(oldname));
    return mod;
}

- (Object) ancestors

- (Object) append_features(mod)

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.



# File 'eval.c'

/*
 *  call-seq:
 *     append_features(mod)   -> mod
 *
 *  When this module is included in another, Ruby calls
 *  <code>append_features</code> 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 <code>Module#include</code>.
 */

static VALUE
rb_mod_append_features(VALUE module, VALUE include)
{
    switch (TYPE(include)) {
      case T_CLASS:
      case T_MODULE:
    break;
      default:
    Check_Type(include, T_CLASS);
    break;
    }
    rb_include_module(include, module);

    return module;
}

- (Object) attr

- (nil) attr_accessor(symbol, ...)

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. Also creates a method called name= to set the attribute.

   module Mod
     attr_accessor(:one, :two)
   end
   Mod.instance_methods.sort   #=> [:one, :one=, :two, :two=]

Returns:

  • (nil)


# File 'object.c'

/*
 *  call-seq:
 *     attr_accessor(symbol, ...)    -> nil
 *
 *  Defines a named attribute for this module, where the name is
 *  <i>symbol.</i><code>id2name</code>, creating an instance variable
 *  (<code>@name</code>) and a corresponding access method to read it.
 *  Also creates a method called <code>name=</code> to set the attribute.
 *
 *     module Mod
 *       attr_accessor(:one, :two)
 *     end
 *     Mod.instance_methods.sort   #=> [:one, :one=, :two, :two=]
 */

static VALUE
rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
{
    int i;

    for (i=0; i<argc; i++) {
    rb_attr(klass, rb_to_id(argv[i]), TRUE, TRUE, TRUE);
    }
    return Qnil;
}

- (nil) attr_reader(symbol, ...) - (nil) attr(symbol, ...)

Creates instance variables and corresponding methods that return the value of each instance variable. Equivalent to calling ``attr:name’’ on each name in turn.

Overloads:

  • - (nil) attr_reader(symbol, ...)

    Returns:

    • (nil)
  • - (nil) attr(symbol, ...)

    Returns:

    • (nil)


# File 'object.c'

/*
 *  call-seq:
 *     attr_reader(symbol, ...)    -> nil
 *     attr(symbol, ...)             -> nil
 *
 *  Creates instance variables and corresponding methods that return the
 *  value of each instance variable. Equivalent to calling
 *  ``<code>attr</code><i>:name</i>'' on each name in turn.
 */

static VALUE
rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
{
    int i;

    for (i=0; i<argc; i++) {
    rb_attr(klass, rb_to_id(argv[i]), TRUE, FALSE, TRUE);
    }
    return Qnil;
}

- (nil) attr_writer(symbol, ...)

Creates an accessor method to allow assignment to the attribute aSymbol.id2name.

Returns:

  • (nil)


# File 'object.c'

/*
 *  call-seq:
 *      attr_writer(symbol, ...)    -> nil
 *
 *  Creates an accessor method to allow assignment to the attribute
 *  <i>aSymbol</i><code>.id2name</code>.
 */

static VALUE
rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
{
    int i;

    for (i=0; i<argc; i++) {
    rb_attr(klass, rb_to_id(argv[i]), FALSE, TRUE, TRUE);
    }
    return Qnil;
}

- (nil) autoload

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"

Returns:

  • (nil)


# File 'load.c'

/*
 *  call-seq:
 *     mod.autoload(module, filename)   -> nil
 *
 *  Registers _filename_ to be loaded (using <code>Kernel::require</code>)
 *  the first time that _module_ (which may be a <code>String</code> or
 *  a symbol) is accessed in the namespace of _mod_.
 *
 *     module A
 *     end
 *     A.autoload(:B, "b")
 *     A::B.doit            # autoloads "b"
 */

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;
}

- (nil) autoload?(name)

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"

Returns:

  • (nil)


# File 'load.c'

/*
 *  call-seq:
 *     mod.autoload?(name)   -> String or nil
 *
 *  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"
 */

static VALUE
rb_mod_autoload_p(VALUE mod, VALUE sym)
{
    return rb_autoload_p(mod, rb_to_id(sym));
}

- (Object) class_eval(string[, filename [, lineno]]) - (Object) module_eval { ... }

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

Overloads:

  • - (Object) class_eval(string[, filename [, lineno]])

    Returns:

  • - (Object) module_eval { ... }

    Yields:

    • []

    Returns:



# File 'vm_eval.c'

/*
 *  call-seq:
 *     mod.class_eval(string [, filename [, lineno]])  -> obj
 *     mod.module_eval {|| block }                     -> obj
 *
 *  Evaluates the string or block in the context of _mod_. This can
 *  be used to add methods to a class. <code>module_eval</code> 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)
 *
 *  <em>produces:</em>
 *
 *     Hello there!
 *     dummy:123:in `module_eval': undefined local variable
 *         or method `code' for Thing:Class
 */

VALUE
rb_mod_module_eval(int argc, VALUE *argv, VALUE mod)
{
    return specific_eval(argc, argv, mod, mod);
}

- (Object) module_exec(arg...) {|var...| ... } - (Object) class_exec(arg...) {|var...| ... }

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!

Overloads:

  • - (Object) module_exec(arg...) {|var...| ... }

    Yields:

    • (var...)

    Returns:

  • - (Object) class_exec(arg...) {|var...| ... }

    Yields:

    • (var...)

    Returns:



# File 'vm_eval.c'

/*
 *  call-seq:
 *     mod.module_exec(arg...) {|var...| block }       -> obj
 *     mod.class_exec(arg...) {|var...| block }        -> obj
 *
 *  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()
 *
 *  <em>produces:</em>
 *
 *     Hello there!
 */

VALUE
rb_mod_module_exec(int argc, VALUE *argv, VALUE mod)
{
    return yield_under(mod, mod, rb_ary_new4(argc, argv));
}

- (Boolean) class_variable_defined?(symbol)

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

Returns:

  • (Boolean)


# File 'object.c'

/*
 *  call-seq:
 *     obj.class_variable_defined?(symbol)    -> true or false
 *
 *  Returns <code>true</code> if the given class variable is defined
 *  in <i>obj</i>.
 *
 *     class Fred
 *       @@foo = 99
 *     end
 *     Fred.class_variable_defined?(:@@foo)    #=> true
 *     Fred.class_variable_defined?(:@@bar)    #=> false
 */

static VALUE
rb_mod_cvar_defined(VALUE obj, VALUE iv)
{
    ID id = rb_to_id(iv);

    if (!rb_is_class_id(id)) {
    rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
    }
    return rb_cvar_defined(obj, id);
}

- (Object) class_variable_get(symbol)

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

Returns:



# File 'object.c'

/*
 *  call-seq:
 *     mod.class_variable_get(symbol)    -> obj
 *
 *  Returns the value of the given class variable (or throws a
 *  <code>NameError</code> exception). The <code>@@</code> part of the
 *  variable name should be included for regular class variables
 *
 *     class Fred
 *       @@foo = 99
 *     end
 *     Fred.class_variable_get(:@@foo)     #=> 99
 */

static VALUE
rb_mod_cvar_get(VALUE obj, VALUE iv)
{
    ID id = rb_to_id(iv);

    if (!rb_is_class_id(id)) {
    rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
    }
    return rb_cvar_get(obj, id);
}

- (Object) class_variable_set(symbol, obj)

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

Returns:



# File 'object.c'

/*
 *  call-seq:
 *     obj.class_variable_set(symbol, obj)    -> obj
 *
 *  Sets the class variable names by <i>symbol</i> to
 *  <i>object</i>.
 *
 *     class Fred
 *       @@foo = 99
 *       def foo
 *         @@foo
 *       end
 *     end
 *     Fred.class_variable_set(:@@foo, 101)     #=> 101
 *     Fred.new.foo                             #=> 101
 */

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, "`%s' is not allowed as a class variable name", rb_id2name(id));
    }
    rb_cvar_set(obj, id, val);
    return val;
}

- (Object) class_variables

- (Boolean) const_defined?(sym, inherit = true)

Returns true if a constant with the given name is defined by mod, or its ancestors if inherit is not false.

   Math.const_defined? "PI"   #=> true
   IO.const_defined? "SYNC"   #=> true
   IO.const_defined? "SYNC", false   #=> false

Returns:

  • (Boolean)


# File 'object.c'

/*
 *  call-seq:
 *     mod.const_defined?(sym, inherit=true)   -> true or false
 *
 *  Returns <code>true</code> if a constant with the given name is
 *  defined by <i>mod</i>, or its ancestors if +inherit+ is not false.
 *
 *     Math.const_defined? "PI"   #=> true
 *     IO.const_defined? "SYNC"   #=> true
 *     IO.const_defined? "SYNC", false   #=> false
 */

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);
    }
    id = rb_to_id(name);
    if (!rb_is_const_id(id)) {
    rb_name_error(id, "wrong constant name %s", rb_id2name(id));
    }
    return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
}

- (Object) const_get(sym, inherit = true)

Returns the value of the named constant in mod.

   Math.const_get(:PI)   #=> 3.14159265358979

If the constant is not defined or is defined by the ancestors and inherit is false, NameError will be raised.

Returns:



# File 'object.c'

/*
 *  call-seq:
 *     mod.const_get(sym, inherit=true)    -> obj
 *
 *  Returns the value of the named constant in <i>mod</i>.
 *
 *     Math.const_get(:PI)   #=> 3.14159265358979
 *
 *  If the constant is not defined or is defined by the ancestors and
 *  +inherit+ is false, +NameError+ will be raised.
 */

static VALUE
rb_mod_const_get(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);
    }
    id = rb_to_id(name);
    if (!rb_is_const_id(id)) {
    rb_name_error(id, "wrong constant name %s", rb_id2name(id));
    }
    return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
}

- (Object) const_missing

- (Object) const_set(sym, obj)

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

Returns:



# File 'object.c'

/*
 *  call-seq:
 *     mod.const_set(sym, obj)    -> obj
 *
 *  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
 */

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 %s", rb_id2name(id));
    }
    rb_const_set(mod, id, value);
    return value;
}

- (Object) constants

- (Object) define_method(symbol, method) - (Proc) define_method(symbol) { ... }

Defines an instance method in the receiver. The method parameter can be a Proc, a Method or an UnboundMethod object. If a block is specified, it is used as the method body. This block is evaluated using instance_eval, a point that is tricky to demonstrate because define_method is private. (This is why we resort to the send hack in this example.)

   class A
     def fred
       puts "In Fred"
     end
     def create_method(name, &block)
       self.class.send(:define_method, name, &block)
     end
     define_method(:wilma) { puts "Charge it!" }
   end
   class B < A
     define_method(:barney, instance_method(:fred))
   end
   a = B.new
   a.barney
   a.wilma
   a.create_method(:betty) { p self }
   a.betty

produces:

   In Fred
   Charge it!
   #<B:0x401b39e8>

Overloads:

  • - (Proc) define_method(symbol) { ... }

    Yields:

    • []

    Returns:



# File 'proc.c'

/*
 *  call-seq:
 *     define_method(symbol, method)     -> new_method
 *     define_method(symbol) { block }   -> proc
 *
 *  Defines an instance method in the receiver. The _method_
 *  parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
 *  If a block is specified, it is used as the method body. This block
 *  is evaluated using <code>instance_eval</code>, a point that is
 *  tricky to demonstrate because <code>define_method</code> is private.
 *  (This is why we resort to the +send+ hack in this example.)
 *
 *     class A
 *       def fred
 *         puts "In Fred"
 *       end
 *       def create_method(name, &block)
 *         self.class.send(:define_method, name, &block)
 *       end
 *       define_method(:wilma) { puts "Charge it!" }
 *     end
 *     class B < A
 *       define_method(:barney, instance_method(:fred))
 *     end
 *     a = B.new
 *     a.barney
 *     a.wilma
 *     a.create_method(:betty) { p self }
 *     a.betty
 *
 *  <em>produces:</em>
 *
 *     In Fred
 *     Charge it!
 *     #<B:0x401b39e8>
 */

static VALUE
rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
{
    ID id;
    VALUE body;
    int noex = NOEX_PUBLIC;

    if (argc == 1) {
    id = rb_to_id(argv[0]);
    body = rb_block_lambda();
    }
    else if (argc == 2) {
    id = rb_to_id(argv[0]);
    body = argv[1];
    if (!rb_obj_is_method(body) && !rb_obj_is_proc(body)) {
        rb_raise(rb_eTypeError,
             "wrong argument type %s (expected Proc/Method)",
             rb_obj_classname(body));
    }
    }
    else {
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
    }

    if (rb_obj_is_method(body)) {
    struct METHOD *method = (struct METHOD *)DATA_PTR(body);
    VALUE rclass = method->rclass;
    if (rclass != mod && !RTEST(rb_class_inherited_p(mod, rclass))) {
        if (FL_TEST(rclass, FL_SINGLETON)) {
        rb_raise(rb_eTypeError,
             "can't bind singleton method to a different class");
        }
        else {
        rb_raise(rb_eTypeError,
             "bind argument must be a subclass of %s",
             rb_class2name(rclass));
        }
    }
    rb_method_entry_set(mod, id, &method->me, noex);
    }
    else if (rb_obj_is_proc(body)) {
    rb_proc_t *proc;
    body = proc_dup(body);
    GetProcPtr(body, proc);
    if (BUILTIN_TYPE(proc->block.iseq) != T_NODE) {
        proc->block.iseq->defined_method_id = id;
        proc->block.iseq->klass = mod;
        proc->is_lambda = TRUE;
        proc->is_from_method = TRUE;
    }
    rb_add_method(mod, id, VM_METHOD_TYPE_BMETHOD, (void *)body, noex);
    }
    else {
    /* type error */
    rb_raise(rb_eTypeError, "wrong argument type (expected Proc/Method)");
    }

    return body;
}

- (Object) extend_object(obj)

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

Returns:



# File 'eval.c'

/*
 *  call-seq:
 *     extend_object(obj)    -> obj
 *
 *  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 <code>Object#extend</code>.
 *
 *     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
 *
 *  <em>produces:</em>
 *
 *     Picky added to Array
 *     Can't add Picky to a String
 */

static VALUE
rb_mod_extend_object(VALUE mod, VALUE obj)
{
    rb_extend_object(obj, mod);
    return obj;
}

- (Object) extended

Not documented



# File 'object.c'

/*
 * Not documented
 */

static VALUE
rb_obj_dummy(void)
{
    return Qnil;
}

- (Object) freeze

Prevents further modifications to mod.

This method returns self.



# File 'object.c'

/*
 *  call-seq:
 *     mod.freeze       -> mod
 *
 *  Prevents further modifications to <i>mod</i>.
 *
 *  This method returns self.
 */

static VALUE
rb_mod_freeze(VALUE mod)
{
    rb_class_name(mod);
    return rb_obj_freeze(mod);
}

- (Module) include

Invokes Module.append_features on each parameter in reverse order.

Returns:



# File 'eval.c'

/*
 *  call-seq:
 *     include(module, ...)    -> self
 *
 *  Invokes <code>Module.append_features</code> on each parameter in reverse order.
 */

static VALUE
rb_mod_include(int argc, VALUE *argv, VALUE module)
{
    int i;

    for (i = 0; i < argc; i++)
    Check_Type(argv[i], T_MODULE);
    while (argc--) {
    rb_funcall(argv[argc], rb_intern("append_features"), 1, module);
    rb_funcall(argv[argc], rb_intern("included"), 1, module);
    }
    return module;
}

- (Boolean) include?

Returns:

  • (Boolean)

- (Object) included

Not documented



# File 'object.c'

/*
 * Not documented
 */

static VALUE
rb_obj_dummy(void)
{
    return Qnil;
}

- (Object) included_modules

- (Object) initialize_copy

- (Object) instance_method(symbol)

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!


# File 'proc.c'

/*
 *  call-seq:
 *     mod.instance_method(symbol)   -> unbound_method
 *
 *  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')
 *
 *  <em>produces:</em>
 *
 *     Hello there, Dave!
 */

static VALUE
rb_mod_instance_method(VALUE mod, VALUE vid)
{
    return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod, FALSE);
}

- (Object) instance_methods

- (Object) method_added

Not documented



# File 'object.c'

/*
 * Not documented
 */

static VALUE
rb_obj_dummy(void)
{
    return Qnil;
}

- (Boolean) method_defined?(symbol)

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

Returns:

  • (Boolean)


# File 'vm_method.c'

/*
 *  call-seq:
 *     mod.method_defined?(symbol)    -> true or false
 *
 *  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
 */

static VALUE
rb_mod_method_defined(VALUE mod, VALUE mid)
{
    if (!rb_method_boundp(mod, rb_to_id(mid), 1)) {
    return Qfalse;
    }
    return Qtrue;

}

- (Object) method_removed

Not documented



# File 'object.c'

/*
 * Not documented
 */

static VALUE
rb_obj_dummy(void)
{
    return Qnil;
}

- (Object) method_undefined

Not documented



# File 'object.c'

/*
 * Not documented
 */

static VALUE
rb_obj_dummy(void)
{
    return Qnil;
}

- (Object) class_eval(string[, filename [, lineno]]) - (Object) module_eval { ... }

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

Overloads:

  • - (Object) class_eval(string[, filename [, lineno]])

    Returns:

  • - (Object) module_eval { ... }

    Yields:

    • []

    Returns:



# File 'vm_eval.c'

/*
 *  call-seq:
 *     mod.class_eval(string [, filename [, lineno]])  -> obj
 *     mod.module_eval {|| block }                     -> obj
 *
 *  Evaluates the string or block in the context of _mod_. This can
 *  be used to add methods to a class. <code>module_eval</code> 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)
 *
 *  <em>produces:</em>
 *
 *     Hello there!
 *     dummy:123:in `module_eval': undefined local variable
 *         or method `code' for Thing:Class
 */

VALUE
rb_mod_module_eval(int argc, VALUE *argv, VALUE mod)
{
    return specific_eval(argc, argv, mod, mod);
}

- (Object) module_exec(arg...) {|var...| ... } - (Object) class_exec(arg...) {|var...| ... }

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!

Overloads:

  • - (Object) module_exec(arg...) {|var...| ... }

    Yields:

    • (var...)

    Returns:

  • - (Object) class_exec(arg...) {|var...| ... }

    Yields:

    • (var...)

    Returns:



# File 'vm_eval.c'

/*
 *  call-seq:
 *     mod.module_exec(arg...) {|var...| block }       -> obj
 *     mod.class_exec(arg...) {|var...| block }        -> obj
 *
 *  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()
 *
 *  <em>produces:</em>
 *
 *     Hello there!
 */

VALUE
rb_mod_module_exec(int argc, VALUE *argv, VALUE mod)
{
    return yield_under(mod, mod, rb_ary_new4(argc, argv));
}

- (Module) module_function(symbol, ...)

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"

Returns:



# File 'vm_method.c'

/*
 *  call-seq:
 *     module_function(symbol, ...)    -> self
 *
 *  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"
 */

static VALUE
rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
{
    int i;
    ID id;
    const rb_method_entry_t *me;

    if (TYPE(module) != T_MODULE) {
    rb_raise(rb_eTypeError, "module_function must be called for modules");
    }

    secure_visibility(module);
    if (argc == 0) {
    SCOPE_SET(NOEX_MODFUNC);
    return module;
    }

    set_method_visibility(module, argc, argv, NOEX_PRIVATE);

    for (i = 0; i < argc; i++) {
    VALUE m = module;

    id = rb_to_id(argv[i]);
    for (;;) {
        me = search_method(m, id);
        if (me == 0) {
        me = search_method(rb_cObject, id);
        }
        if (UNDEFINED_METHOD_ENTRY_P(me)) {
        rb_print_undef(module, id, 0);
        }
        if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
        break; /* normal case: need not to follow 'super' link */
        }
        m = RCLASS_SUPER(m);
        if (!m)
        break;
    }
    rb_method_entry_set(rb_singleton_class(module), id, me, NOEX_PUBLIC);
    }
    return module;
}

- (Object) name

- (Module) private - (Module) private(symbol, ...)

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]

Overloads:



# File 'vm_method.c'

/*
 *  call-seq:
 *     private                 -> self
 *     private(symbol, ...)    -> self
 *
 *  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]
 */

static VALUE
rb_mod_private(int argc, VALUE *argv, VALUE module)
{
    secure_visibility(module);
    if (argc == 0) {
    SCOPE_SET(NOEX_PRIVATE);
    }
    else {
    set_method_visibility(module, argc, argv, NOEX_PRIVATE);
    }
    return module;
}

- (Object) private_class_method(symbol, ...)

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


# File 'vm_method.c'

/*
 *  call-seq:
 *     mod.private_class_method(symbol, ...)   -> mod
 *
 *  Makes existing class methods private. Often used to hide the default
 *  constructor <code>new</code>.
 *
 *     class SimpleSingleton  # Not thread safe
 *       private_class_method :new
 *       def SimpleSingleton.create(*args, &block)
 *         @me = new(*args, &block) if ! @me
 *         @me
 *       end
 *     end
 */

static VALUE
rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
{
    set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PRIVATE);
    return obj;
}

- (Object) private_instance_methods

- (Boolean) private_method_defined?(symbol)

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

Returns:

  • (Boolean)


# File 'vm_method.c'

/*
 *  call-seq:
 *     mod.private_method_defined?(symbol)    -> true or false
 *
 *  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
 */

static VALUE
rb_mod_private_method_defined(VALUE mod, VALUE mid)
{
    return check_definition(mod, rb_to_id(mid), NOEX_PRIVATE);
}

- (Module) protected - (Module) protected(symbol, ...)

With no arguments, sets the default visibility for subsequently defined methods to protected. With arguments, sets the named methods to have protected visibility.

Overloads:



# File 'vm_method.c'

/*
 *  call-seq:
 *     protected                -> self
 *     protected(symbol, ...)   -> self
 *
 *  With no arguments, sets the default visibility for subsequently
 *  defined methods to protected. With arguments, sets the named methods
 *  to have protected visibility.
 */

static VALUE
rb_mod_protected(int argc, VALUE *argv, VALUE module)
{
    secure_visibility(module);
    if (argc == 0) {
    SCOPE_SET(NOEX_PROTECTED);
    }
    else {
    set_method_visibility(module, argc, argv, NOEX_PROTECTED);
    }
    return module;
}

- (Object) protected_instance_methods

- (Boolean) protected_method_defined?(symbol)

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

Returns:

  • (Boolean)


# File 'vm_method.c'

/*
 *  call-seq:
 *     mod.protected_method_defined?(symbol)   -> true or false
 *
 *  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
 */

static VALUE
rb_mod_protected_method_defined(VALUE mod, VALUE mid)
{
    return check_definition(mod, rb_to_id(mid), NOEX_PROTECTED);
}

- (Module) public - (Module) public(symbol, ...)

With no arguments, sets the default visibility for subsequently defined methods to public. With arguments, sets the named methods to have public visibility.

Overloads:



# File 'vm_method.c'

/*
 *  call-seq:
 *     public                 -> self
 *     public(symbol, ...)    -> self
 *
 *  With no arguments, sets the default visibility for subsequently
 *  defined methods to public. With arguments, sets the named methods to
 *  have public visibility.
 */

static VALUE
rb_mod_public(int argc, VALUE *argv, VALUE module)
{
    secure_visibility(module);
    if (argc == 0) {
    SCOPE_SET(NOEX_PUBLIC);
    }
    else {
    set_method_visibility(module, argc, argv, NOEX_PUBLIC);
    }
    return module;
}

- (Object) public_class_method(symbol, ...)

Makes a list of existing class methods public.



# File 'vm_method.c'

/*
 *  call-seq:
 *     mod.public_class_method(symbol, ...)    -> mod
 *
 *  Makes a list of existing class methods public.
 */

static VALUE
rb_mod_public_method(int argc, VALUE *argv, VALUE obj)
{
    set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PUBLIC);
    return obj;
}

- (Object) public_instance_method(symbol)

Similar to instance_method, searches public method only.



# File 'proc.c'

/*
 *  call-seq:
 *     mod.public_instance_method(symbol)   -> unbound_method
 *
 *  Similar to _instance_method_, searches public method only.
 */

static VALUE
rb_mod_public_instance_method(VALUE mod, VALUE vid)
{
    return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod, TRUE);
}

- (Object) public_instance_methods

- (Boolean) public_method_defined?(symbol)

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

Returns:

  • (Boolean)


# File 'vm_method.c'

/*
 *  call-seq:
 *     mod.public_method_defined?(symbol)   -> true or false
 *
 *  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
 */

static VALUE
rb_mod_public_method_defined(VALUE mod, VALUE mid)
{
    return check_definition(mod, rb_to_id(mid), NOEX_PUBLIC);
}

- (Object) remove_class_variable

- (Object) remove_const

- (Module) remove_method(symbol)

Removes the method identified by symbol from the current class. For an example, see Module.undef_method.

Returns:



# File 'vm_method.c'

/*
 *  call-seq:
 *     remove_method(symbol)   -> self
 *
 *  Removes the method identified by _symbol_ from the current
 *  class. For an example, see <code>Module.undef_method</code>.
 */

static VALUE
rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
{
    int i;

    for (i = 0; i < argc; i++) {
    remove_method(mod, rb_to_id(argv[i]));
    }
    return mod;
}

- (String) to_s

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.

Returns:



# File 'object.c'

/*
 * call-seq:
 *   mod.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.
 */

static VALUE
rb_mod_to_s(VALUE klass)
{
    if (FL_TEST(klass, FL_SINGLETON)) {
    VALUE s = rb_usascii_str_new2("#<");
    VALUE v = rb_iv_get(klass, "__attached__");

    rb_str_cat2(s, "Class:");
    switch (TYPE(v)) {
      case T_CLASS: case T_MODULE:
        rb_str_append(s, rb_inspect(v));
        break;
      default:
        rb_str_append(s, rb_any_to_s(v));
        break;
    }
    rb_str_cat2(s, ">");

    return s;
    }
    return rb_str_dup(rb_class_name(klass));
}

- (Module) undef_method(symbol)

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)

Returns:



# File 'vm_method.c'

/*
 *  call-seq:
 *     undef_method(symbol)    -> self
 *
 *  Prevents the current class from responding to calls to the named
 *  method. Contrast this with <code>remove_method</code>, 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
 *
 *  <em>produces:</em>
 *
 *     In child
 *     In parent
 *     prog.rb:23: undefined method `hello' for #<Child:0x401b3bb4> (NoMethodError)
 */

static VALUE
rb_mod_undef_method(int argc, VALUE *argv, VALUE mod)
{
    int i;
    for (i = 0; i < argc; i++) {
    rb_undef(mod, rb_to_id(argv[i]));
    }
    return mod;
}