Module: Object2module::ModuleExtensions

Included in:
Module
Defined in:
lib/object2module.rb,
ext/object2module/object2module.c

Instance Method Summary collapse

Instance Method Details

#gen_include(*objs) ⇒ Object

Adds to the implied receiver the instance methods from each object given as a parameter.

Examples:

class C
  def hello
    "Hello from C.\n"
  end
end

class Klass
  gen_include(C)
end

k = Klass.new
k.hello         #=> "Hello from C.\n"

Parameters:

  • objs (Array)

    The array of objects to gen_include

Returns:



85
86
87
# File 'lib/object2module.rb', line 85

def gen_include(*objs)
  __gen_extend_or_include__(:gen_include_one, *objs)
end

#gen_include_one(module) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/object2module/object2module.c', line 56

VALUE
rb_gen_include_one(VALUE klass, VALUE module)
{
  VALUE p, c;
  int changed = 0;

  rb_frozen_class_p(klass);
  if (!OBJ_UNTRUSTED(klass)) {
    rb_secure(4);
  }

  /* when including an object, include its singleton class */
  if (TYPE(module) == T_OBJECT)
    module = rb_singleton_class(module);
    
  OBJ_INFECT(klass, module);
  c = klass;

  // loop until superclass is 0 (for modules) or superclass is a meta^n singleton of Object (for classes)
  while (module && !is_meta_singleton_of(module, rb_cObject)) {
    int superclass_seen = FALSE;

    if (RCLASS_M_TBL(klass) == RCLASS_M_TBL(module))
      rb_raise(rb_eArgError, "cyclic include detected");
    /* ignore if the module included already in superclasses */
    for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
      switch (BUILTIN_TYPE(p)) {
      case T_ICLASS:
        if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
          if (!superclass_seen) {
            c = p;  /* move insertion point */
          }
          goto skip;
        }
        break;
      case T_CLASS:
        superclass_seen = TRUE;
        break;
      }
    }
    c = RCLASS_SUPER(c) = include_class_new(module, RCLASS_SUPER(c));
    if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
      changed = 1;
  skip:
    module = RCLASS_SUPER(module);
  }
  if (changed) rb_clear_cache();

  return Qnil;
}