Ruby  2.0.0p247(2013-06-27revision41674)
object.c
Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   object.c -
00004 
00005   $Author: nagachika $
00006   created at: Thu Jul 15 12:01:24 JST 1993
00007 
00008   Copyright (C) 1993-2007 Yukihiro Matsumoto
00009   Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
00010   Copyright (C) 2000  Information-technology Promotion Agency, Japan
00011 
00012 **********************************************************************/
00013 
00014 #include "ruby/ruby.h"
00015 #include "ruby/st.h"
00016 #include "ruby/util.h"
00017 #include "ruby/encoding.h"
00018 #include <stdio.h>
00019 #include <errno.h>
00020 #include <ctype.h>
00021 #include <math.h>
00022 #include <float.h>
00023 #include "constant.h"
00024 #include "internal.h"
00025 #include "probes.h"
00026 
00027 VALUE rb_cBasicObject;
00028 VALUE rb_mKernel;
00029 VALUE rb_cObject;
00030 VALUE rb_cModule;
00031 VALUE rb_cClass;
00032 VALUE rb_cData;
00033 
00034 VALUE rb_cNilClass;
00035 VALUE rb_cTrueClass;
00036 VALUE rb_cFalseClass;
00037 
00038 static ID id_eq, id_eql, id_match, id_inspect;
00039 static ID id_init_copy, id_init_clone, id_init_dup;
00040 static ID id_const_missing;
00041 
00042 #define CLASS_OR_MODULE_P(obj) \
00043     (!SPECIAL_CONST_P(obj) && \
00044      (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
00045 
00046 /*
00047  *  call-seq:
00048  *     obj === other   -> true or false
00049  *
00050  *  Case Equality -- For class Object, effectively the same as calling
00051  *  <code>#==</code>, but typically overridden by descendants to provide
00052  *  meaningful semantics in +case+ statements.
00053  */
00054 
00055 VALUE
00056 rb_equal(VALUE obj1, VALUE obj2)
00057 {
00058     VALUE result;
00059 
00060     if (obj1 == obj2) return Qtrue;
00061     result = rb_funcall(obj1, id_eq, 1, obj2);
00062     if (RTEST(result)) return Qtrue;
00063     return Qfalse;
00064 }
00065 
00066 int
00067 rb_eql(VALUE obj1, VALUE obj2)
00068 {
00069     return RTEST(rb_funcall(obj1, id_eql, 1, obj2));
00070 }
00071 
00072 /*
00073  *  call-seq:
00074  *     obj == other        -> true or false
00075  *     obj.equal?(other)   -> true or false
00076  *     obj.eql?(other)     -> true or false
00077  *
00078  *  Equality --- At the <code>Object</code> level, <code>==</code> returns
00079  *  <code>true</code> only if +obj+ and +other+ are the same object.
00080  *  Typically, this method is overridden in descendant classes to provide
00081  *  class-specific meaning.
00082  *
00083  *  Unlike <code>==</code>, the <code>equal?</code> method should never be
00084  *  overridden by subclasses as it is used to determine object identity
00085  *  (that is, <code>a.equal?(b)</code> if and only if <code>a</code> is the
00086  *  same object as <code>b</code>):
00087  *
00088  *    obj = "a"
00089  *    other = obj.dup
00090  *
00091  *    a == other      #=> true
00092  *    a.equal? other  #=> false
00093  *    a.equal? a      #=> true
00094  *
00095  *  The <code>eql?</code> method returns <code>true</code> if +obj+ and
00096  *  +other+ refer to the same hash key.  This is used by Hash to test members
00097  *  for equality.  For objects of class <code>Object</code>, <code>eql?</code>
00098  *  is synonymous with <code>==</code>.  Subclasses normally continue this
00099  *  tradition by aliasing <code>eql?</code> to their overridden <code>==</code>
00100  *  method, but there are exceptions.  <code>Numeric</code> types, for
00101  *  example, perform type conversion across <code>==</code>, but not across
00102  *  <code>eql?</code>, so:
00103  *
00104  *     1 == 1.0     #=> true
00105  *     1.eql? 1.0   #=> false
00106  */
00107 
00108 VALUE
00109 rb_obj_equal(VALUE obj1, VALUE obj2)
00110 {
00111     if (obj1 == obj2) return Qtrue;
00112     return Qfalse;
00113 }
00114 
00115 /*
00116  * Generates a Fixnum hash value for this object.  This function must have the
00117  * property that <code>a.eql?(b)</code> implies <code>a.hash == b.hash</code>.
00118  *
00119  * The hash value is used along with #eql? by the Hash class to determine if
00120  * two objects reference the same hash key.  Any hash value that exceeds the
00121  * capacity of a Fixnum will be truncated before being used.
00122  *
00123  * The hash value for an object may not be identical across invocations or
00124  * implementations of ruby.  If you need a stable identifier across ruby
00125  * invocations and implementations you will need to generate one with a custom
00126  * method.
00127  */
00128 VALUE
00129 rb_obj_hash(VALUE obj)
00130 {
00131     VALUE oid = rb_obj_id(obj);
00132 #if SIZEOF_LONG == SIZEOF_VOIDP
00133     st_index_t index = NUM2LONG(oid);
00134 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
00135     st_index_t index = NUM2LL(oid);
00136 #else
00137 # error not supported
00138 #endif
00139     st_index_t h = rb_hash_end(rb_hash_start(index));
00140     return LONG2FIX(h);
00141 }
00142 
00143 /*
00144  *  call-seq:
00145  *     !obj    -> true or false
00146  *
00147  *  Boolean negate.
00148  */
00149 
00150 VALUE
00151 rb_obj_not(VALUE obj)
00152 {
00153     return RTEST(obj) ? Qfalse : Qtrue;
00154 }
00155 
00156 /*
00157  *  call-seq:
00158  *     obj != other        -> true or false
00159  *
00160  *  Returns true if two objects are not-equal, otherwise false.
00161  */
00162 
00163 VALUE
00164 rb_obj_not_equal(VALUE obj1, VALUE obj2)
00165 {
00166     VALUE result = rb_funcall(obj1, id_eq, 1, obj2);
00167     return RTEST(result) ? Qfalse : Qtrue;
00168 }
00169 
00170 VALUE
00171 rb_class_real(VALUE cl)
00172 {
00173     if (cl == 0)
00174         return 0;
00175     while ((RBASIC(cl)->flags & FL_SINGLETON) || BUILTIN_TYPE(cl) == T_ICLASS) {
00176         cl = RCLASS_SUPER(cl);
00177     }
00178     return cl;
00179 }
00180 
00181 /*
00182  *  call-seq:
00183  *     obj.class    -> class
00184  *
00185  *  Returns the class of <i>obj</i>. This method must always be
00186  *  called with an explicit receiver, as <code>class</code> is also a
00187  *  reserved word in Ruby.
00188  *
00189  *     1.class      #=> Fixnum
00190  *     self.class   #=> Object
00191  */
00192 
00193 VALUE
00194 rb_obj_class(VALUE obj)
00195 {
00196     return rb_class_real(CLASS_OF(obj));
00197 }
00198 
00199 /*
00200  *  call-seq:
00201  *     obj.singleton_class    -> class
00202  *
00203  *  Returns the singleton class of <i>obj</i>.  This method creates
00204  *  a new singleton class if <i>obj</i> does not have it.
00205  *
00206  *  If <i>obj</i> is <code>nil</code>, <code>true</code>, or
00207  *  <code>false</code>, it returns NilClass, TrueClass, or FalseClass,
00208  *  respectively.
00209  *  If <i>obj</i> is a Fixnum or a Symbol, it raises a TypeError.
00210  *
00211  *     Object.new.singleton_class  #=> #<Class:#<Object:0xb7ce1e24>>
00212  *     String.singleton_class      #=> #<Class:String>
00213  *     nil.singleton_class         #=> NilClass
00214  */
00215 
00216 static VALUE
00217 rb_obj_singleton_class(VALUE obj)
00218 {
00219     return rb_singleton_class(obj);
00220 }
00221 
00222 static void
00223 init_copy(VALUE dest, VALUE obj)
00224 {
00225     if (OBJ_FROZEN(dest)) {
00226         rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
00227     }
00228     RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
00229     RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR|FL_TAINT|FL_UNTRUSTED);
00230     rb_copy_generic_ivar(dest, obj);
00231     rb_gc_copy_finalizer(dest, obj);
00232     switch (TYPE(obj)) {
00233       case T_OBJECT:
00234         if (!(RBASIC(dest)->flags & ROBJECT_EMBED) && ROBJECT_IVPTR(dest)) {
00235             xfree(ROBJECT_IVPTR(dest));
00236             ROBJECT(dest)->as.heap.ivptr = 0;
00237             ROBJECT(dest)->as.heap.numiv = 0;
00238             ROBJECT(dest)->as.heap.iv_index_tbl = 0;
00239         }
00240         if (RBASIC(obj)->flags & ROBJECT_EMBED) {
00241             MEMCPY(ROBJECT(dest)->as.ary, ROBJECT(obj)->as.ary, VALUE, ROBJECT_EMBED_LEN_MAX);
00242             RBASIC(dest)->flags |= ROBJECT_EMBED;
00243         }
00244         else {
00245             long len = ROBJECT(obj)->as.heap.numiv;
00246             VALUE *ptr = ALLOC_N(VALUE, len);
00247             MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
00248             ROBJECT(dest)->as.heap.ivptr = ptr;
00249             ROBJECT(dest)->as.heap.numiv = len;
00250             ROBJECT(dest)->as.heap.iv_index_tbl = ROBJECT(obj)->as.heap.iv_index_tbl;
00251             RBASIC(dest)->flags &= ~ROBJECT_EMBED;
00252         }
00253         break;
00254       case T_CLASS:
00255       case T_MODULE:
00256         if (RCLASS_IV_TBL(dest)) {
00257             st_free_table(RCLASS_IV_TBL(dest));
00258             RCLASS_IV_TBL(dest) = 0;
00259         }
00260         if (RCLASS_CONST_TBL(dest)) {
00261             rb_free_const_table(RCLASS_CONST_TBL(dest));
00262             RCLASS_CONST_TBL(dest) = 0;
00263         }
00264         if (RCLASS_IV_TBL(obj)) {
00265             RCLASS_IV_TBL(dest) = st_copy(RCLASS_IV_TBL(obj));
00266         }
00267         break;
00268     }
00269 }
00270 
00271 /*
00272  *  call-seq:
00273  *     obj.clone -> an_object
00274  *
00275  *  Produces a shallow copy of <i>obj</i>---the instance variables of
00276  *  <i>obj</i> are copied, but not the objects they reference. Copies
00277  *  the frozen and tainted state of <i>obj</i>. See also the discussion
00278  *  under <code>Object#dup</code>.
00279  *
00280  *     class Klass
00281  *        attr_accessor :str
00282  *     end
00283  *     s1 = Klass.new      #=> #<Klass:0x401b3a38>
00284  *     s1.str = "Hello"    #=> "Hello"
00285  *     s2 = s1.clone       #=> #<Klass:0x401b3998 @str="Hello">
00286  *     s2.str[1,4] = "i"   #=> "i"
00287  *     s1.inspect          #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
00288  *     s2.inspect          #=> "#<Klass:0x401b3998 @str=\"Hi\">"
00289  *
00290  *  This method may have class-specific behavior.  If so, that
00291  *  behavior will be documented under the #+initialize_copy+ method of
00292  *  the class.
00293  */
00294 
00295 VALUE
00296 rb_obj_clone(VALUE obj)
00297 {
00298     VALUE clone;
00299     VALUE singleton;
00300 
00301     if (rb_special_const_p(obj)) {
00302         rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
00303     }
00304     clone = rb_obj_alloc(rb_obj_class(obj));
00305     singleton = rb_singleton_class_clone_and_attach(obj, clone);
00306     RBASIC(clone)->klass = singleton;
00307     if (FL_TEST(singleton, FL_SINGLETON)) {
00308         rb_singleton_class_attached(singleton, clone);
00309     }
00310     RBASIC(clone)->flags &= (FL_TAINT|FL_UNTRUSTED);
00311     RBASIC(clone)->flags |= RBASIC(obj)->flags & ~(FL_FREEZE|FL_FINALIZE);
00312     init_copy(clone, obj);
00313     rb_funcall(clone, id_init_clone, 1, obj);
00314     RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
00315 
00316     return clone;
00317 }
00318 
00319 /*
00320  *  call-seq:
00321  *     obj.dup -> an_object
00322  *
00323  *  Produces a shallow copy of <i>obj</i>---the instance variables of
00324  *  <i>obj</i> are copied, but not the objects they reference.
00325  *  <code>dup</code> copies the tainted state of <i>obj</i>. See also
00326  *  the discussion under <code>Object#clone</code>. In general,
00327  *  <code>clone</code> and <code>dup</code> may have different semantics
00328  *  in descendant classes. While <code>clone</code> is used to duplicate
00329  *  an object, including its internal state, <code>dup</code> typically
00330  *  uses the class of the descendant object to create the new instance.
00331  *
00332  *  This method may have class-specific behavior.  If so, that
00333  *  behavior will be documented under the #+initialize_copy+ method of
00334  *  the class.
00335  */
00336 
00337 VALUE
00338 rb_obj_dup(VALUE obj)
00339 {
00340     VALUE dup;
00341 
00342     if (rb_special_const_p(obj)) {
00343         rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj));
00344     }
00345     dup = rb_obj_alloc(rb_obj_class(obj));
00346     init_copy(dup, obj);
00347     rb_funcall(dup, id_init_dup, 1, obj);
00348 
00349     return dup;
00350 }
00351 
00352 /* :nodoc: */
00353 VALUE
00354 rb_obj_init_copy(VALUE obj, VALUE orig)
00355 {
00356     if (obj == orig) return obj;
00357     rb_check_frozen(obj);
00358     rb_check_trusted(obj);
00359     if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
00360         rb_raise(rb_eTypeError, "initialize_copy should take same class object");
00361     }
00362     return obj;
00363 }
00364 
00365 /* :nodoc: */
00366 VALUE
00367 rb_obj_init_dup_clone(VALUE obj, VALUE orig)
00368 {
00369     rb_funcall(obj, id_init_copy, 1, orig);
00370     return obj;
00371 }
00372 
00373 /*
00374  *  call-seq:
00375  *     obj.to_s    -> string
00376  *
00377  *  Returns a string representing <i>obj</i>. The default
00378  *  <code>to_s</code> prints the object's class and an encoding of the
00379  *  object id. As a special case, the top-level object that is the
00380  *  initial execution context of Ruby programs returns ``main.''
00381  */
00382 
00383 VALUE
00384 rb_any_to_s(VALUE obj)
00385 {
00386     VALUE str;
00387     VALUE cname = rb_class_name(CLASS_OF(obj));
00388 
00389     str = rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)obj);
00390     OBJ_INFECT(str, obj);
00391 
00392     return str;
00393 }
00394 
00395 /*
00396  * If the default external encoding is ASCII compatible, the encoding of
00397  * inspected result must be compatible with it.
00398  * If the default external encoding is ASCII incomapatible,
00399  * the result must be ASCII only.
00400  */
00401 VALUE
00402 rb_inspect(VALUE obj)
00403 {
00404     VALUE str = rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
00405     rb_encoding *ext = rb_default_external_encoding();
00406     if (!rb_enc_asciicompat(ext)) {
00407         if (!rb_enc_str_asciionly_p(str))
00408             rb_raise(rb_eEncCompatError, "inspected result must be ASCII only if default external encoding is ASCII incompatible");
00409         return str;
00410     }
00411     if (rb_enc_get(str) != ext && !rb_enc_str_asciionly_p(str))
00412         rb_raise(rb_eEncCompatError, "inspected result must be ASCII only or use the same encoding with default external");
00413     return str;
00414 }
00415 
00416 static int
00417 inspect_i(st_data_t k, st_data_t v, st_data_t a)
00418 {
00419     ID id = (ID)k;
00420     VALUE value = (VALUE)v;
00421     VALUE str = (VALUE)a;
00422     VALUE str2;
00423     const char *ivname;
00424 
00425     /* need not to show internal data */
00426     if (CLASS_OF(value) == 0) return ST_CONTINUE;
00427     if (!rb_is_instance_id(id)) return ST_CONTINUE;
00428     if (RSTRING_PTR(str)[0] == '-') { /* first element */
00429         RSTRING_PTR(str)[0] = '#';
00430         rb_str_cat2(str, " ");
00431     }
00432     else {
00433         rb_str_cat2(str, ", ");
00434     }
00435     ivname = rb_id2name(id);
00436     rb_str_cat2(str, ivname);
00437     rb_str_cat2(str, "=");
00438     str2 = rb_inspect(value);
00439     rb_str_append(str, str2);
00440     OBJ_INFECT(str, str2);
00441 
00442     return ST_CONTINUE;
00443 }
00444 
00445 static VALUE
00446 inspect_obj(VALUE obj, VALUE str, int recur)
00447 {
00448     if (recur) {
00449         rb_str_cat2(str, " ...");
00450     }
00451     else {
00452         rb_ivar_foreach(obj, inspect_i, str);
00453     }
00454     rb_str_cat2(str, ">");
00455     RSTRING_PTR(str)[0] = '#';
00456     OBJ_INFECT(str, obj);
00457 
00458     return str;
00459 }
00460 
00461 /*
00462  *  call-seq:
00463  *     obj.inspect   -> string
00464  *
00465  * Returns a string containing a human-readable representation of <i>obj</i>.
00466  * By default, show the class name and the list of the instance variables and
00467  * their values (by calling #inspect on each of them).
00468  * User defined classes should override this method to make better
00469  * representation of <i>obj</i>.  When overriding this method, it should
00470  * return a string whose encoding is compatible with the default external
00471  * encoding.
00472  *
00473  *     [ 1, 2, 3..4, 'five' ].inspect   #=> "[1, 2, 3..4, \"five\"]"
00474  *     Time.new.inspect                 #=> "2008-03-08 19:43:39 +0900"
00475  *
00476  *     class Foo
00477  *     end
00478  *     Foo.new.inspect                  #=> "#<Foo:0x0300c868>"
00479  *
00480  *     class Bar
00481  *       def initialize
00482  *         @bar = 1
00483  *       end
00484  *     end
00485  *     Bar.new.inspect                  #=> "#<Bar:0x0300c868 @bar=1>"
00486  *
00487  *     class Baz
00488  *       def to_s
00489  *         "baz"
00490  *       end
00491  *     end
00492  *     Baz.new.inspect                  #=> "#<Baz:0x0300c868>"
00493  */
00494 
00495 static VALUE
00496 rb_obj_inspect(VALUE obj)
00497 {
00498     if (rb_ivar_count(obj) > 0) {
00499         VALUE str;
00500         VALUE c = rb_class_name(CLASS_OF(obj));
00501 
00502         str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void*)obj);
00503         return rb_exec_recursive(inspect_obj, obj, str);
00504     }
00505     else {
00506         return rb_any_to_s(obj);
00507     }
00508 }
00509 
00510 static VALUE
00511 class_or_module_required(VALUE c)
00512 {
00513     if (SPECIAL_CONST_P(c)) goto not_class;
00514     switch (BUILTIN_TYPE(c)) {
00515       case T_MODULE:
00516       case T_CLASS:
00517       case T_ICLASS:
00518         break;
00519 
00520       default:
00521       not_class:
00522         rb_raise(rb_eTypeError, "class or module required");
00523     }
00524     return c;
00525 }
00526 
00527 /*
00528  *  call-seq:
00529  *     obj.instance_of?(class)    -> true or false
00530  *
00531  *  Returns <code>true</code> if <i>obj</i> is an instance of the given
00532  *  class. See also <code>Object#kind_of?</code>.
00533  *
00534  *     class A;     end
00535  *     class B < A; end
00536  *     class C < B; end
00537  *
00538  *     b = B.new
00539  *     b.instance_of? A   #=> false
00540  *     b.instance_of? B   #=> true
00541  *     b.instance_of? C   #=> false
00542  */
00543 
00544 VALUE
00545 rb_obj_is_instance_of(VALUE obj, VALUE c)
00546 {
00547     c = class_or_module_required(c);
00548     if (rb_obj_class(obj) == c) return Qtrue;
00549     return Qfalse;
00550 }
00551 
00552 
00553 /*
00554  *  call-seq:
00555  *     obj.is_a?(class)       -> true or false
00556  *     obj.kind_of?(class)    -> true or false
00557  *
00558  *  Returns <code>true</code> if <i>class</i> is the class of
00559  *  <i>obj</i>, or if <i>class</i> is one of the superclasses of
00560  *  <i>obj</i> or modules included in <i>obj</i>.
00561  *
00562  *     module M;    end
00563  *     class A
00564  *       include M
00565  *     end
00566  *     class B < A; end
00567  *     class C < B; end
00568  *
00569  *     b = B.new
00570  *     b.is_a? A          #=> true
00571  *     b.is_a? B          #=> true
00572  *     b.is_a? C          #=> false
00573  *     b.is_a? M          #=> true
00574  *
00575  *     b.kind_of? A       #=> true
00576  *     b.kind_of? B       #=> true
00577  *     b.kind_of? C       #=> false
00578  *     b.kind_of? M       #=> true
00579  */
00580 
00581 VALUE
00582 rb_obj_is_kind_of(VALUE obj, VALUE c)
00583 {
00584     VALUE cl = CLASS_OF(obj);
00585 
00586     c = class_or_module_required(c);
00587     c = RCLASS_ORIGIN(c);
00588     while (cl) {
00589         if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
00590             return Qtrue;
00591         cl = RCLASS_SUPER(cl);
00592     }
00593     return Qfalse;
00594 }
00595 
00596 
00597 /*
00598  *  call-seq:
00599  *     obj.tap{|x|...}    -> obj
00600  *
00601  *  Yields <code>x</code> to the block, and then returns <code>x</code>.
00602  *  The primary purpose of this method is to "tap into" a method chain,
00603  *  in order to perform operations on intermediate results within the chain.
00604  *
00605  *      (1..10)                .tap {|x| puts "original: #{x.inspect}"}
00606  *        .to_a                .tap {|x| puts "array: #{x.inspect}"}
00607  *        .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"}
00608  *        .map { |x| x*x }     .tap {|x| puts "squares: #{x.inspect}"}
00609  *
00610  */
00611 
00612 VALUE
00613 rb_obj_tap(VALUE obj)
00614 {
00615     rb_yield(obj);
00616     return obj;
00617 }
00618 
00619 
00620 /*
00621  * Document-method: inherited
00622  *
00623  * call-seq:
00624  *    inherited(subclass)
00625  *
00626  * Callback invoked whenever a subclass of the current class is created.
00627  *
00628  * Example:
00629  *
00630  *    class Foo
00631  *      def self.inherited(subclass)
00632  *        puts "New subclass: #{subclass}"
00633  *      end
00634  *    end
00635  *
00636  *    class Bar < Foo
00637  *    end
00638  *
00639  *    class Baz < Bar
00640  *    end
00641  *
00642  * produces:
00643  *
00644  *    New subclass: Bar
00645  *    New subclass: Baz
00646  */
00647 
00648 /* Document-method: method_added
00649  *
00650  * call-seq:
00651  *   method_added(method_name)
00652  *
00653  * Invoked as a callback whenever an instance method is added to the
00654  * receiver.
00655  *
00656  *   module Chatty
00657  *     def self.method_added(method_name)
00658  *       puts "Adding #{method_name.inspect}"
00659  *     end
00660  *     def self.some_class_method() end
00661  *     def some_instance_method() end
00662  *   end
00663  *
00664  * produces:
00665  *
00666  *   Adding :some_instance_method
00667  *
00668  */
00669 
00670 /* Document-method: method_removed
00671  *
00672  * call-seq:
00673  *   method_removed(method_name)
00674  *
00675  * Invoked as a callback whenever an instance method is removed from the
00676  * receiver.
00677  *
00678  *   module Chatty
00679  *     def self.method_removed(method_name)
00680  *       puts "Removing #{method_name.inspect}"
00681  *     end
00682  *     def self.some_class_method() end
00683  *     def some_instance_method() end
00684  *     class << self
00685  *       remove_method :some_class_method
00686  *     end
00687  *     remove_method :some_instance_method
00688  *   end
00689  *
00690  * produces:
00691  *
00692  *   Removing :some_instance_method
00693  *
00694  */
00695 
00696 /*
00697  * Document-method: singleton_method_added
00698  *
00699  *  call-seq:
00700  *     singleton_method_added(symbol)
00701  *
00702  *  Invoked as a callback whenever a singleton method is added to the
00703  *  receiver.
00704  *
00705  *     module Chatty
00706  *       def Chatty.singleton_method_added(id)
00707  *         puts "Adding #{id.id2name}"
00708  *       end
00709  *       def self.one()     end
00710  *       def two()          end
00711  *       def Chatty.three() end
00712  *     end
00713  *
00714  *  <em>produces:</em>
00715  *
00716  *     Adding singleton_method_added
00717  *     Adding one
00718  *     Adding three
00719  *
00720  */
00721 
00722 /*
00723  * Document-method: singleton_method_removed
00724  *
00725  *  call-seq:
00726  *     singleton_method_removed(symbol)
00727  *
00728  *  Invoked as a callback whenever a singleton method is removed from
00729  *  the receiver.
00730  *
00731  *     module Chatty
00732  *       def Chatty.singleton_method_removed(id)
00733  *         puts "Removing #{id.id2name}"
00734  *       end
00735  *       def self.one()     end
00736  *       def two()          end
00737  *       def Chatty.three() end
00738  *       class << self
00739  *         remove_method :three
00740  *         remove_method :one
00741  *       end
00742  *     end
00743  *
00744  *  <em>produces:</em>
00745  *
00746  *     Removing three
00747  *     Removing one
00748  */
00749 
00750 /*
00751  * Document-method: singleton_method_undefined
00752  *
00753  *  call-seq:
00754  *     singleton_method_undefined(symbol)
00755  *
00756  *  Invoked as a callback whenever a singleton method is undefined in
00757  *  the receiver.
00758  *
00759  *     module Chatty
00760  *       def Chatty.singleton_method_undefined(id)
00761  *         puts "Undefining #{id.id2name}"
00762  *       end
00763  *       def Chatty.one()   end
00764  *       class << self
00765  *          undef_method(:one)
00766  *       end
00767  *     end
00768  *
00769  *  <em>produces:</em>
00770  *
00771  *     Undefining one
00772  */
00773 
00774 
00775 /*
00776  * Document-method: included
00777  *
00778  * call-seq:
00779  *    included( othermod )
00780  *
00781  * Callback invoked whenever the receiver is included in another
00782  * module or class. This should be used in preference to
00783  * <tt>Module.append_features</tt> if your code wants to perform some
00784  * action when a module is included in another.
00785  *
00786  *        module A
00787  *          def A.included(mod)
00788  *            puts "#{self} included in #{mod}"
00789  *          end
00790  *        end
00791  *        module Enumerable
00792  *          include A
00793  *        end
00794  *         # => prints "A included in Enumerable"
00795  */
00796 
00797 /*
00798  * Document-method: prepended
00799  *
00800  * call-seq:
00801  *    prepended( othermod )
00802  *
00803  * The equivalent of <tt>included</tt>, but for prepended modules.
00804  *
00805  *        module A
00806  *          def self.prepended(mod)
00807  *            puts "#{self} prepended to #{mod}"
00808  *          end
00809  *        end
00810  *        module Enumerable
00811  *          prepend A
00812  *        end
00813  *         # => prints "A prepended to Enumerable"
00814  */
00815 
00816 /*
00817  * Document-method: initialize
00818  *
00819  * call-seq:
00820  *    BasicObject.new
00821  *
00822  * Returns a new BasicObject.
00823  */
00824 
00825 /*
00826  * Not documented
00827  */
00828 
00829 static VALUE
00830 rb_obj_dummy(void)
00831 {
00832     return Qnil;
00833 }
00834 
00835 /*
00836  *  call-seq:
00837  *     obj.tainted?    -> true or false
00838  *
00839  *  Returns <code>true</code> if the object is tainted.
00840  */
00841 
00842 VALUE
00843 rb_obj_tainted(VALUE obj)
00844 {
00845     if (OBJ_TAINTED(obj))
00846         return Qtrue;
00847     return Qfalse;
00848 }
00849 
00850 /*
00851  *  call-seq:
00852  *     obj.taint -> obj
00853  *
00854  *  Marks <i>obj</i> as tainted---if the <code>$SAFE</code> level is
00855  *  set appropriately, many method calls which might alter the running
00856  *  programs environment will refuse to accept tainted strings.
00857  */
00858 
00859 VALUE
00860 rb_obj_taint(VALUE obj)
00861 {
00862     rb_secure(4);
00863     if (!OBJ_TAINTED(obj)) {
00864         rb_check_frozen(obj);
00865         OBJ_TAINT(obj);
00866     }
00867     return obj;
00868 }
00869 
00870 
00871 /*
00872  *  call-seq:
00873  *     obj.untaint    -> obj
00874  *
00875  *  Removes the taint from <i>obj</i>.
00876  */
00877 
00878 VALUE
00879 rb_obj_untaint(VALUE obj)
00880 {
00881     rb_secure(3);
00882     if (OBJ_TAINTED(obj)) {
00883         rb_check_frozen(obj);
00884         FL_UNSET(obj, FL_TAINT);
00885     }
00886     return obj;
00887 }
00888 
00889 /*
00890  *  call-seq:
00891  *     obj.untrusted?    -> true or false
00892  *
00893  *  Returns <code>true</code> if the object is untrusted.
00894  */
00895 
00896 VALUE
00897 rb_obj_untrusted(VALUE obj)
00898 {
00899     if (OBJ_UNTRUSTED(obj))
00900         return Qtrue;
00901     return Qfalse;
00902 }
00903 
00904 /*
00905  *  call-seq:
00906  *     obj.untrust -> obj
00907  *
00908  *  Marks <i>obj</i> as untrusted.
00909  */
00910 
00911 VALUE
00912 rb_obj_untrust(VALUE obj)
00913 {
00914     rb_secure(4);
00915     if (!OBJ_UNTRUSTED(obj)) {
00916         rb_check_frozen(obj);
00917         OBJ_UNTRUST(obj);
00918     }
00919     return obj;
00920 }
00921 
00922 
00923 /*
00924  *  call-seq:
00925  *     obj.trust    -> obj
00926  *
00927  *  Removes the untrusted mark from <i>obj</i>.
00928  */
00929 
00930 VALUE
00931 rb_obj_trust(VALUE obj)
00932 {
00933     rb_secure(3);
00934     if (OBJ_UNTRUSTED(obj)) {
00935         rb_check_frozen(obj);
00936         FL_UNSET(obj, FL_UNTRUSTED);
00937     }
00938     return obj;
00939 }
00940 
00941 void
00942 rb_obj_infect(VALUE obj1, VALUE obj2)
00943 {
00944     OBJ_INFECT(obj1, obj2);
00945 }
00946 
00947 static st_table *immediate_frozen_tbl = 0;
00948 
00949 /*
00950  *  call-seq:
00951  *     obj.freeze    -> obj
00952  *
00953  *  Prevents further modifications to <i>obj</i>. A
00954  *  <code>RuntimeError</code> will be raised if modification is attempted.
00955  *  There is no way to unfreeze a frozen object. See also
00956  *  <code>Object#frozen?</code>.
00957  *
00958  *  This method returns self.
00959  *
00960  *     a = [ "a", "b", "c" ]
00961  *     a.freeze
00962  *     a << "z"
00963  *
00964  *  <em>produces:</em>
00965  *
00966  *     prog.rb:3:in `<<': can't modify frozen array (RuntimeError)
00967  *      from prog.rb:3
00968  */
00969 
00970 VALUE
00971 rb_obj_freeze(VALUE obj)
00972 {
00973     if (!OBJ_FROZEN(obj)) {
00974         if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(obj)) {
00975             rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
00976         }
00977         OBJ_FREEZE(obj);
00978         if (SPECIAL_CONST_P(obj)) {
00979             if (!immediate_frozen_tbl) {
00980                 immediate_frozen_tbl = st_init_numtable();
00981             }
00982             st_insert(immediate_frozen_tbl, obj, (st_data_t)Qtrue);
00983         }
00984     }
00985     return obj;
00986 }
00987 
00988 /*
00989  *  call-seq:
00990  *     obj.frozen?    -> true or false
00991  *
00992  *  Returns the freeze status of <i>obj</i>.
00993  *
00994  *     a = [ "a", "b", "c" ]
00995  *     a.freeze    #=> ["a", "b", "c"]
00996  *     a.frozen?   #=> true
00997  */
00998 
00999 VALUE
01000 rb_obj_frozen_p(VALUE obj)
01001 {
01002     if (OBJ_FROZEN(obj)) return Qtrue;
01003     if (SPECIAL_CONST_P(obj)) {
01004         if (!immediate_frozen_tbl) return Qfalse;
01005         if (st_lookup(immediate_frozen_tbl, obj, 0)) return Qtrue;
01006     }
01007     return Qfalse;
01008 }
01009 
01010 
01011 /*
01012  * Document-class: NilClass
01013  *
01014  *  The class of the singleton object <code>nil</code>.
01015  */
01016 
01017 /*
01018  *  call-seq:
01019  *     nil.to_i -> 0
01020  *
01021  *  Always returns zero.
01022  *
01023  *     nil.to_i   #=> 0
01024  */
01025 
01026 
01027 static VALUE
01028 nil_to_i(VALUE obj)
01029 {
01030     return INT2FIX(0);
01031 }
01032 
01033 /*
01034  *  call-seq:
01035  *     nil.to_f    -> 0.0
01036  *
01037  *  Always returns zero.
01038  *
01039  *     nil.to_f   #=> 0.0
01040  */
01041 
01042 static VALUE
01043 nil_to_f(VALUE obj)
01044 {
01045     return DBL2NUM(0.0);
01046 }
01047 
01048 /*
01049  *  call-seq:
01050  *     nil.to_s    -> ""
01051  *
01052  *  Always returns the empty string.
01053  */
01054 
01055 static VALUE
01056 nil_to_s(VALUE obj)
01057 {
01058     return rb_usascii_str_new(0, 0);
01059 }
01060 
01061 /*
01062  * Document-method: to_a
01063  *
01064  *  call-seq:
01065  *     nil.to_a    -> []
01066  *
01067  *  Always returns an empty array.
01068  *
01069  *     nil.to_a   #=> []
01070  */
01071 
01072 static VALUE
01073 nil_to_a(VALUE obj)
01074 {
01075     return rb_ary_new2(0);
01076 }
01077 
01078 /*
01079  * Document-method: to_h
01080  *
01081  *  call-seq:
01082  *     nil.to_h    -> {}
01083  *
01084  *  Always returns an empty hash.
01085  *
01086  *     nil.to_h   #=> {}
01087  */
01088 
01089 static VALUE
01090 nil_to_h(VALUE obj)
01091 {
01092     return rb_hash_new();
01093 }
01094 
01095 /*
01096  *  call-seq:
01097  *    nil.inspect  -> "nil"
01098  *
01099  *  Always returns the string "nil".
01100  */
01101 
01102 static VALUE
01103 nil_inspect(VALUE obj)
01104 {
01105     return rb_usascii_str_new2("nil");
01106 }
01107 
01108 /***********************************************************************
01109  *  Document-class: TrueClass
01110  *
01111  *  The global value <code>true</code> is the only instance of class
01112  *  <code>TrueClass</code> and represents a logically true value in
01113  *  boolean expressions. The class provides operators allowing
01114  *  <code>true</code> to be used in logical expressions.
01115  */
01116 
01117 
01118 /*
01119  * call-seq:
01120  *   true.to_s   ->  "true"
01121  *
01122  * The string representation of <code>true</code> is "true".
01123  */
01124 
01125 static VALUE
01126 true_to_s(VALUE obj)
01127 {
01128     return rb_usascii_str_new2("true");
01129 }
01130 
01131 
01132 /*
01133  *  call-seq:
01134  *     true & obj    -> true or false
01135  *
01136  *  And---Returns <code>false</code> if <i>obj</i> is
01137  *  <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
01138  */
01139 
01140 static VALUE
01141 true_and(VALUE obj, VALUE obj2)
01142 {
01143     return RTEST(obj2)?Qtrue:Qfalse;
01144 }
01145 
01146 /*
01147  *  call-seq:
01148  *     true | obj   -> true
01149  *
01150  *  Or---Returns <code>true</code>. As <i>anObject</i> is an argument to
01151  *  a method call, it is always evaluated; there is no short-circuit
01152  *  evaluation in this case.
01153  *
01154  *     true |  puts("or")
01155  *     true || puts("logical or")
01156  *
01157  *  <em>produces:</em>
01158  *
01159  *     or
01160  */
01161 
01162 static VALUE
01163 true_or(VALUE obj, VALUE obj2)
01164 {
01165     return Qtrue;
01166 }
01167 
01168 
01169 /*
01170  *  call-seq:
01171  *     true ^ obj   -> !obj
01172  *
01173  *  Exclusive Or---Returns <code>true</code> if <i>obj</i> is
01174  *  <code>nil</code> or <code>false</code>, <code>false</code>
01175  *  otherwise.
01176  */
01177 
01178 static VALUE
01179 true_xor(VALUE obj, VALUE obj2)
01180 {
01181     return RTEST(obj2)?Qfalse:Qtrue;
01182 }
01183 
01184 
01185 /*
01186  *  Document-class: FalseClass
01187  *
01188  *  The global value <code>false</code> is the only instance of class
01189  *  <code>FalseClass</code> and represents a logically false value in
01190  *  boolean expressions. The class provides operators allowing
01191  *  <code>false</code> to participate correctly in logical expressions.
01192  *
01193  */
01194 
01195 /*
01196  * call-seq:
01197  *   false.to_s   ->  "false"
01198  *
01199  * 'nuf said...
01200  */
01201 
01202 static VALUE
01203 false_to_s(VALUE obj)
01204 {
01205     return rb_usascii_str_new2("false");
01206 }
01207 
01208 /*
01209  *  call-seq:
01210  *     false & obj   -> false
01211  *     nil & obj     -> false
01212  *
01213  *  And---Returns <code>false</code>. <i>obj</i> is always
01214  *  evaluated as it is the argument to a method call---there is no
01215  *  short-circuit evaluation in this case.
01216  */
01217 
01218 static VALUE
01219 false_and(VALUE obj, VALUE obj2)
01220 {
01221     return Qfalse;
01222 }
01223 
01224 
01225 /*
01226  *  call-seq:
01227  *     false | obj   ->   true or false
01228  *     nil   | obj   ->   true or false
01229  *
01230  *  Or---Returns <code>false</code> if <i>obj</i> is
01231  *  <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
01232  */
01233 
01234 static VALUE
01235 false_or(VALUE obj, VALUE obj2)
01236 {
01237     return RTEST(obj2)?Qtrue:Qfalse;
01238 }
01239 
01240 
01241 
01242 /*
01243  *  call-seq:
01244  *     false ^ obj    -> true or false
01245  *     nil   ^ obj    -> true or false
01246  *
01247  *  Exclusive Or---If <i>obj</i> is <code>nil</code> or
01248  *  <code>false</code>, returns <code>false</code>; otherwise, returns
01249  *  <code>true</code>.
01250  *
01251  */
01252 
01253 static VALUE
01254 false_xor(VALUE obj, VALUE obj2)
01255 {
01256     return RTEST(obj2)?Qtrue:Qfalse;
01257 }
01258 
01259 /*
01260  * call_seq:
01261  *   nil.nil?               -> true
01262  *
01263  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
01264  */
01265 
01266 static VALUE
01267 rb_true(VALUE obj)
01268 {
01269     return Qtrue;
01270 }
01271 
01272 /*
01273  * call_seq:
01274  *   nil.nil?               -> true
01275  *   <anything_else>.nil?   -> false
01276  *
01277  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
01278  */
01279 
01280 
01281 static VALUE
01282 rb_false(VALUE obj)
01283 {
01284     return Qfalse;
01285 }
01286 
01287 
01288 /*
01289  *  call-seq:
01290  *     obj =~ other  -> nil
01291  *
01292  *  Pattern Match---Overridden by descendants (notably
01293  *  <code>Regexp</code> and <code>String</code>) to provide meaningful
01294  *  pattern-match semantics.
01295  */
01296 
01297 static VALUE
01298 rb_obj_match(VALUE obj1, VALUE obj2)
01299 {
01300     return Qnil;
01301 }
01302 
01303 /*
01304  *  call-seq:
01305  *     obj !~ other  -> true or false
01306  *
01307  *  Returns true if two objects do not match (using the <i>=~</i>
01308  *  method), otherwise false.
01309  */
01310 
01311 static VALUE
01312 rb_obj_not_match(VALUE obj1, VALUE obj2)
01313 {
01314     VALUE result = rb_funcall(obj1, id_match, 1, obj2);
01315     return RTEST(result) ? Qfalse : Qtrue;
01316 }
01317 
01318 
01319 /*
01320  *  call-seq:
01321  *     obj <=> other -> 0 or nil
01322  *
01323  *  Returns 0 if obj === other, otherwise nil.
01324  *
01325  *  The <=> is used by various methods to compare objects, for example
01326  *  Enumerable#sort, Enumerable#max etc.
01327  *
01328  *  Your implementation of <=> should return one of the following values: -1, 0,
01329  *  1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
01330  *  1 means self is bigger than other. Nil means the two values could not be
01331  *  compared.
01332  *
01333  *  When you defined <=>, you can include Comparable to gain the methods <=, <,
01334  *  ==, >=, > and between?.
01335  */
01336 static VALUE
01337 rb_obj_cmp(VALUE obj1, VALUE obj2)
01338 {
01339     if (obj1 == obj2 || rb_equal(obj1, obj2))
01340         return INT2FIX(0);
01341     return Qnil;
01342 }
01343 
01344 /***********************************************************************
01345  *
01346  * Document-class: Module
01347  *
01348  *  A <code>Module</code> is a collection of methods and constants. The
01349  *  methods in a module may be instance methods or module methods.
01350  *  Instance methods appear as methods in a class when the module is
01351  *  included, module methods do not. Conversely, module methods may be
01352  *  called without creating an encapsulating object, while instance
01353  *  methods may not. (See <code>Module#module_function</code>)
01354  *
01355  *  In the descriptions that follow, the parameter <i>sym</i> refers
01356  *  to a symbol, which is either a quoted string or a
01357  *  <code>Symbol</code> (such as <code>:name</code>).
01358  *
01359  *     module Mod
01360  *       include Math
01361  *       CONST = 1
01362  *       def meth
01363  *         #  ...
01364  *       end
01365  *     end
01366  *     Mod.class              #=> Module
01367  *     Mod.constants          #=> [:CONST, :PI, :E]
01368  *     Mod.instance_methods   #=> [:meth]
01369  *
01370  */
01371 
01372 /*
01373  * call-seq:
01374  *   mod.to_s   -> string
01375  *
01376  * Return a string representing this module or class. For basic
01377  * classes and modules, this is the name. For singletons, we
01378  * show information on the thing we're attached to as well.
01379  */
01380 
01381 static VALUE
01382 rb_mod_to_s(VALUE klass)
01383 {
01384     ID id_defined_at;
01385     VALUE refined_class, defined_at;
01386 
01387     if (FL_TEST(klass, FL_SINGLETON)) {
01388         VALUE s = rb_usascii_str_new2("#<Class:");
01389         VALUE v = rb_iv_get(klass, "__attached__");
01390 
01391         if (CLASS_OR_MODULE_P(v)) {
01392             rb_str_append(s, rb_inspect(v));
01393         }
01394         else {
01395             rb_str_append(s, rb_any_to_s(v));
01396         }
01397         rb_str_cat2(s, ">");
01398 
01399         return s;
01400     }
01401     refined_class = rb_refinement_module_get_refined_class(klass);
01402     if (!NIL_P(refined_class)) {
01403         VALUE s = rb_usascii_str_new2("#<refinement:");
01404 
01405         rb_str_concat(s, rb_inspect(refined_class));
01406         rb_str_cat2(s, "@");
01407         CONST_ID(id_defined_at, "__defined_at__");
01408         defined_at = rb_attr_get(klass, id_defined_at);
01409         rb_str_concat(s, rb_inspect(defined_at));
01410         rb_str_cat2(s, ">");
01411         return s;
01412     }
01413     return rb_str_dup(rb_class_name(klass));
01414 }
01415 
01416 /*
01417  *  call-seq:
01418  *     mod.freeze       -> mod
01419  *
01420  *  Prevents further modifications to <i>mod</i>.
01421  *
01422  *  This method returns self.
01423  */
01424 
01425 static VALUE
01426 rb_mod_freeze(VALUE mod)
01427 {
01428     rb_class_name(mod);
01429     return rb_obj_freeze(mod);
01430 }
01431 
01432 /*
01433  *  call-seq:
01434  *     mod === obj    -> true or false
01435  *
01436  *  Case Equality---Returns <code>true</code> if <i>anObject</i> is an
01437  *  instance of <i>mod</i> or one of <i>mod</i>'s descendants. Of
01438  *  limited use for modules, but can be used in <code>case</code>
01439  *  statements to classify objects by class.
01440  */
01441 
01442 static VALUE
01443 rb_mod_eqq(VALUE mod, VALUE arg)
01444 {
01445     return rb_obj_is_kind_of(arg, mod);
01446 }
01447 
01448 /*
01449  * call-seq:
01450  *   mod <= other   ->  true, false, or nil
01451  *
01452  * Returns true if <i>mod</i> is a subclass of <i>other</i> or
01453  * is the same as <i>other</i>. Returns
01454  * <code>nil</code> if there's no relationship between the two.
01455  * (Think of the relationship in terms of the class definition:
01456  * "class A<B" implies "A<B").
01457  *
01458  */
01459 
01460 VALUE
01461 rb_class_inherited_p(VALUE mod, VALUE arg)
01462 {
01463     VALUE start = mod;
01464 
01465     if (mod == arg) return Qtrue;
01466     if (!CLASS_OR_MODULE_P(arg)) {
01467         rb_raise(rb_eTypeError, "compared with non class/module");
01468     }
01469     arg = RCLASS_ORIGIN(arg);
01470     while (mod) {
01471         if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg))
01472             return Qtrue;
01473         mod = RCLASS_SUPER(mod);
01474     }
01475     /* not mod < arg; check if mod > arg */
01476     while (arg) {
01477         if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start))
01478             return Qfalse;
01479         arg = RCLASS_SUPER(arg);
01480     }
01481     return Qnil;
01482 }
01483 
01484 /*
01485  * call-seq:
01486  *   mod < other   ->  true, false, or nil
01487  *
01488  * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
01489  * <code>nil</code> if there's no relationship between the two.
01490  * (Think of the relationship in terms of the class definition:
01491  * "class A<B" implies "A<B").
01492  *
01493  */
01494 
01495 static VALUE
01496 rb_mod_lt(VALUE mod, VALUE arg)
01497 {
01498     if (mod == arg) return Qfalse;
01499     return rb_class_inherited_p(mod, arg);
01500 }
01501 
01502 
01503 /*
01504  * call-seq:
01505  *   mod >= other   ->  true, false, or nil
01506  *
01507  * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
01508  * two modules are the same. Returns
01509  * <code>nil</code> if there's no relationship between the two.
01510  * (Think of the relationship in terms of the class definition:
01511  * "class A<B" implies "B>A").
01512  *
01513  */
01514 
01515 static VALUE
01516 rb_mod_ge(VALUE mod, VALUE arg)
01517 {
01518     if (!CLASS_OR_MODULE_P(arg)) {
01519         rb_raise(rb_eTypeError, "compared with non class/module");
01520     }
01521 
01522     return rb_class_inherited_p(arg, mod);
01523 }
01524 
01525 /*
01526  * call-seq:
01527  *   mod > other   ->  true, false, or nil
01528  *
01529  * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
01530  * <code>nil</code> if there's no relationship between the two.
01531  * (Think of the relationship in terms of the class definition:
01532  * "class A<B" implies "B>A").
01533  *
01534  */
01535 
01536 static VALUE
01537 rb_mod_gt(VALUE mod, VALUE arg)
01538 {
01539     if (mod == arg) return Qfalse;
01540     return rb_mod_ge(mod, arg);
01541 }
01542 
01543 /*
01544  *  call-seq:
01545  *     module <=> other_module   -> -1, 0, +1, or nil
01546  *
01547  *  Comparison---Returns -1, 0, +1 or nil depending on whether +module+
01548  *  includes +other_module+, they are the same, or if +module+ is included by
01549  *  +other_module+. This is the basis for the tests in Comparable.
01550  *
01551  *  Returns +nil+ if +module+ has no relationship with +other_module+, if
01552  *  +other_module+ is not a module, or if the two values are incomparable.
01553  */
01554 
01555 static VALUE
01556 rb_mod_cmp(VALUE mod, VALUE arg)
01557 {
01558     VALUE cmp;
01559 
01560     if (mod == arg) return INT2FIX(0);
01561     if (!CLASS_OR_MODULE_P(arg)) {
01562         return Qnil;
01563     }
01564 
01565     cmp = rb_class_inherited_p(mod, arg);
01566     if (NIL_P(cmp)) return Qnil;
01567     if (cmp) {
01568         return INT2FIX(-1);
01569     }
01570     return INT2FIX(1);
01571 }
01572 
01573 static VALUE
01574 rb_module_s_alloc(VALUE klass)
01575 {
01576     VALUE mod = rb_module_new();
01577 
01578     RBASIC(mod)->klass = klass;
01579     return mod;
01580 }
01581 
01582 static VALUE
01583 rb_class_s_alloc(VALUE klass)
01584 {
01585     return rb_class_boot(0);
01586 }
01587 
01588 /*
01589  *  call-seq:
01590  *    Module.new                  -> mod
01591  *    Module.new {|mod| block }   -> mod
01592  *
01593  *  Creates a new anonymous module. If a block is given, it is passed
01594  *  the module object, and the block is evaluated in the context of this
01595  *  module using <code>module_eval</code>.
01596  *
01597  *     fred = Module.new do
01598  *       def meth1
01599  *         "hello"
01600  *       end
01601  *       def meth2
01602  *         "bye"
01603  *       end
01604  *     end
01605  *     a = "my string"
01606  *     a.extend(fred)   #=> "my string"
01607  *     a.meth1          #=> "hello"
01608  *     a.meth2          #=> "bye"
01609  *
01610  *  Assign the module to a constant (name starting uppercase) if you
01611  *  want to treat it like a regular module.
01612  */
01613 
01614 static VALUE
01615 rb_mod_initialize(VALUE module)
01616 {
01617     if (rb_block_given_p()) {
01618         rb_mod_module_exec(1, &module, module);
01619     }
01620     return Qnil;
01621 }
01622 
01623 /*
01624  *  call-seq:
01625  *     Class.new(super_class=Object)               -> a_class
01626  *     Class.new(super_class=Object) { |mod| ... } -> a_class
01627  *
01628  *  Creates a new anonymous (unnamed) class with the given superclass
01629  *  (or <code>Object</code> if no parameter is given). You can give a
01630  *  class a name by assigning the class object to a constant.
01631  *
01632  *  If a block is given, it is passed the class object, and the block
01633  *  is evaluated in the context of this class using
01634  *  <code>class_eval</code>.
01635  *
01636  *     fred = Class.new do
01637  *       def meth1
01638  *         "hello"
01639  *       end
01640  *       def meth2
01641  *         "bye"
01642  *       end
01643  *     end
01644  *
01645  *     a = fred.new     #=> #<#<Class:0x100381890>:0x100376b98>
01646  *     a.meth1          #=> "hello"
01647  *     a.meth2          #=> "bye"
01648  *
01649  *  Assign the class to a constant (name starting uppercase) if you
01650  *  want to treat it like a regular class.
01651  */
01652 
01653 static VALUE
01654 rb_class_initialize(int argc, VALUE *argv, VALUE klass)
01655 {
01656     VALUE super;
01657 
01658     if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
01659         rb_raise(rb_eTypeError, "already initialized class");
01660     }
01661     if (argc == 0) {
01662         super = rb_cObject;
01663     }
01664     else {
01665         rb_scan_args(argc, argv, "01", &super);
01666         rb_check_inheritable(super);
01667         if (super != rb_cBasicObject && !RCLASS_SUPER(super)) {
01668             rb_raise(rb_eTypeError, "can't inherit uninitialized class");
01669         }
01670     }
01671     RCLASS_SUPER(klass) = super;
01672     rb_make_metaclass(klass, RBASIC(super)->klass);
01673     rb_class_inherited(super, klass);
01674     rb_mod_initialize(klass);
01675 
01676     return klass;
01677 }
01678 
01679 /*
01680  *  call-seq:
01681  *     class.allocate()   ->   obj
01682  *
01683  *  Allocates space for a new object of <i>class</i>'s class and does not
01684  *  call initialize on the new instance. The returned object must be an
01685  *  instance of <i>class</i>.
01686  *
01687  *      klass = Class.new do
01688  *        def initialize(*args)
01689  *          @initialized = true
01690  *        end
01691  *
01692  *        def initialized?
01693  *          @initialized || false
01694  *        end
01695  *      end
01696  *
01697  *      klass.allocate.initialized? #=> false
01698  *
01699  */
01700 
01701 VALUE
01702 rb_obj_alloc(VALUE klass)
01703 {
01704     VALUE obj;
01705     rb_alloc_func_t allocator;
01706 
01707     if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
01708         rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
01709     }
01710     if (FL_TEST(klass, FL_SINGLETON)) {
01711         rb_raise(rb_eTypeError, "can't create instance of singleton class");
01712     }
01713     allocator = rb_get_alloc_func(klass);
01714     if (!allocator) {
01715         rb_raise(rb_eTypeError, "allocator undefined for %"PRIsVALUE,
01716                  klass);
01717     }
01718 
01719 #if !defined(DTRACE_PROBES_DISABLED) || !DTRACE_PROBES_DISABLED
01720     if (RUBY_DTRACE_OBJECT_CREATE_ENABLED()) {
01721         const char * file = rb_sourcefile();
01722         RUBY_DTRACE_OBJECT_CREATE(rb_class2name(klass),
01723                                   file ? file : "",
01724                                   rb_sourceline());
01725     }
01726 #endif
01727 
01728     obj = (*allocator)(klass);
01729 
01730     if (rb_obj_class(obj) != rb_class_real(klass)) {
01731         rb_raise(rb_eTypeError, "wrong instance allocation");
01732     }
01733     return obj;
01734 }
01735 
01736 static VALUE
01737 rb_class_allocate_instance(VALUE klass)
01738 {
01739     NEWOBJ_OF(obj, struct RObject, klass, T_OBJECT);
01740     return (VALUE)obj;
01741 }
01742 
01743 /*
01744  *  call-seq:
01745  *     class.new(args, ...)    ->  obj
01746  *
01747  *  Calls <code>allocate</code> to create a new object of
01748  *  <i>class</i>'s class, then invokes that object's
01749  *  <code>initialize</code> method, passing it <i>args</i>.
01750  *  This is the method that ends up getting called whenever
01751  *  an object is constructed using .new.
01752  *
01753  */
01754 
01755 VALUE
01756 rb_class_new_instance(int argc, VALUE *argv, VALUE klass)
01757 {
01758     VALUE obj;
01759 
01760     obj = rb_obj_alloc(klass);
01761     rb_obj_call_init(obj, argc, argv);
01762 
01763     return obj;
01764 }
01765 
01766 /*
01767  *  call-seq:
01768  *     class.superclass -> a_super_class or nil
01769  *
01770  *  Returns the superclass of <i>class</i>, or <code>nil</code>.
01771  *
01772  *     File.superclass          #=> IO
01773  *     IO.superclass            #=> Object
01774  *     Object.superclass        #=> BasicObject
01775  *     class Foo; end
01776  *     class Bar < Foo; end
01777  *     Bar.superclass           #=> Foo
01778  *
01779  *  returns nil when the given class hasn't a parent class:
01780  *
01781  *     BasicObject.superclass   #=> nil
01782  *
01783  */
01784 
01785 VALUE
01786 rb_class_superclass(VALUE klass)
01787 {
01788     VALUE super = RCLASS_SUPER(klass);
01789 
01790     if (!super) {
01791         if (klass == rb_cBasicObject) return Qnil;
01792         rb_raise(rb_eTypeError, "uninitialized class");
01793     }
01794     while (RB_TYPE_P(super, T_ICLASS)) {
01795         super = RCLASS_SUPER(super);
01796     }
01797     if (!super) {
01798         return Qnil;
01799     }
01800     return super;
01801 }
01802 
01803 VALUE
01804 rb_class_get_superclass(VALUE klass)
01805 {
01806     return RCLASS_SUPER(klass);
01807 }
01808 
01809 /*
01810  *  call-seq:
01811  *     attr_reader(symbol, ...)    -> nil
01812  *     attr(symbol, ...)             -> nil
01813  *
01814  *  Creates instance variables and corresponding methods that return the
01815  *  value of each instance variable. Equivalent to calling
01816  *  ``<code>attr</code><i>:name</i>'' on each name in turn.
01817  */
01818 
01819 static VALUE
01820 rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
01821 {
01822     int i;
01823 
01824     for (i=0; i<argc; i++) {
01825         rb_attr(klass, rb_to_id(argv[i]), TRUE, FALSE, TRUE);
01826     }
01827     return Qnil;
01828 }
01829 
01830 VALUE
01831 rb_mod_attr(int argc, VALUE *argv, VALUE klass)
01832 {
01833     if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
01834         rb_warning("optional boolean argument is obsoleted");
01835         rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), TRUE);
01836         return Qnil;
01837     }
01838     return rb_mod_attr_reader(argc, argv, klass);
01839 }
01840 
01841 /*
01842  *  call-seq:
01843  *      attr_writer(symbol, ...)    -> nil
01844  *
01845  *  Creates an accessor method to allow assignment to the attribute
01846  *  <i>symbol</i><code>.id2name</code>.
01847  */
01848 
01849 static VALUE
01850 rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
01851 {
01852     int i;
01853 
01854     for (i=0; i<argc; i++) {
01855         rb_attr(klass, rb_to_id(argv[i]), FALSE, TRUE, TRUE);
01856     }
01857     return Qnil;
01858 }
01859 
01860 /*
01861  *  call-seq:
01862  *     attr_accessor(symbol, ...)    -> nil
01863  *
01864  *  Defines a named attribute for this module, where the name is
01865  *  <i>symbol.</i><code>id2name</code>, creating an instance variable
01866  *  (<code>@name</code>) and a corresponding access method to read it.
01867  *  Also creates a method called <code>name=</code> to set the attribute.
01868  *
01869  *     module Mod
01870  *       attr_accessor(:one, :two)
01871  *     end
01872  *     Mod.instance_methods.sort   #=> [:one, :one=, :two, :two=]
01873  */
01874 
01875 static VALUE
01876 rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
01877 {
01878     int i;
01879 
01880     for (i=0; i<argc; i++) {
01881         rb_attr(klass, rb_to_id(argv[i]), TRUE, TRUE, TRUE);
01882     }
01883     return Qnil;
01884 }
01885 
01886 /*
01887  *  call-seq:
01888  *     mod.const_get(sym, inherit=true)    -> obj
01889  *     mod.const_get(str, inherit=true)    -> obj
01890  *
01891  *  Checks for a constant with the given name in <i>mod</i>
01892  *  If +inherit+ is set, the lookup will also search
01893  *  the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
01894  *
01895  *  The value of the constant is returned if a definition is found,
01896  *  otherwise a +NameError+ is raised.
01897  *
01898  *     Math.const_get(:PI)   #=> 3.14159265358979
01899  *
01900  *  This method will recursively look up constant names if a namespaced
01901  *  class name is provided.  For example:
01902  *
01903  *     module Foo; class Bar; end end
01904  *     Object.const_get 'Foo::Bar'
01905  *
01906  *  The +inherit+ flag is respected on each lookup.  For example:
01907  *
01908  *     module Foo
01909  *       class Bar
01910  *         VAL = 10
01911  *       end
01912  *
01913  *       class Baz < Bar; end
01914  *     end
01915  *
01916  *     Object.const_get 'Foo::Baz::VAL'         # => 10
01917  *     Object.const_get 'Foo::Baz::VAL', false  # => NameError
01918  */
01919 
01920 static VALUE
01921 rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
01922 {
01923     VALUE name, recur;
01924     rb_encoding *enc;
01925     const char *pbeg, *p, *path, *pend;
01926     ID id;
01927     int nestable = 1;
01928 
01929     if (argc == 1) {
01930         name = argv[0];
01931         recur = Qtrue;
01932     }
01933     else {
01934         rb_scan_args(argc, argv, "11", &name, &recur);
01935     }
01936 
01937     if (SYMBOL_P(name)) {
01938         name = rb_sym_to_s(name);
01939         nestable = 0;
01940     }
01941 
01942     name = rb_check_string_type(name);
01943     Check_Type(name, T_STRING);
01944 
01945     enc = rb_enc_get(name);
01946     path = RSTRING_PTR(name);
01947 
01948     if (!rb_enc_asciicompat(enc)) {
01949         rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
01950     }
01951 
01952     pbeg = p = path;
01953     pend = path + RSTRING_LEN(name);
01954 
01955     if (p >= pend || !*p) {
01956       wrong_name:
01957         rb_raise(rb_eNameError, "wrong constant name %"PRIsVALUE,
01958                  QUOTE(name));
01959     }
01960 
01961     if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
01962         if (!nestable) goto wrong_name;
01963         mod = rb_cObject;
01964         p += 2;
01965         pbeg = p;
01966     }
01967 
01968     while (p < pend) {
01969         VALUE part;
01970         long len, beglen;
01971 
01972         while (p < pend && *p != ':') p++;
01973 
01974         if (pbeg == p) goto wrong_name;
01975 
01976         id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
01977         beglen = pbeg-path;
01978 
01979         if (p < pend && p[0] == ':') {
01980             if (!nestable) goto wrong_name;
01981             if (p + 2 >= pend || p[1] != ':') goto wrong_name;
01982             p += 2;
01983             pbeg = p;
01984         }
01985 
01986         if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
01987             rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
01988                      QUOTE(name));
01989         }
01990 
01991         if (!id) {
01992             if (!ISUPPER(*pbeg) || !rb_enc_symname2_p(pbeg, len, enc)) {
01993                 part = rb_str_subseq(name, beglen, len);
01994                 rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
01995                                   QUOTE(part));
01996             }
01997             else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
01998                 id = rb_intern3(pbeg, len, enc);
01999             }
02000             else {
02001                 part = rb_str_subseq(name, beglen, len);
02002                 rb_name_error_str(part, "uninitialized constant %"PRIsVALUE"%"PRIsVALUE,
02003                                   rb_str_subseq(name, 0, beglen),
02004                                   QUOTE(part));
02005             }
02006         }
02007         if (!rb_is_const_id(id)) {
02008             rb_name_error(id, "wrong constant name %"PRIsVALUE,
02009                           QUOTE_ID(id));
02010         }
02011         mod = RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
02012     }
02013 
02014     return mod;
02015 }
02016 
02017 /*
02018  *  call-seq:
02019  *     mod.const_set(sym, obj)    -> obj
02020  *
02021  *  Sets the named constant to the given object, returning that object.
02022  *  Creates a new constant if no constant with the given name previously
02023  *  existed.
02024  *
02025  *     Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0)   #=> 3.14285714285714
02026  *     Math::HIGH_SCHOOL_PI - Math::PI              #=> 0.00126448926734968
02027  */
02028 
02029 static VALUE
02030 rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
02031 {
02032     ID id = rb_to_id(name);
02033 
02034     if (!rb_is_const_id(id)) {
02035         rb_name_error(id, "wrong constant name %"PRIsVALUE,
02036                       QUOTE_ID(id));
02037     }
02038     rb_const_set(mod, id, value);
02039     return value;
02040 }
02041 
02042 /*
02043  *  call-seq:
02044  *     mod.const_defined?(sym, inherit=true)   -> true or false
02045  *
02046  *  Checks for a constant with the given name in <i>mod</i>
02047  *  If +inherit+ is set, the lookup will also search
02048  *  the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
02049  *
02050  *  Returns whether or not a definition is found:
02051  *
02052  *     Math.const_defined? "PI"   #=> true
02053  *     IO.const_defined? :SYNC   #=> true
02054  *     IO.const_defined? :SYNC, false   #=> false
02055  */
02056 
02057 static VALUE
02058 rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
02059 {
02060     VALUE name, recur;
02061     ID id;
02062 
02063     if (argc == 1) {
02064         name = argv[0];
02065         recur = Qtrue;
02066     }
02067     else {
02068         rb_scan_args(argc, argv, "11", &name, &recur);
02069     }
02070     if (!(id = rb_check_id(&name))) {
02071         if (rb_is_const_name(name)) {
02072             return Qfalse;
02073         }
02074         else {
02075             rb_name_error_str(name, "wrong constant name %"PRIsVALUE,
02076                               QUOTE(name));
02077         }
02078     }
02079     if (!rb_is_const_id(id)) {
02080         rb_name_error(id, "wrong constant name %"PRIsVALUE,
02081                       QUOTE_ID(id));
02082     }
02083     return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
02084 }
02085 
02086 /*
02087  *  call-seq:
02088  *     obj.instance_variable_get(symbol)    -> obj
02089  *
02090  *  Returns the value of the given instance variable, or nil if the
02091  *  instance variable is not set. The <code>@</code> part of the
02092  *  variable name should be included for regular instance
02093  *  variables. Throws a <code>NameError</code> exception if the
02094  *  supplied symbol is not valid as an instance variable name.
02095  *
02096  *     class Fred
02097  *       def initialize(p1, p2)
02098  *         @a, @b = p1, p2
02099  *       end
02100  *     end
02101  *     fred = Fred.new('cat', 99)
02102  *     fred.instance_variable_get(:@a)    #=> "cat"
02103  *     fred.instance_variable_get("@b")   #=> 99
02104  */
02105 
02106 static VALUE
02107 rb_obj_ivar_get(VALUE obj, VALUE iv)
02108 {
02109     ID id = rb_check_id(&iv);
02110 
02111     if (!id) {
02112         if (rb_is_instance_name(iv)) {
02113             return Qnil;
02114         }
02115         else {
02116             rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02117                               QUOTE(iv));
02118         }
02119     }
02120     if (!rb_is_instance_id(id)) {
02121         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02122                       QUOTE_ID(id));
02123     }
02124     return rb_ivar_get(obj, id);
02125 }
02126 
02127 /*
02128  *  call-seq:
02129  *     obj.instance_variable_set(symbol, obj)    -> obj
02130  *
02131  *  Sets the instance variable names by <i>symbol</i> to
02132  *  <i>object</i>, thereby frustrating the efforts of the class's
02133  *  author to attempt to provide proper encapsulation. The variable
02134  *  did not have to exist prior to this call.
02135  *
02136  *     class Fred
02137  *       def initialize(p1, p2)
02138  *         @a, @b = p1, p2
02139  *       end
02140  *     end
02141  *     fred = Fred.new('cat', 99)
02142  *     fred.instance_variable_set(:@a, 'dog')   #=> "dog"
02143  *     fred.instance_variable_set(:@c, 'cat')   #=> "cat"
02144  *     fred.inspect                             #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
02145  */
02146 
02147 static VALUE
02148 rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
02149 {
02150     ID id = rb_to_id(iv);
02151 
02152     if (!rb_is_instance_id(id)) {
02153         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02154                       QUOTE_ID(id));
02155     }
02156     return rb_ivar_set(obj, id, val);
02157 }
02158 
02159 /*
02160  *  call-seq:
02161  *     obj.instance_variable_defined?(symbol)    -> true or false
02162  *
02163  *  Returns <code>true</code> if the given instance variable is
02164  *  defined in <i>obj</i>.
02165  *
02166  *     class Fred
02167  *       def initialize(p1, p2)
02168  *         @a, @b = p1, p2
02169  *       end
02170  *     end
02171  *     fred = Fred.new('cat', 99)
02172  *     fred.instance_variable_defined?(:@a)    #=> true
02173  *     fred.instance_variable_defined?("@b")   #=> true
02174  *     fred.instance_variable_defined?("@c")   #=> false
02175  */
02176 
02177 static VALUE
02178 rb_obj_ivar_defined(VALUE obj, VALUE iv)
02179 {
02180     ID id = rb_check_id(&iv);
02181 
02182     if (!id) {
02183         if (rb_is_instance_name(iv)) {
02184             return Qfalse;
02185         }
02186         else {
02187             rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02188                               QUOTE(iv));
02189         }
02190     }
02191     if (!rb_is_instance_id(id)) {
02192         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02193                       QUOTE_ID(id));
02194     }
02195     return rb_ivar_defined(obj, id);
02196 }
02197 
02198 /*
02199  *  call-seq:
02200  *     mod.class_variable_get(symbol)    -> obj
02201  *
02202  *  Returns the value of the given class variable (or throws a
02203  *  <code>NameError</code> exception). The <code>@@</code> part of the
02204  *  variable name should be included for regular class variables
02205  *
02206  *     class Fred
02207  *       @@foo = 99
02208  *     end
02209  *     Fred.class_variable_get(:@@foo)     #=> 99
02210  */
02211 
02212 static VALUE
02213 rb_mod_cvar_get(VALUE obj, VALUE iv)
02214 {
02215     ID id = rb_check_id(&iv);
02216 
02217     if (!id) {
02218         if (rb_is_class_name(iv)) {
02219             rb_name_error_str(iv, "uninitialized class variable %"PRIsVALUE" in %"PRIsVALUE"",
02220                               iv, rb_class_name(obj));
02221         }
02222         else {
02223             rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name",
02224                               QUOTE(iv));
02225         }
02226     }
02227     if (!rb_is_class_id(id)) {
02228         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
02229                       QUOTE_ID(id));
02230     }
02231     return rb_cvar_get(obj, id);
02232 }
02233 
02234 /*
02235  *  call-seq:
02236  *     obj.class_variable_set(symbol, obj)    -> obj
02237  *
02238  *  Sets the class variable names by <i>symbol</i> to
02239  *  <i>object</i>.
02240  *
02241  *     class Fred
02242  *       @@foo = 99
02243  *       def foo
02244  *         @@foo
02245  *       end
02246  *     end
02247  *     Fred.class_variable_set(:@@foo, 101)     #=> 101
02248  *     Fred.new.foo                             #=> 101
02249  */
02250 
02251 static VALUE
02252 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
02253 {
02254     ID id = rb_to_id(iv);
02255 
02256     if (!rb_is_class_id(id)) {
02257         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
02258                       QUOTE_ID(id));
02259     }
02260     rb_cvar_set(obj, id, val);
02261     return val;
02262 }
02263 
02264 /*
02265  *  call-seq:
02266  *     obj.class_variable_defined?(symbol)    -> true or false
02267  *
02268  *  Returns <code>true</code> if the given class variable is defined
02269  *  in <i>obj</i>.
02270  *
02271  *     class Fred
02272  *       @@foo = 99
02273  *     end
02274  *     Fred.class_variable_defined?(:@@foo)    #=> true
02275  *     Fred.class_variable_defined?(:@@bar)    #=> false
02276  */
02277 
02278 static VALUE
02279 rb_mod_cvar_defined(VALUE obj, VALUE iv)
02280 {
02281     ID id = rb_check_id(&iv);
02282 
02283     if (!id) {
02284         if (rb_is_class_name(iv)) {
02285             return Qfalse;
02286         }
02287         else {
02288             rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name",
02289                               QUOTE(iv));
02290         }
02291     }
02292     if (!rb_is_class_id(id)) {
02293         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
02294                       QUOTE_ID(id));
02295     }
02296     return rb_cvar_defined(obj, id);
02297 }
02298 
02299 static struct conv_method_tbl {
02300     const char *method;
02301     ID id;
02302 } conv_method_names[] = {
02303     {"to_int", 0},
02304     {"to_ary", 0},
02305     {"to_str", 0},
02306     {"to_sym", 0},
02307     {"to_hash", 0},
02308     {"to_proc", 0},
02309     {"to_io", 0},
02310     {"to_a", 0},
02311     {"to_s", 0},
02312     {NULL, 0}
02313 };
02314 #define IMPLICIT_CONVERSIONS 7
02315 
02316 static VALUE
02317 convert_type(VALUE val, const char *tname, const char *method, int raise)
02318 {
02319     ID m = 0;
02320     int i;
02321     VALUE r;
02322 
02323     for (i=0; conv_method_names[i].method; i++) {
02324         if (conv_method_names[i].method[0] == method[0] &&
02325             strcmp(conv_method_names[i].method, method) == 0) {
02326             m = conv_method_names[i].id;
02327             break;
02328         }
02329     }
02330     if (!m) m = rb_intern(method);
02331     r = rb_check_funcall(val, m, 0, 0);
02332     if (r == Qundef) {
02333         if (raise) {
02334             rb_raise(rb_eTypeError, i < IMPLICIT_CONVERSIONS
02335                 ? "no implicit conversion of %s into %s"
02336                 : "can't convert %s into %s",
02337                      NIL_P(val) ? "nil" :
02338                      val == Qtrue ? "true" :
02339                      val == Qfalse ? "false" :
02340                      rb_obj_classname(val),
02341                      tname);
02342         }
02343         return Qnil;
02344     }
02345     return r;
02346 }
02347 
02348 VALUE
02349 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
02350 {
02351     VALUE v;
02352 
02353     if (TYPE(val) == type) return val;
02354     v = convert_type(val, tname, method, TRUE);
02355     if (TYPE(v) != type) {
02356         const char *cname = rb_obj_classname(val);
02357         rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
02358                  cname, tname, cname, method, rb_obj_classname(v));
02359     }
02360     return v;
02361 }
02362 
02363 VALUE
02364 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
02365 {
02366     VALUE v;
02367 
02368     /* always convert T_DATA */
02369     if (TYPE(val) == type && type != T_DATA) return val;
02370     v = convert_type(val, tname, method, FALSE);
02371     if (NIL_P(v)) return Qnil;
02372     if (TYPE(v) != type) {
02373         const char *cname = rb_obj_classname(val);
02374         rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
02375                  cname, tname, cname, method, rb_obj_classname(v));
02376     }
02377     return v;
02378 }
02379 
02380 
02381 static VALUE
02382 rb_to_integer(VALUE val, const char *method)
02383 {
02384     VALUE v;
02385 
02386     if (FIXNUM_P(val)) return val;
02387     if (RB_TYPE_P(val, T_BIGNUM)) return val;
02388     v = convert_type(val, "Integer", method, TRUE);
02389     if (!rb_obj_is_kind_of(v, rb_cInteger)) {
02390         const char *cname = rb_obj_classname(val);
02391         rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)",
02392                  cname, cname, method, rb_obj_classname(v));
02393     }
02394     return v;
02395 }
02396 
02397 VALUE
02398 rb_check_to_integer(VALUE val, const char *method)
02399 {
02400     VALUE v;
02401 
02402     if (FIXNUM_P(val)) return val;
02403     if (RB_TYPE_P(val, T_BIGNUM)) return val;
02404     v = convert_type(val, "Integer", method, FALSE);
02405     if (!rb_obj_is_kind_of(v, rb_cInteger)) {
02406         return Qnil;
02407     }
02408     return v;
02409 }
02410 
02411 VALUE
02412 rb_to_int(VALUE val)
02413 {
02414     return rb_to_integer(val, "to_int");
02415 }
02416 
02417 VALUE
02418 rb_check_to_int(VALUE val)
02419 {
02420     return rb_check_to_integer(val, "to_int");
02421 }
02422 
02423 static VALUE
02424 rb_convert_to_integer(VALUE val, int base)
02425 {
02426     VALUE tmp;
02427 
02428     switch (TYPE(val)) {
02429       case T_FLOAT:
02430         if (base != 0) goto arg_error;
02431         if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX
02432             && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) {
02433             break;
02434         }
02435         return rb_dbl2big(RFLOAT_VALUE(val));
02436 
02437       case T_FIXNUM:
02438       case T_BIGNUM:
02439         if (base != 0) goto arg_error;
02440         return val;
02441 
02442       case T_STRING:
02443       string_conv:
02444         return rb_str_to_inum(val, base, TRUE);
02445 
02446       case T_NIL:
02447         if (base != 0) goto arg_error;
02448         rb_raise(rb_eTypeError, "can't convert nil into Integer");
02449         break;
02450 
02451       default:
02452         break;
02453     }
02454     if (base != 0) {
02455         tmp = rb_check_string_type(val);
02456         if (!NIL_P(tmp)) goto string_conv;
02457       arg_error:
02458         rb_raise(rb_eArgError, "base specified for non string value");
02459     }
02460     tmp = convert_type(val, "Integer", "to_int", FALSE);
02461     if (NIL_P(tmp)) {
02462         return rb_to_integer(val, "to_i");
02463     }
02464     return tmp;
02465 
02466 }
02467 
02468 VALUE
02469 rb_Integer(VALUE val)
02470 {
02471     return rb_convert_to_integer(val, 0);
02472 }
02473 
02474 /*
02475  *  call-seq:
02476  *     Integer(arg,base=0)    -> integer
02477  *
02478  *  Converts <i>arg</i> to a <code>Fixnum</code> or <code>Bignum</code>.
02479  *  Numeric types are converted directly (with floating point numbers
02480  *  being truncated).    <i>base</i> (0, or between 2 and 36) is a base for
02481  *  integer string representation.  If <i>arg</i> is a <code>String</code>,
02482  *  when <i>base</i> is omitted or equals to zero, radix indicators
02483  *  (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
02484  *  In any case, strings should be strictly conformed to numeric
02485  *  representation. This behavior is different from that of
02486  *  <code>String#to_i</code>.  Non string values will be converted using
02487  *  <code>to_int</code>, and <code>to_i</code>.
02488  *
02489  *     Integer(123.999)    #=> 123
02490  *     Integer("0x1a")     #=> 26
02491  *     Integer(Time.new)   #=> 1204973019
02492  *     Integer("0930", 10) #=> 930
02493  *     Integer("111", 2)   #=> 7
02494  */
02495 
02496 static VALUE
02497 rb_f_integer(int argc, VALUE *argv, VALUE obj)
02498 {
02499     VALUE arg = Qnil;
02500     int base = 0;
02501 
02502     switch (argc) {
02503       case 2:
02504         base = NUM2INT(argv[1]);
02505       case 1:
02506         arg = argv[0];
02507         break;
02508       default:
02509         /* should cause ArgumentError */
02510         rb_scan_args(argc, argv, "11", NULL, NULL);
02511     }
02512     return rb_convert_to_integer(arg, base);
02513 }
02514 
02515 double
02516 rb_cstr_to_dbl(const char *p, int badcheck)
02517 {
02518     const char *q;
02519     char *end;
02520     double d;
02521     const char *ellipsis = "";
02522     int w;
02523     enum {max_width = 20};
02524 #define OutOfRange() ((end - p > max_width) ? \
02525                       (w = max_width, ellipsis = "...") : \
02526                       (w = (int)(end - p), ellipsis = ""))
02527 
02528     if (!p) return 0.0;
02529     q = p;
02530     while (ISSPACE(*p)) p++;
02531 
02532     if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
02533         return 0.0;
02534     }
02535 
02536     d = strtod(p, &end);
02537     if (errno == ERANGE) {
02538         OutOfRange();
02539         rb_warning("Float %.*s%s out of range", w, p, ellipsis);
02540         errno = 0;
02541     }
02542     if (p == end) {
02543         if (badcheck) {
02544           bad:
02545             rb_invalid_str(q, "Float()");
02546         }
02547         return d;
02548     }
02549     if (*end) {
02550         char buf[DBL_DIG * 4 + 10];
02551         char *n = buf;
02552         char *e = buf + sizeof(buf) - 1;
02553         char prev = 0;
02554 
02555         while (p < end && n < e) prev = *n++ = *p++;
02556         while (*p) {
02557             if (*p == '_') {
02558                 /* remove underscores between digits */
02559                 if (badcheck) {
02560                     if (n == buf || !ISDIGIT(prev)) goto bad;
02561                     ++p;
02562                     if (!ISDIGIT(*p)) goto bad;
02563                 }
02564                 else {
02565                     while (*++p == '_');
02566                     continue;
02567                 }
02568             }
02569             prev = *p++;
02570             if (n < e) *n++ = prev;
02571         }
02572         *n = '\0';
02573         p = buf;
02574 
02575         if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
02576             return 0.0;
02577         }
02578 
02579         d = strtod(p, &end);
02580         if (errno == ERANGE) {
02581             OutOfRange();
02582             rb_warning("Float %.*s%s out of range", w, p, ellipsis);
02583             errno = 0;
02584         }
02585         if (badcheck) {
02586             if (!end || p == end) goto bad;
02587             while (*end && ISSPACE(*end)) end++;
02588             if (*end) goto bad;
02589         }
02590     }
02591     if (errno == ERANGE) {
02592         errno = 0;
02593         OutOfRange();
02594         rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
02595     }
02596     return d;
02597 }
02598 
02599 double
02600 rb_str_to_dbl(VALUE str, int badcheck)
02601 {
02602     char *s;
02603     long len;
02604     double ret;
02605     VALUE v = 0;
02606 
02607     StringValue(str);
02608     s = RSTRING_PTR(str);
02609     len = RSTRING_LEN(str);
02610     if (s) {
02611         if (badcheck && memchr(s, '\0', len)) {
02612             rb_raise(rb_eArgError, "string for Float contains null byte");
02613         }
02614         if (s[len]) {           /* no sentinel somehow */
02615             char *p =  ALLOCV(v, len);
02616             MEMCPY(p, s, char, len);
02617             p[len] = '\0';
02618             s = p;
02619         }
02620     }
02621     ret = rb_cstr_to_dbl(s, badcheck);
02622     if (v)
02623         ALLOCV_END(v);
02624     return ret;
02625 }
02626 
02627 VALUE
02628 rb_Float(VALUE val)
02629 {
02630     switch (TYPE(val)) {
02631       case T_FIXNUM:
02632         return DBL2NUM((double)FIX2LONG(val));
02633 
02634       case T_FLOAT:
02635         return val;
02636 
02637       case T_BIGNUM:
02638         return DBL2NUM(rb_big2dbl(val));
02639 
02640       case T_STRING:
02641         return DBL2NUM(rb_str_to_dbl(val, TRUE));
02642 
02643       case T_NIL:
02644         rb_raise(rb_eTypeError, "can't convert nil into Float");
02645         break;
02646 
02647       default:
02648         return rb_convert_type(val, T_FLOAT, "Float", "to_f");
02649     }
02650 
02651     UNREACHABLE;
02652 }
02653 
02654 /*
02655  *  call-seq:
02656  *     Float(arg)    -> float
02657  *
02658  *  Returns <i>arg</i> converted to a float. Numeric types are converted
02659  *  directly, the rest are converted using <i>arg</i>.to_f. As of Ruby
02660  *  1.8, converting <code>nil</code> generates a <code>TypeError</code>.
02661  *
02662  *     Float(1)           #=> 1.0
02663  *     Float("123.456")   #=> 123.456
02664  */
02665 
02666 static VALUE
02667 rb_f_float(VALUE obj, VALUE arg)
02668 {
02669     return rb_Float(arg);
02670 }
02671 
02672 VALUE
02673 rb_to_float(VALUE val)
02674 {
02675     if (RB_TYPE_P(val, T_FLOAT)) return val;
02676     if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
02677         rb_raise(rb_eTypeError, "can't convert %s into Float",
02678                  NIL_P(val) ? "nil" :
02679                  val == Qtrue ? "true" :
02680                  val == Qfalse ? "false" :
02681                  rb_obj_classname(val));
02682     }
02683     return rb_convert_type(val, T_FLOAT, "Float", "to_f");
02684 }
02685 
02686 VALUE
02687 rb_check_to_float(VALUE val)
02688 {
02689     if (RB_TYPE_P(val, T_FLOAT)) return val;
02690     if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
02691         return Qnil;
02692     }
02693     return rb_check_convert_type(val, T_FLOAT, "Float", "to_f");
02694 }
02695 
02696 double
02697 rb_num2dbl(VALUE val)
02698 {
02699     switch (TYPE(val)) {
02700       case T_FLOAT:
02701         return RFLOAT_VALUE(val);
02702 
02703       case T_STRING:
02704         rb_raise(rb_eTypeError, "no implicit conversion to float from string");
02705         break;
02706 
02707       case T_NIL:
02708         rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
02709         break;
02710 
02711       default:
02712         break;
02713     }
02714 
02715     return RFLOAT_VALUE(rb_Float(val));
02716 }
02717 
02718 VALUE
02719 rb_String(VALUE val)
02720 {
02721     VALUE tmp = rb_check_string_type(val);
02722     if (NIL_P(tmp))
02723         tmp = rb_convert_type(val, T_STRING, "String", "to_s");
02724     return tmp;
02725 }
02726 
02727 
02728 /*
02729  *  call-seq:
02730  *     String(arg)   -> string
02731  *
02732  *  Converts <i>arg</i> to a <code>String</code> by calling its
02733  *  <code>to_s</code> method.
02734  *
02735  *     String(self)        #=> "main"
02736  *     String(self.class)  #=> "Object"
02737  *     String(123456)      #=> "123456"
02738  */
02739 
02740 static VALUE
02741 rb_f_string(VALUE obj, VALUE arg)
02742 {
02743     return rb_String(arg);
02744 }
02745 
02746 VALUE
02747 rb_Array(VALUE val)
02748 {
02749     VALUE tmp = rb_check_array_type(val);
02750 
02751     if (NIL_P(tmp)) {
02752         tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
02753         if (NIL_P(tmp)) {
02754             return rb_ary_new3(1, val);
02755         }
02756     }
02757     return tmp;
02758 }
02759 
02760 /*
02761  *  call-seq:
02762  *     Array(arg)    -> array
02763  *
02764  *  Returns +arg+ as an Array.
02765  *
02766  *  First tries to call Array#to_ary on +arg+, then Array#to_a.
02767  *
02768  *     Array(1..5)   #=> [1, 2, 3, 4, 5]
02769  */
02770 
02771 static VALUE
02772 rb_f_array(VALUE obj, VALUE arg)
02773 {
02774     return rb_Array(arg);
02775 }
02776 
02777 VALUE
02778 rb_Hash(VALUE val)
02779 {
02780     VALUE tmp;
02781 
02782     if (NIL_P(val)) return rb_hash_new();
02783     tmp = rb_check_hash_type(val);
02784     if (NIL_P(tmp)) {
02785         if (RB_TYPE_P(val, T_ARRAY) && RARRAY_LEN(val) == 0)
02786             return rb_hash_new();
02787         rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
02788     }
02789     return tmp;
02790 }
02791 
02792 /*
02793  *  call-seq:
02794  *     Hash(arg)    -> hash
02795  *
02796  *  Converts <i>arg</i> to a <code>Hash</code> by calling
02797  *  <i>arg</i><code>.to_hash</code>. Returns an empty <code>Hash</code> when
02798  *  <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>.
02799  *
02800  *     Hash([])          #=> {}
02801  *     Hash(nil)         #=> nil
02802  *     Hash(key: :value) #=> {:key => :value}
02803  *     Hash([1, 2, 3])   #=> TypeError
02804  */
02805 
02806 static VALUE
02807 rb_f_hash(VALUE obj, VALUE arg)
02808 {
02809     return rb_Hash(arg);
02810 }
02811 
02812 /*
02813  *  Document-class: Class
02814  *
02815  *  Classes in Ruby are first-class objects---each is an instance of
02816  *  class <code>Class</code>.
02817  *
02818  *  Typically, you create a new class by using:
02819  *
02820  *    class Name
02821  *     # some class describing the class behavior
02822  *    end
02823  *
02824  *  When a new class is created, an object of type Class is initialized and
02825  *  assigned to a global constant (<code>Name</code> in this case).
02826  *
02827  *  When <code>Name.new</code> is called to create a new object, the
02828  *  <code>new</code> method in <code>Class</code> is run by default.
02829  *  This can be demonstrated by overriding <code>new</code> in
02830  *  <code>Class</code>:
02831  *
02832  *     class Class
02833  *        alias oldNew  new
02834  *        def new(*args)
02835  *          print "Creating a new ", self.name, "\n"
02836  *          oldNew(*args)
02837  *        end
02838  *      end
02839  *
02840  *
02841  *      class Name
02842  *      end
02843  *
02844  *
02845  *      n = Name.new
02846  *
02847  *  <em>produces:</em>
02848  *
02849  *     Creating a new Name
02850  *
02851  *  Classes, modules, and objects are interrelated. In the diagram
02852  *  that follows, the vertical arrows represent inheritance, and the
02853  *  parentheses meta-classes. All metaclasses are instances
02854  *  of the class `Class'.
02855  *                             +---------+             +-...
02856  *                             |         |             |
02857  *             BasicObject-----|-->(BasicObject)-------|-...
02858  *                 ^           |         ^             |
02859  *                 |           |         |             |
02860  *              Object---------|----->(Object)---------|-...
02861  *                 ^           |         ^             |
02862  *                 |           |         |             |
02863  *                 +-------+   |         +--------+    |
02864  *                 |       |   |         |        |    |
02865  *                 |    Module-|---------|--->(Module)-|-...
02866  *                 |       ^   |         |        ^    |
02867  *                 |       |   |         |        |    |
02868  *                 |     Class-|---------|---->(Class)-|-...
02869  *                 |       ^   |         |        ^    |
02870  *                 |       +---+         |        +----+
02871  *                 |                     |
02872  *    obj--->OtherClass---------->(OtherClass)-----------...
02873  *
02874  */
02875 
02876 
02895 /*  Document-class: BasicObject
02896  *
02897  *  BasicObject is the parent class of all classes in Ruby.  It's an explicit
02898  *  blank class.
02899  *
02900  *  BasicObject can be used for creating object hierarchies independent of
02901  *  Ruby's object hierarchy, proxy objects like the Delegator class, or other
02902  *  uses where namespace pollution from Ruby's methods and classes must be
02903  *  avoided.
02904  *
02905  *  To avoid polluting BasicObject for other users an appropriately named
02906  *  subclass of BasicObject should be created instead of directly modifying
02907  *  BasicObject:
02908  *
02909  *    class MyObjectSystem < BasicObject
02910  *    end
02911  *
02912  *  BasicObject does not include Kernel (for methods like +puts+) and
02913  *  BasicObject is outside of the namespace of the standard library so common
02914  *  classes will not be found without a using a full class path.
02915  *
02916  *  A variety of strategies can be used to provide useful portions of the
02917  *  standard library to subclasses of BasicObject.  A subclass could
02918  *  <code>include Kernel</code> to obtain +puts+, +exit+, etc.  A custom
02919  *  Kernel-like module could be created and included or delegation can be used
02920  *  via #method_missing:
02921  *
02922  *    class MyObjectSystem < BasicObject
02923  *      DELEGATE = [:puts, :p]
02924  *
02925  *      def method_missing(name, *args, &block)
02926  *        super unless DELEGATE.include? name
02927  *        ::Kernel.send(name, *args, &block)
02928  *      end
02929  *
02930  *      def respond_to_missing?(name, include_private = false)
02931  *        DELEGATE.include?(name) or super
02932  *      end
02933  *    end
02934  *
02935  *  Access to classes and modules from the Ruby standard library can be
02936  *  obtained in a BasicObject subclass by referencing the desired constant
02937  *  from the root like <code>::File</code> or <code>::Enumerator</code>.
02938  *  Like #method_missing, #const_missing can be used to delegate constant
02939  *  lookup to +Object+:
02940  *
02941  *    class MyObjectSystem < BasicObject
02942  *      def self.const_missing(name)
02943  *        ::Object.const_get(name)
02944  *      end
02945  *    end
02946  */
02947 
02948 /*  Document-class: Object
02949  *
02950  *  Object is the default root of all Ruby objects.  Object inherits from
02951  *  BasicObject which allows creating alternate object hierarchies.  Methods
02952  *  on object are available to all classes unless explicitly overridden.
02953  *
02954  *  Object mixes in the Kernel module, making the built-in kernel functions
02955  *  globally accessible.  Although the instance methods of Object are defined
02956  *  by the Kernel module, we have chosen to document them here for clarity.
02957  *
02958  *  When referencing constants in classes inheriting from Object you do not
02959  *  need to use the full namespace.  For example, referencing +File+ inside
02960  *  +YourClass+ will find the top-level File class.
02961  *
02962  *  In the descriptions of Object's methods, the parameter <i>symbol</i> refers
02963  *  to a symbol, which is either a quoted string or a Symbol (such as
02964  *  <code>:name</code>).
02965  */
02966 
02967 void
02968 Init_Object(void)
02969 {
02970     int i;
02971 
02972     Init_class_hierarchy();
02973 
02974 #if 0
02975     // teach RDoc about these classes
02976     rb_cBasicObject = rb_define_class("BasicObject", Qnil);
02977     rb_cObject = rb_define_class("Object", rb_cBasicObject);
02978     rb_cModule = rb_define_class("Module", rb_cObject);
02979     rb_cClass =  rb_define_class("Class",  rb_cModule);
02980 #endif
02981 
02982 #undef rb_intern
02983 #define rb_intern(str) rb_intern_const(str)
02984 
02985     rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0);
02986     rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
02987     rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
02988     rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
02989     rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
02990     rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);
02991 
02992     rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1);
02993     rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1);
02994     rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
02995 
02996     /* Document-module: Kernel
02997      *
02998      * The Kernel module is included by class Object, so its methods are
02999      * available in every Ruby object.
03000      *
03001      * The Kernel instance methods are documented in class Object while the
03002      * module methods are documented here.  These methods are called without a
03003      * receiver and thus can be called in functional form:
03004      *
03005      *   sprintf "%.1f", 1.234 #=> "1.2"
03006      *
03007      */
03008     rb_mKernel = rb_define_module("Kernel");
03009     rb_include_module(rb_cObject, rb_mKernel);
03010     rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1);
03011     rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1);
03012     rb_define_private_method(rb_cModule, "extended", rb_obj_dummy, 1);
03013     rb_define_private_method(rb_cModule, "prepended", rb_obj_dummy, 1);
03014     rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
03015     rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
03016     rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
03017 
03018     rb_define_method(rb_mKernel, "nil?", rb_false, 0);
03019     rb_define_method(rb_mKernel, "===", rb_equal, 1);
03020     rb_define_method(rb_mKernel, "=~", rb_obj_match, 1);
03021     rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
03022     rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
03023     rb_define_method(rb_mKernel, "hash", rb_obj_hash, 0);
03024     rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
03025 
03026     rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
03027     rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
03028     rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
03029     rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
03030     rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
03031     rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
03032     rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1);
03033 
03034     rb_define_method(rb_mKernel, "taint", rb_obj_taint, 0);
03035     rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
03036     rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
03037     rb_define_method(rb_mKernel, "untrust", rb_obj_untrust, 0);
03038     rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0);
03039     rb_define_method(rb_mKernel, "trust", rb_obj_trust, 0);
03040     rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
03041     rb_define_method(rb_mKernel, "frozen?", rb_obj_frozen_p, 0);
03042 
03043     rb_define_method(rb_mKernel, "to_s", rb_any_to_s, 0);
03044     rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
03045     rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */
03046     rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
03047     rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
03048     rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */
03049     rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */
03050     rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
03051     rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
03052     rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
03053     rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
03054     rb_define_method(rb_mKernel, "remove_instance_variable",
03055                      rb_obj_remove_instance_variable, 1); /* in variable.c */
03056 
03057     rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
03058     rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
03059     rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
03060     rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
03061 
03062     rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */
03063     rb_define_global_function("format", rb_f_sprintf, -1);  /* in sprintf.c */
03064 
03065     rb_define_global_function("Integer", rb_f_integer, -1);
03066     rb_define_global_function("Float", rb_f_float, 1);
03067 
03068     rb_define_global_function("String", rb_f_string, 1);
03069     rb_define_global_function("Array", rb_f_array, 1);
03070     rb_define_global_function("Hash", rb_f_hash, 1);
03071 
03072     rb_cNilClass = rb_define_class("NilClass", rb_cObject);
03073     rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
03074     rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
03075     rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
03076     rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
03077     rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0);
03078     rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
03079     rb_define_method(rb_cNilClass, "&", false_and, 1);
03080     rb_define_method(rb_cNilClass, "|", false_or, 1);
03081     rb_define_method(rb_cNilClass, "^", false_xor, 1);
03082 
03083     rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
03084     rb_undef_alloc_func(rb_cNilClass);
03085     rb_undef_method(CLASS_OF(rb_cNilClass), "new");
03086     /*
03087      * An alias of +nil+
03088      */
03089     rb_define_global_const("NIL", Qnil);
03090 
03091     rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
03092     rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
03093     rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
03094     rb_define_method(rb_cModule, "<=>",  rb_mod_cmp, 1);
03095     rb_define_method(rb_cModule, "<",  rb_mod_lt, 1);
03096     rb_define_method(rb_cModule, "<=", rb_class_inherited_p, 1);
03097     rb_define_method(rb_cModule, ">",  rb_mod_gt, 1);
03098     rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
03099     rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
03100     rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
03101     rb_define_alias(rb_cModule, "inspect", "to_s");
03102     rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
03103     rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
03104     rb_define_method(rb_cModule, "name", rb_mod_name, 0);  /* in variable.c */
03105     rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
03106 
03107     rb_define_private_method(rb_cModule, "attr", rb_mod_attr, -1);
03108     rb_define_private_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
03109     rb_define_private_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
03110     rb_define_private_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
03111 
03112     rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
03113     rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
03114     rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
03115     rb_define_method(rb_cModule, "public_instance_methods",
03116                      rb_class_public_instance_methods, -1);    /* in class.c */
03117     rb_define_method(rb_cModule, "protected_instance_methods",
03118                      rb_class_protected_instance_methods, -1); /* in class.c */
03119     rb_define_method(rb_cModule, "private_instance_methods",
03120                      rb_class_private_instance_methods, -1);   /* in class.c */
03121 
03122     rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
03123     rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
03124     rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
03125     rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
03126     rb_define_private_method(rb_cModule, "remove_const",
03127                              rb_mod_remove_const, 1); /* in variable.c */
03128     rb_define_method(rb_cModule, "const_missing",
03129                      rb_mod_const_missing, 1); /* in variable.c */
03130     rb_define_method(rb_cModule, "class_variables",
03131                      rb_mod_class_variables, -1); /* in variable.c */
03132     rb_define_method(rb_cModule, "remove_class_variable",
03133                      rb_mod_remove_cvar, 1); /* in variable.c */
03134     rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
03135     rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
03136     rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
03137     rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); /* in variable.c */
03138     rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); /* in variable.c */
03139 
03140     rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
03141     rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
03142     rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
03143     rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
03144     rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
03145     rb_undef_method(rb_cClass, "extend_object");
03146     rb_undef_method(rb_cClass, "append_features");
03147 
03148     /*
03149      * Document-class: Data
03150      *
03151      * This is a recommended base class for C extensions using Data_Make_Struct
03152      * or Data_Wrap_Struct, see README.EXT for details.
03153      */
03154     rb_cData = rb_define_class("Data", rb_cObject);
03155     rb_undef_alloc_func(rb_cData);
03156 
03157     rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
03158     rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
03159     rb_define_alias(rb_cTrueClass, "inspect", "to_s");
03160     rb_define_method(rb_cTrueClass, "&", true_and, 1);
03161     rb_define_method(rb_cTrueClass, "|", true_or, 1);
03162     rb_define_method(rb_cTrueClass, "^", true_xor, 1);
03163     rb_undef_alloc_func(rb_cTrueClass);
03164     rb_undef_method(CLASS_OF(rb_cTrueClass), "new");
03165     /*
03166      * An alias of +true+
03167      */
03168     rb_define_global_const("TRUE", Qtrue);
03169 
03170     rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
03171     rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
03172     rb_define_alias(rb_cFalseClass, "inspect", "to_s");
03173     rb_define_method(rb_cFalseClass, "&", false_and, 1);
03174     rb_define_method(rb_cFalseClass, "|", false_or, 1);
03175     rb_define_method(rb_cFalseClass, "^", false_xor, 1);
03176     rb_undef_alloc_func(rb_cFalseClass);
03177     rb_undef_method(CLASS_OF(rb_cFalseClass), "new");
03178     /*
03179      * An alias of +false+
03180      */
03181     rb_define_global_const("FALSE", Qfalse);
03182 
03183     id_eq = rb_intern("==");
03184     id_eql = rb_intern("eql?");
03185     id_match = rb_intern("=~");
03186     id_inspect = rb_intern("inspect");
03187     id_init_copy = rb_intern("initialize_copy");
03188     id_init_clone = rb_intern("initialize_clone");
03189     id_init_dup = rb_intern("initialize_dup");
03190     id_const_missing = rb_intern("const_missing");
03191 
03192     for (i=0; conv_method_names[i].method; i++) {
03193         conv_method_names[i].id = rb_intern(conv_method_names[i].method);
03194     }
03195 }
03196