Ruby  2.0.0p481(2014-05-08revision45883)
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  * Document-method: extended
00776  *
00777  * call-seq:
00778  *    extended(othermod)
00779  *
00780  * The equivalent of <tt>included</tt>, but for extended modules.
00781  *
00782  *        module A
00783  *          def self.extended(mod)
00784  *            puts "#{self} extended in #{mod}"
00785  *          end
00786  *        end
00787  *        module Enumerable
00788  *          extend A
00789  *        end
00790  *         # => prints "A extended in Enumerable"
00791  */
00792 
00793 /*
00794  * Document-method: included
00795  *
00796  * call-seq:
00797  *    included(othermod)
00798  *
00799  * Callback invoked whenever the receiver is included in another
00800  * module or class. This should be used in preference to
00801  * <tt>Module.append_features</tt> if your code wants to perform some
00802  * action when a module is included in another.
00803  *
00804  *        module A
00805  *          def A.included(mod)
00806  *            puts "#{self} included in #{mod}"
00807  *          end
00808  *        end
00809  *        module Enumerable
00810  *          include A
00811  *        end
00812  *         # => prints "A included in Enumerable"
00813  */
00814 
00815 /*
00816  * Document-method: prepended
00817  *
00818  * call-seq:
00819  *    prepended(othermod)
00820  *
00821  * The equivalent of <tt>included</tt>, but for prepended modules.
00822  *
00823  *        module A
00824  *          def self.prepended(mod)
00825  *            puts "#{self} prepended to #{mod}"
00826  *          end
00827  *        end
00828  *        module Enumerable
00829  *          prepend A
00830  *        end
00831  *         # => prints "A prepended to Enumerable"
00832  */
00833 
00834 /*
00835  * Document-method: initialize
00836  *
00837  * call-seq:
00838  *    BasicObject.new
00839  *
00840  * Returns a new BasicObject.
00841  */
00842 
00843 /*
00844  * Not documented
00845  */
00846 
00847 static VALUE
00848 rb_obj_dummy(void)
00849 {
00850     return Qnil;
00851 }
00852 
00853 /*
00854  *  call-seq:
00855  *     obj.tainted?    -> true or false
00856  *
00857  *  Returns <code>true</code> if the object is tainted.
00858  */
00859 
00860 VALUE
00861 rb_obj_tainted(VALUE obj)
00862 {
00863     if (OBJ_TAINTED(obj))
00864         return Qtrue;
00865     return Qfalse;
00866 }
00867 
00868 /*
00869  *  call-seq:
00870  *     obj.taint -> obj
00871  *
00872  *  Marks <i>obj</i> as tainted---if the <code>$SAFE</code> level is
00873  *  set appropriately, many method calls which might alter the running
00874  *  programs environment will refuse to accept tainted strings.
00875  */
00876 
00877 VALUE
00878 rb_obj_taint(VALUE obj)
00879 {
00880     rb_secure(4);
00881     if (!OBJ_TAINTED(obj)) {
00882         rb_check_frozen(obj);
00883         OBJ_TAINT(obj);
00884     }
00885     return obj;
00886 }
00887 
00888 
00889 /*
00890  *  call-seq:
00891  *     obj.untaint    -> obj
00892  *
00893  *  Removes the taint from <i>obj</i>.
00894  */
00895 
00896 VALUE
00897 rb_obj_untaint(VALUE obj)
00898 {
00899     rb_secure(3);
00900     if (OBJ_TAINTED(obj)) {
00901         rb_check_frozen(obj);
00902         FL_UNSET(obj, FL_TAINT);
00903     }
00904     return obj;
00905 }
00906 
00907 /*
00908  *  call-seq:
00909  *     obj.untrusted?    -> true or false
00910  *
00911  *  Returns <code>true</code> if the object is untrusted.
00912  */
00913 
00914 VALUE
00915 rb_obj_untrusted(VALUE obj)
00916 {
00917     if (OBJ_UNTRUSTED(obj))
00918         return Qtrue;
00919     return Qfalse;
00920 }
00921 
00922 /*
00923  *  call-seq:
00924  *     obj.untrust -> obj
00925  *
00926  *  Marks <i>obj</i> as untrusted.
00927  */
00928 
00929 VALUE
00930 rb_obj_untrust(VALUE obj)
00931 {
00932     rb_secure(4);
00933     if (!OBJ_UNTRUSTED(obj)) {
00934         rb_check_frozen(obj);
00935         OBJ_UNTRUST(obj);
00936     }
00937     return obj;
00938 }
00939 
00940 
00941 /*
00942  *  call-seq:
00943  *     obj.trust    -> obj
00944  *
00945  *  Removes the untrusted mark from <i>obj</i>.
00946  */
00947 
00948 VALUE
00949 rb_obj_trust(VALUE obj)
00950 {
00951     rb_secure(3);
00952     if (OBJ_UNTRUSTED(obj)) {
00953         rb_check_frozen(obj);
00954         FL_UNSET(obj, FL_UNTRUSTED);
00955     }
00956     return obj;
00957 }
00958 
00959 void
00960 rb_obj_infect(VALUE obj1, VALUE obj2)
00961 {
00962     OBJ_INFECT(obj1, obj2);
00963 }
00964 
00965 static st_table *immediate_frozen_tbl = 0;
00966 
00967 /*
00968  *  call-seq:
00969  *     obj.freeze    -> obj
00970  *
00971  *  Prevents further modifications to <i>obj</i>. A
00972  *  <code>RuntimeError</code> will be raised if modification is attempted.
00973  *  There is no way to unfreeze a frozen object. See also
00974  *  <code>Object#frozen?</code>.
00975  *
00976  *  This method returns self.
00977  *
00978  *     a = [ "a", "b", "c" ]
00979  *     a.freeze
00980  *     a << "z"
00981  *
00982  *  <em>produces:</em>
00983  *
00984  *     prog.rb:3:in `<<': can't modify frozen array (RuntimeError)
00985  *      from prog.rb:3
00986  */
00987 
00988 VALUE
00989 rb_obj_freeze(VALUE obj)
00990 {
00991     if (!OBJ_FROZEN(obj)) {
00992         if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(obj)) {
00993             rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
00994         }
00995         OBJ_FREEZE(obj);
00996         if (SPECIAL_CONST_P(obj)) {
00997             if (!immediate_frozen_tbl) {
00998                 immediate_frozen_tbl = st_init_numtable();
00999             }
01000             st_insert(immediate_frozen_tbl, obj, (st_data_t)Qtrue);
01001         }
01002     }
01003     return obj;
01004 }
01005 
01006 /*
01007  *  call-seq:
01008  *     obj.frozen?    -> true or false
01009  *
01010  *  Returns the freeze status of <i>obj</i>.
01011  *
01012  *     a = [ "a", "b", "c" ]
01013  *     a.freeze    #=> ["a", "b", "c"]
01014  *     a.frozen?   #=> true
01015  */
01016 
01017 VALUE
01018 rb_obj_frozen_p(VALUE obj)
01019 {
01020     if (OBJ_FROZEN(obj)) return Qtrue;
01021     if (SPECIAL_CONST_P(obj)) {
01022         if (!immediate_frozen_tbl) return Qfalse;
01023         if (st_lookup(immediate_frozen_tbl, obj, 0)) return Qtrue;
01024     }
01025     return Qfalse;
01026 }
01027 
01028 
01029 /*
01030  * Document-class: NilClass
01031  *
01032  *  The class of the singleton object <code>nil</code>.
01033  */
01034 
01035 /*
01036  *  call-seq:
01037  *     nil.to_i -> 0
01038  *
01039  *  Always returns zero.
01040  *
01041  *     nil.to_i   #=> 0
01042  */
01043 
01044 
01045 static VALUE
01046 nil_to_i(VALUE obj)
01047 {
01048     return INT2FIX(0);
01049 }
01050 
01051 /*
01052  *  call-seq:
01053  *     nil.to_f    -> 0.0
01054  *
01055  *  Always returns zero.
01056  *
01057  *     nil.to_f   #=> 0.0
01058  */
01059 
01060 static VALUE
01061 nil_to_f(VALUE obj)
01062 {
01063     return DBL2NUM(0.0);
01064 }
01065 
01066 /*
01067  *  call-seq:
01068  *     nil.to_s    -> ""
01069  *
01070  *  Always returns the empty string.
01071  */
01072 
01073 static VALUE
01074 nil_to_s(VALUE obj)
01075 {
01076     return rb_usascii_str_new(0, 0);
01077 }
01078 
01079 /*
01080  * Document-method: to_a
01081  *
01082  *  call-seq:
01083  *     nil.to_a    -> []
01084  *
01085  *  Always returns an empty array.
01086  *
01087  *     nil.to_a   #=> []
01088  */
01089 
01090 static VALUE
01091 nil_to_a(VALUE obj)
01092 {
01093     return rb_ary_new2(0);
01094 }
01095 
01096 /*
01097  * Document-method: to_h
01098  *
01099  *  call-seq:
01100  *     nil.to_h    -> {}
01101  *
01102  *  Always returns an empty hash.
01103  *
01104  *     nil.to_h   #=> {}
01105  */
01106 
01107 static VALUE
01108 nil_to_h(VALUE obj)
01109 {
01110     return rb_hash_new();
01111 }
01112 
01113 /*
01114  *  call-seq:
01115  *    nil.inspect  -> "nil"
01116  *
01117  *  Always returns the string "nil".
01118  */
01119 
01120 static VALUE
01121 nil_inspect(VALUE obj)
01122 {
01123     return rb_usascii_str_new2("nil");
01124 }
01125 
01126 /***********************************************************************
01127  *  Document-class: TrueClass
01128  *
01129  *  The global value <code>true</code> is the only instance of class
01130  *  <code>TrueClass</code> and represents a logically true value in
01131  *  boolean expressions. The class provides operators allowing
01132  *  <code>true</code> to be used in logical expressions.
01133  */
01134 
01135 
01136 /*
01137  * call-seq:
01138  *   true.to_s   ->  "true"
01139  *
01140  * The string representation of <code>true</code> is "true".
01141  */
01142 
01143 static VALUE
01144 true_to_s(VALUE obj)
01145 {
01146     return rb_usascii_str_new2("true");
01147 }
01148 
01149 
01150 /*
01151  *  call-seq:
01152  *     true & obj    -> true or false
01153  *
01154  *  And---Returns <code>false</code> if <i>obj</i> is
01155  *  <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
01156  */
01157 
01158 static VALUE
01159 true_and(VALUE obj, VALUE obj2)
01160 {
01161     return RTEST(obj2)?Qtrue:Qfalse;
01162 }
01163 
01164 /*
01165  *  call-seq:
01166  *     true | obj   -> true
01167  *
01168  *  Or---Returns <code>true</code>. As <i>anObject</i> is an argument to
01169  *  a method call, it is always evaluated; there is no short-circuit
01170  *  evaluation in this case.
01171  *
01172  *     true |  puts("or")
01173  *     true || puts("logical or")
01174  *
01175  *  <em>produces:</em>
01176  *
01177  *     or
01178  */
01179 
01180 static VALUE
01181 true_or(VALUE obj, VALUE obj2)
01182 {
01183     return Qtrue;
01184 }
01185 
01186 
01187 /*
01188  *  call-seq:
01189  *     true ^ obj   -> !obj
01190  *
01191  *  Exclusive Or---Returns <code>true</code> if <i>obj</i> is
01192  *  <code>nil</code> or <code>false</code>, <code>false</code>
01193  *  otherwise.
01194  */
01195 
01196 static VALUE
01197 true_xor(VALUE obj, VALUE obj2)
01198 {
01199     return RTEST(obj2)?Qfalse:Qtrue;
01200 }
01201 
01202 
01203 /*
01204  *  Document-class: FalseClass
01205  *
01206  *  The global value <code>false</code> is the only instance of class
01207  *  <code>FalseClass</code> and represents a logically false value in
01208  *  boolean expressions. The class provides operators allowing
01209  *  <code>false</code> to participate correctly in logical expressions.
01210  *
01211  */
01212 
01213 /*
01214  * call-seq:
01215  *   false.to_s   ->  "false"
01216  *
01217  * 'nuf said...
01218  */
01219 
01220 static VALUE
01221 false_to_s(VALUE obj)
01222 {
01223     return rb_usascii_str_new2("false");
01224 }
01225 
01226 /*
01227  *  call-seq:
01228  *     false & obj   -> false
01229  *     nil & obj     -> false
01230  *
01231  *  And---Returns <code>false</code>. <i>obj</i> is always
01232  *  evaluated as it is the argument to a method call---there is no
01233  *  short-circuit evaluation in this case.
01234  */
01235 
01236 static VALUE
01237 false_and(VALUE obj, VALUE obj2)
01238 {
01239     return Qfalse;
01240 }
01241 
01242 
01243 /*
01244  *  call-seq:
01245  *     false | obj   ->   true or false
01246  *     nil   | obj   ->   true or false
01247  *
01248  *  Or---Returns <code>false</code> if <i>obj</i> is
01249  *  <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
01250  */
01251 
01252 static VALUE
01253 false_or(VALUE obj, VALUE obj2)
01254 {
01255     return RTEST(obj2)?Qtrue:Qfalse;
01256 }
01257 
01258 
01259 
01260 /*
01261  *  call-seq:
01262  *     false ^ obj    -> true or false
01263  *     nil   ^ obj    -> true or false
01264  *
01265  *  Exclusive Or---If <i>obj</i> is <code>nil</code> or
01266  *  <code>false</code>, returns <code>false</code>; otherwise, returns
01267  *  <code>true</code>.
01268  *
01269  */
01270 
01271 static VALUE
01272 false_xor(VALUE obj, VALUE obj2)
01273 {
01274     return RTEST(obj2)?Qtrue:Qfalse;
01275 }
01276 
01277 /*
01278  * call_seq:
01279  *   nil.nil?               -> true
01280  *
01281  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
01282  */
01283 
01284 static VALUE
01285 rb_true(VALUE obj)
01286 {
01287     return Qtrue;
01288 }
01289 
01290 /*
01291  * call_seq:
01292  *   nil.nil?               -> true
01293  *   <anything_else>.nil?   -> false
01294  *
01295  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
01296  */
01297 
01298 
01299 static VALUE
01300 rb_false(VALUE obj)
01301 {
01302     return Qfalse;
01303 }
01304 
01305 
01306 /*
01307  *  call-seq:
01308  *     obj =~ other  -> nil
01309  *
01310  *  Pattern Match---Overridden by descendants (notably
01311  *  <code>Regexp</code> and <code>String</code>) to provide meaningful
01312  *  pattern-match semantics.
01313  */
01314 
01315 static VALUE
01316 rb_obj_match(VALUE obj1, VALUE obj2)
01317 {
01318     return Qnil;
01319 }
01320 
01321 /*
01322  *  call-seq:
01323  *     obj !~ other  -> true or false
01324  *
01325  *  Returns true if two objects do not match (using the <i>=~</i>
01326  *  method), otherwise false.
01327  */
01328 
01329 static VALUE
01330 rb_obj_not_match(VALUE obj1, VALUE obj2)
01331 {
01332     VALUE result = rb_funcall(obj1, id_match, 1, obj2);
01333     return RTEST(result) ? Qfalse : Qtrue;
01334 }
01335 
01336 
01337 /*
01338  *  call-seq:
01339  *     obj <=> other -> 0 or nil
01340  *
01341  *  Returns 0 if +obj+ and +other+ are the same object
01342  *  or <code>obj == other</code>, otherwise nil.
01343  *
01344  *  The <=> is used by various methods to compare objects, for example
01345  *  Enumerable#sort, Enumerable#max etc.
01346  *
01347  *  Your implementation of <=> should return one of the following values: -1, 0,
01348  *  1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
01349  *  1 means self is bigger than other. Nil means the two values could not be
01350  *  compared.
01351  *
01352  *  When you define <=>, you can include Comparable to gain the methods <=, <,
01353  *  ==, >=, > and between?.
01354  */
01355 static VALUE
01356 rb_obj_cmp(VALUE obj1, VALUE obj2)
01357 {
01358     if (obj1 == obj2 || rb_equal(obj1, obj2))
01359         return INT2FIX(0);
01360     return Qnil;
01361 }
01362 
01363 /***********************************************************************
01364  *
01365  * Document-class: Module
01366  *
01367  *  A <code>Module</code> is a collection of methods and constants. The
01368  *  methods in a module may be instance methods or module methods.
01369  *  Instance methods appear as methods in a class when the module is
01370  *  included, module methods do not. Conversely, module methods may be
01371  *  called without creating an encapsulating object, while instance
01372  *  methods may not. (See <code>Module#module_function</code>)
01373  *
01374  *  In the descriptions that follow, the parameter <i>sym</i> refers
01375  *  to a symbol, which is either a quoted string or a
01376  *  <code>Symbol</code> (such as <code>:name</code>).
01377  *
01378  *     module Mod
01379  *       include Math
01380  *       CONST = 1
01381  *       def meth
01382  *         #  ...
01383  *       end
01384  *     end
01385  *     Mod.class              #=> Module
01386  *     Mod.constants          #=> [:CONST, :PI, :E]
01387  *     Mod.instance_methods   #=> [:meth]
01388  *
01389  */
01390 
01391 /*
01392  * call-seq:
01393  *   mod.to_s   -> string
01394  *
01395  * Return a string representing this module or class. For basic
01396  * classes and modules, this is the name. For singletons, we
01397  * show information on the thing we're attached to as well.
01398  */
01399 
01400 static VALUE
01401 rb_mod_to_s(VALUE klass)
01402 {
01403     ID id_defined_at;
01404     VALUE refined_class, defined_at;
01405 
01406     if (FL_TEST(klass, FL_SINGLETON)) {
01407         VALUE s = rb_usascii_str_new2("#<Class:");
01408         VALUE v = rb_iv_get(klass, "__attached__");
01409 
01410         if (CLASS_OR_MODULE_P(v)) {
01411             rb_str_append(s, rb_inspect(v));
01412         }
01413         else {
01414             rb_str_append(s, rb_any_to_s(v));
01415         }
01416         rb_str_cat2(s, ">");
01417 
01418         return s;
01419     }
01420     refined_class = rb_refinement_module_get_refined_class(klass);
01421     if (!NIL_P(refined_class)) {
01422         VALUE s = rb_usascii_str_new2("#<refinement:");
01423 
01424         rb_str_concat(s, rb_inspect(refined_class));
01425         rb_str_cat2(s, "@");
01426         CONST_ID(id_defined_at, "__defined_at__");
01427         defined_at = rb_attr_get(klass, id_defined_at);
01428         rb_str_concat(s, rb_inspect(defined_at));
01429         rb_str_cat2(s, ">");
01430         return s;
01431     }
01432     return rb_str_dup(rb_class_name(klass));
01433 }
01434 
01435 /*
01436  *  call-seq:
01437  *     mod.freeze       -> mod
01438  *
01439  *  Prevents further modifications to <i>mod</i>.
01440  *
01441  *  This method returns self.
01442  */
01443 
01444 static VALUE
01445 rb_mod_freeze(VALUE mod)
01446 {
01447     rb_class_name(mod);
01448     return rb_obj_freeze(mod);
01449 }
01450 
01451 /*
01452  *  call-seq:
01453  *     mod === obj    -> true or false
01454  *
01455  *  Case Equality---Returns <code>true</code> if <i>anObject</i> is an
01456  *  instance of <i>mod</i> or one of <i>mod</i>'s descendants. Of
01457  *  limited use for modules, but can be used in <code>case</code>
01458  *  statements to classify objects by class.
01459  */
01460 
01461 static VALUE
01462 rb_mod_eqq(VALUE mod, VALUE arg)
01463 {
01464     return rb_obj_is_kind_of(arg, mod);
01465 }
01466 
01467 /*
01468  * call-seq:
01469  *   mod <= other   ->  true, false, or nil
01470  *
01471  * Returns true if <i>mod</i> is a subclass of <i>other</i> or
01472  * is the same as <i>other</i>. Returns
01473  * <code>nil</code> if there's no relationship between the two.
01474  * (Think of the relationship in terms of the class definition:
01475  * "class A<B" implies "A<B").
01476  *
01477  */
01478 
01479 VALUE
01480 rb_class_inherited_p(VALUE mod, VALUE arg)
01481 {
01482     VALUE start = mod;
01483 
01484     if (mod == arg) return Qtrue;
01485     if (!CLASS_OR_MODULE_P(arg) && !RB_TYPE_P(arg, T_ICLASS)) {
01486         rb_raise(rb_eTypeError, "compared with non class/module");
01487     }
01488     arg = RCLASS_ORIGIN(arg);
01489     while (mod) {
01490         if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg))
01491             return Qtrue;
01492         mod = RCLASS_SUPER(mod);
01493     }
01494     /* not mod < arg; check if mod > arg */
01495     while (arg) {
01496         if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start))
01497             return Qfalse;
01498         arg = RCLASS_SUPER(arg);
01499     }
01500     return Qnil;
01501 }
01502 
01503 /*
01504  * call-seq:
01505  *   mod < other   ->  true, false, or nil
01506  *
01507  * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
01508  * <code>nil</code> if there's no relationship between the two.
01509  * (Think of the relationship in terms of the class definition:
01510  * "class A<B" implies "A<B").
01511  *
01512  */
01513 
01514 static VALUE
01515 rb_mod_lt(VALUE mod, VALUE arg)
01516 {
01517     if (mod == arg) return Qfalse;
01518     return rb_class_inherited_p(mod, arg);
01519 }
01520 
01521 
01522 /*
01523  * call-seq:
01524  *   mod >= other   ->  true, false, or nil
01525  *
01526  * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
01527  * two modules are the same. Returns
01528  * <code>nil</code> if there's no relationship between the two.
01529  * (Think of the relationship in terms of the class definition:
01530  * "class A<B" implies "B>A").
01531  *
01532  */
01533 
01534 static VALUE
01535 rb_mod_ge(VALUE mod, VALUE arg)
01536 {
01537     if (!CLASS_OR_MODULE_P(arg)) {
01538         rb_raise(rb_eTypeError, "compared with non class/module");
01539     }
01540 
01541     return rb_class_inherited_p(arg, mod);
01542 }
01543 
01544 /*
01545  * call-seq:
01546  *   mod > other   ->  true, false, or nil
01547  *
01548  * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
01549  * <code>nil</code> if there's no relationship between the two.
01550  * (Think of the relationship in terms of the class definition:
01551  * "class A<B" implies "B>A").
01552  *
01553  */
01554 
01555 static VALUE
01556 rb_mod_gt(VALUE mod, VALUE arg)
01557 {
01558     if (mod == arg) return Qfalse;
01559     return rb_mod_ge(mod, arg);
01560 }
01561 
01562 /*
01563  *  call-seq:
01564  *     module <=> other_module   -> -1, 0, +1, or nil
01565  *
01566  *  Comparison---Returns -1, 0, +1 or nil depending on whether +module+
01567  *  includes +other_module+, they are the same, or if +module+ is included by
01568  *  +other_module+. This is the basis for the tests in Comparable.
01569  *
01570  *  Returns +nil+ if +module+ has no relationship with +other_module+, if
01571  *  +other_module+ is not a module, or if the two values are incomparable.
01572  */
01573 
01574 static VALUE
01575 rb_mod_cmp(VALUE mod, VALUE arg)
01576 {
01577     VALUE cmp;
01578 
01579     if (mod == arg) return INT2FIX(0);
01580     if (!CLASS_OR_MODULE_P(arg)) {
01581         return Qnil;
01582     }
01583 
01584     cmp = rb_class_inherited_p(mod, arg);
01585     if (NIL_P(cmp)) return Qnil;
01586     if (cmp) {
01587         return INT2FIX(-1);
01588     }
01589     return INT2FIX(1);
01590 }
01591 
01592 static VALUE
01593 rb_module_s_alloc(VALUE klass)
01594 {
01595     VALUE mod = rb_module_new();
01596 
01597     RBASIC(mod)->klass = klass;
01598     return mod;
01599 }
01600 
01601 static VALUE
01602 rb_class_s_alloc(VALUE klass)
01603 {
01604     return rb_class_boot(0);
01605 }
01606 
01607 /*
01608  *  call-seq:
01609  *    Module.new                  -> mod
01610  *    Module.new {|mod| block }   -> mod
01611  *
01612  *  Creates a new anonymous module. If a block is given, it is passed
01613  *  the module object, and the block is evaluated in the context of this
01614  *  module using <code>module_eval</code>.
01615  *
01616  *     fred = Module.new do
01617  *       def meth1
01618  *         "hello"
01619  *       end
01620  *       def meth2
01621  *         "bye"
01622  *       end
01623  *     end
01624  *     a = "my string"
01625  *     a.extend(fred)   #=> "my string"
01626  *     a.meth1          #=> "hello"
01627  *     a.meth2          #=> "bye"
01628  *
01629  *  Assign the module to a constant (name starting uppercase) if you
01630  *  want to treat it like a regular module.
01631  */
01632 
01633 static VALUE
01634 rb_mod_initialize(VALUE module)
01635 {
01636     if (rb_block_given_p()) {
01637         rb_mod_module_exec(1, &module, module);
01638     }
01639     return Qnil;
01640 }
01641 
01642 /*
01643  *  call-seq:
01644  *     Class.new(super_class=Object)               -> a_class
01645  *     Class.new(super_class=Object) { |mod| ... } -> a_class
01646  *
01647  *  Creates a new anonymous (unnamed) class with the given superclass
01648  *  (or <code>Object</code> if no parameter is given). You can give a
01649  *  class a name by assigning the class object to a constant.
01650  *
01651  *  If a block is given, it is passed the class object, and the block
01652  *  is evaluated in the context of this class using
01653  *  <code>class_eval</code>.
01654  *
01655  *     fred = Class.new do
01656  *       def meth1
01657  *         "hello"
01658  *       end
01659  *       def meth2
01660  *         "bye"
01661  *       end
01662  *     end
01663  *
01664  *     a = fred.new     #=> #<#<Class:0x100381890>:0x100376b98>
01665  *     a.meth1          #=> "hello"
01666  *     a.meth2          #=> "bye"
01667  *
01668  *  Assign the class to a constant (name starting uppercase) if you
01669  *  want to treat it like a regular class.
01670  */
01671 
01672 static VALUE
01673 rb_class_initialize(int argc, VALUE *argv, VALUE klass)
01674 {
01675     VALUE super;
01676 
01677     if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
01678         rb_raise(rb_eTypeError, "already initialized class");
01679     }
01680     if (argc == 0) {
01681         super = rb_cObject;
01682     }
01683     else {
01684         rb_scan_args(argc, argv, "01", &super);
01685         rb_check_inheritable(super);
01686         if (super != rb_cBasicObject && !RCLASS_SUPER(super)) {
01687             rb_raise(rb_eTypeError, "can't inherit uninitialized class");
01688         }
01689     }
01690     RCLASS_SUPER(klass) = super;
01691     rb_make_metaclass(klass, RBASIC(super)->klass);
01692     rb_class_inherited(super, klass);
01693     rb_mod_initialize(klass);
01694 
01695     return klass;
01696 }
01697 
01698 /*
01699  *  call-seq:
01700  *     class.allocate()   ->   obj
01701  *
01702  *  Allocates space for a new object of <i>class</i>'s class and does not
01703  *  call initialize on the new instance. The returned object must be an
01704  *  instance of <i>class</i>.
01705  *
01706  *      klass = Class.new do
01707  *        def initialize(*args)
01708  *          @initialized = true
01709  *        end
01710  *
01711  *        def initialized?
01712  *          @initialized || false
01713  *        end
01714  *      end
01715  *
01716  *      klass.allocate.initialized? #=> false
01717  *
01718  */
01719 
01720 VALUE
01721 rb_obj_alloc(VALUE klass)
01722 {
01723     VALUE obj;
01724     rb_alloc_func_t allocator;
01725 
01726     if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
01727         rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
01728     }
01729     if (FL_TEST(klass, FL_SINGLETON)) {
01730         rb_raise(rb_eTypeError, "can't create instance of singleton class");
01731     }
01732     allocator = rb_get_alloc_func(klass);
01733     if (!allocator) {
01734         rb_raise(rb_eTypeError, "allocator undefined for %"PRIsVALUE,
01735                  klass);
01736     }
01737 
01738 #if !defined(DTRACE_PROBES_DISABLED) || !DTRACE_PROBES_DISABLED
01739     if (RUBY_DTRACE_OBJECT_CREATE_ENABLED()) {
01740         const char * file = rb_sourcefile();
01741         RUBY_DTRACE_OBJECT_CREATE(rb_class2name(klass),
01742                                   file ? file : "",
01743                                   rb_sourceline());
01744     }
01745 #endif
01746 
01747     obj = (*allocator)(klass);
01748 
01749     if (rb_obj_class(obj) != rb_class_real(klass)) {
01750         rb_raise(rb_eTypeError, "wrong instance allocation");
01751     }
01752     return obj;
01753 }
01754 
01755 static VALUE
01756 rb_class_allocate_instance(VALUE klass)
01757 {
01758     NEWOBJ_OF(obj, struct RObject, klass, T_OBJECT);
01759     return (VALUE)obj;
01760 }
01761 
01762 /*
01763  *  call-seq:
01764  *     class.new(args, ...)    ->  obj
01765  *
01766  *  Calls <code>allocate</code> to create a new object of
01767  *  <i>class</i>'s class, then invokes that object's
01768  *  <code>initialize</code> method, passing it <i>args</i>.
01769  *  This is the method that ends up getting called whenever
01770  *  an object is constructed using .new.
01771  *
01772  */
01773 
01774 VALUE
01775 rb_class_new_instance(int argc, VALUE *argv, VALUE klass)
01776 {
01777     VALUE obj;
01778 
01779     obj = rb_obj_alloc(klass);
01780     rb_obj_call_init(obj, argc, argv);
01781 
01782     return obj;
01783 }
01784 
01785 /*
01786  *  call-seq:
01787  *     class.superclass -> a_super_class or nil
01788  *
01789  *  Returns the superclass of <i>class</i>, or <code>nil</code>.
01790  *
01791  *     File.superclass          #=> IO
01792  *     IO.superclass            #=> Object
01793  *     Object.superclass        #=> BasicObject
01794  *     class Foo; end
01795  *     class Bar < Foo; end
01796  *     Bar.superclass           #=> Foo
01797  *
01798  *  returns nil when the given class hasn't a parent class:
01799  *
01800  *     BasicObject.superclass   #=> nil
01801  *
01802  */
01803 
01804 VALUE
01805 rb_class_superclass(VALUE klass)
01806 {
01807     VALUE super = RCLASS_SUPER(klass);
01808 
01809     if (!super) {
01810         if (klass == rb_cBasicObject) return Qnil;
01811         rb_raise(rb_eTypeError, "uninitialized class");
01812     }
01813     while (RB_TYPE_P(super, T_ICLASS)) {
01814         super = RCLASS_SUPER(super);
01815     }
01816     if (!super) {
01817         return Qnil;
01818     }
01819     return super;
01820 }
01821 
01822 VALUE
01823 rb_class_get_superclass(VALUE klass)
01824 {
01825     return RCLASS_SUPER(klass);
01826 }
01827 
01828 /*
01829  *  call-seq:
01830  *     attr_reader(symbol, ...)    -> nil
01831  *     attr(symbol, ...)             -> nil
01832  *
01833  *  Creates instance variables and corresponding methods that return the
01834  *  value of each instance variable. Equivalent to calling
01835  *  ``<code>attr</code><i>:name</i>'' on each name in turn.
01836  */
01837 
01838 static VALUE
01839 rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
01840 {
01841     int i;
01842 
01843     for (i=0; i<argc; i++) {
01844         rb_attr(klass, rb_to_id(argv[i]), TRUE, FALSE, TRUE);
01845     }
01846     return Qnil;
01847 }
01848 
01849 VALUE
01850 rb_mod_attr(int argc, VALUE *argv, VALUE klass)
01851 {
01852     if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
01853         rb_warning("optional boolean argument is obsoleted");
01854         rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), TRUE);
01855         return Qnil;
01856     }
01857     return rb_mod_attr_reader(argc, argv, klass);
01858 }
01859 
01860 /*
01861  *  call-seq:
01862  *      attr_writer(symbol, ...)    -> nil
01863  *
01864  *  Creates an accessor method to allow assignment to the attribute
01865  *  <i>symbol</i><code>.id2name</code>.
01866  */
01867 
01868 static VALUE
01869 rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
01870 {
01871     int i;
01872 
01873     for (i=0; i<argc; i++) {
01874         rb_attr(klass, rb_to_id(argv[i]), FALSE, TRUE, TRUE);
01875     }
01876     return Qnil;
01877 }
01878 
01879 /*
01880  *  call-seq:
01881  *     attr_accessor(symbol, ...)    -> nil
01882  *
01883  *  Defines a named attribute for this module, where the name is
01884  *  <i>symbol.</i><code>id2name</code>, creating an instance variable
01885  *  (<code>@name</code>) and a corresponding access method to read it.
01886  *  Also creates a method called <code>name=</code> to set the attribute.
01887  *
01888  *     module Mod
01889  *       attr_accessor(:one, :two)
01890  *     end
01891  *     Mod.instance_methods.sort   #=> [:one, :one=, :two, :two=]
01892  */
01893 
01894 static VALUE
01895 rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
01896 {
01897     int i;
01898 
01899     for (i=0; i<argc; i++) {
01900         rb_attr(klass, rb_to_id(argv[i]), TRUE, TRUE, TRUE);
01901     }
01902     return Qnil;
01903 }
01904 
01905 /*
01906  *  call-seq:
01907  *     mod.const_get(sym, inherit=true)    -> obj
01908  *     mod.const_get(str, inherit=true)    -> obj
01909  *
01910  *  Checks for a constant with the given name in <i>mod</i>
01911  *  If +inherit+ is set, the lookup will also search
01912  *  the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
01913  *
01914  *  The value of the constant is returned if a definition is found,
01915  *  otherwise a +NameError+ is raised.
01916  *
01917  *     Math.const_get(:PI)   #=> 3.14159265358979
01918  *
01919  *  This method will recursively look up constant names if a namespaced
01920  *  class name is provided.  For example:
01921  *
01922  *     module Foo; class Bar; end end
01923  *     Object.const_get 'Foo::Bar'
01924  *
01925  *  The +inherit+ flag is respected on each lookup.  For example:
01926  *
01927  *     module Foo
01928  *       class Bar
01929  *         VAL = 10
01930  *       end
01931  *
01932  *       class Baz < Bar; end
01933  *     end
01934  *
01935  *     Object.const_get 'Foo::Baz::VAL'         # => 10
01936  *     Object.const_get 'Foo::Baz::VAL', false  # => NameError
01937  */
01938 
01939 static VALUE
01940 rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
01941 {
01942     VALUE name, recur;
01943     rb_encoding *enc;
01944     const char *pbeg, *p, *path, *pend;
01945     ID id;
01946     int nestable = 1;
01947 
01948     if (argc == 1) {
01949         name = argv[0];
01950         recur = Qtrue;
01951     }
01952     else {
01953         rb_scan_args(argc, argv, "11", &name, &recur);
01954     }
01955 
01956     if (SYMBOL_P(name)) {
01957         name = rb_sym_to_s(name);
01958         nestable = 0;
01959     }
01960 
01961     name = rb_check_string_type(name);
01962     Check_Type(name, T_STRING);
01963 
01964     enc = rb_enc_get(name);
01965     path = RSTRING_PTR(name);
01966 
01967     if (!rb_enc_asciicompat(enc)) {
01968         rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
01969     }
01970 
01971     pbeg = p = path;
01972     pend = path + RSTRING_LEN(name);
01973 
01974     if (p >= pend || !*p) {
01975       wrong_name:
01976         rb_raise(rb_eNameError, "wrong constant name %"PRIsVALUE,
01977                  QUOTE(name));
01978     }
01979 
01980     if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
01981         if (!nestable) goto wrong_name;
01982         mod = rb_cObject;
01983         p += 2;
01984         pbeg = p;
01985     }
01986 
01987     while (p < pend) {
01988         VALUE part;
01989         long len, beglen;
01990 
01991         while (p < pend && *p != ':') p++;
01992 
01993         if (pbeg == p) goto wrong_name;
01994 
01995         id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
01996         beglen = pbeg-path;
01997 
01998         if (p < pend && p[0] == ':') {
01999             if (!nestable) goto wrong_name;
02000             if (p + 2 >= pend || p[1] != ':') goto wrong_name;
02001             p += 2;
02002             pbeg = p;
02003         }
02004 
02005         if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
02006             rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
02007                      QUOTE(name));
02008         }
02009 
02010         if (!id) {
02011             if (!ISUPPER(*pbeg) || !rb_enc_symname2_p(pbeg, len, enc)) {
02012                 part = rb_str_subseq(name, beglen, len);
02013                 rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
02014                                   QUOTE(part));
02015             }
02016             else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
02017                 id = rb_intern3(pbeg, len, enc);
02018             }
02019             else {
02020                 part = rb_str_subseq(name, beglen, len);
02021                 rb_name_error_str(part, "uninitialized constant %"PRIsVALUE"%"PRIsVALUE,
02022                                   rb_str_subseq(name, 0, beglen),
02023                                   QUOTE(part));
02024             }
02025         }
02026         if (!rb_is_const_id(id)) {
02027             rb_name_error(id, "wrong constant name %"PRIsVALUE,
02028                           QUOTE_ID(id));
02029         }
02030         mod = RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
02031     }
02032 
02033     return mod;
02034 }
02035 
02036 /*
02037  *  call-seq:
02038  *     mod.const_set(sym, obj)    -> obj
02039  *
02040  *  Sets the named constant to the given object, returning that object.
02041  *  Creates a new constant if no constant with the given name previously
02042  *  existed.
02043  *
02044  *     Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0)   #=> 3.14285714285714
02045  *     Math::HIGH_SCHOOL_PI - Math::PI              #=> 0.00126448926734968
02046  */
02047 
02048 static VALUE
02049 rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
02050 {
02051     ID id = rb_to_id(name);
02052 
02053     if (!rb_is_const_id(id)) {
02054         rb_name_error(id, "wrong constant name %"PRIsVALUE,
02055                       QUOTE_ID(id));
02056     }
02057     rb_const_set(mod, id, value);
02058     return value;
02059 }
02060 
02061 /*
02062  *  call-seq:
02063  *     mod.const_defined?(sym, inherit=true)   -> true or false
02064  *
02065  *  Checks for a constant with the given name in <i>mod</i>
02066  *  If +inherit+ is set, the lookup will also search
02067  *  the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
02068  *
02069  *  Returns whether or not a definition is found:
02070  *
02071  *     Math.const_defined? "PI"   #=> true
02072  *     IO.const_defined? :SYNC   #=> true
02073  *     IO.const_defined? :SYNC, false   #=> false
02074  */
02075 
02076 static VALUE
02077 rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
02078 {
02079     VALUE name, recur;
02080     ID id;
02081 
02082     if (argc == 1) {
02083         name = argv[0];
02084         recur = Qtrue;
02085     }
02086     else {
02087         rb_scan_args(argc, argv, "11", &name, &recur);
02088     }
02089     if (!(id = rb_check_id(&name))) {
02090         if (rb_is_const_name(name)) {
02091             return Qfalse;
02092         }
02093         else {
02094             rb_name_error_str(name, "wrong constant name %"PRIsVALUE,
02095                               QUOTE(name));
02096         }
02097     }
02098     if (!rb_is_const_id(id)) {
02099         rb_name_error(id, "wrong constant name %"PRIsVALUE,
02100                       QUOTE_ID(id));
02101     }
02102     return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
02103 }
02104 
02105 /*
02106  *  call-seq:
02107  *     obj.instance_variable_get(symbol)    -> obj
02108  *
02109  *  Returns the value of the given instance variable, or nil if the
02110  *  instance variable is not set. The <code>@</code> part of the
02111  *  variable name should be included for regular instance
02112  *  variables. Throws a <code>NameError</code> exception if the
02113  *  supplied symbol is not valid as an instance variable name.
02114  *
02115  *     class Fred
02116  *       def initialize(p1, p2)
02117  *         @a, @b = p1, p2
02118  *       end
02119  *     end
02120  *     fred = Fred.new('cat', 99)
02121  *     fred.instance_variable_get(:@a)    #=> "cat"
02122  *     fred.instance_variable_get("@b")   #=> 99
02123  */
02124 
02125 static VALUE
02126 rb_obj_ivar_get(VALUE obj, VALUE iv)
02127 {
02128     ID id = rb_check_id(&iv);
02129 
02130     if (!id) {
02131         if (rb_is_instance_name(iv)) {
02132             return Qnil;
02133         }
02134         else {
02135             rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02136                               QUOTE(iv));
02137         }
02138     }
02139     if (!rb_is_instance_id(id)) {
02140         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02141                       QUOTE_ID(id));
02142     }
02143     return rb_ivar_get(obj, id);
02144 }
02145 
02146 /*
02147  *  call-seq:
02148  *     obj.instance_variable_set(symbol, obj)    -> obj
02149  *
02150  *  Sets the instance variable names by <i>symbol</i> to
02151  *  <i>object</i>, thereby frustrating the efforts of the class's
02152  *  author to attempt to provide proper encapsulation. The variable
02153  *  did not have to exist prior to this call.
02154  *
02155  *     class Fred
02156  *       def initialize(p1, p2)
02157  *         @a, @b = p1, p2
02158  *       end
02159  *     end
02160  *     fred = Fred.new('cat', 99)
02161  *     fred.instance_variable_set(:@a, 'dog')   #=> "dog"
02162  *     fred.instance_variable_set(:@c, 'cat')   #=> "cat"
02163  *     fred.inspect                             #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
02164  */
02165 
02166 static VALUE
02167 rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
02168 {
02169     ID id = rb_to_id(iv);
02170 
02171     if (!rb_is_instance_id(id)) {
02172         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02173                       QUOTE_ID(id));
02174     }
02175     return rb_ivar_set(obj, id, val);
02176 }
02177 
02178 /*
02179  *  call-seq:
02180  *     obj.instance_variable_defined?(symbol)    -> true or false
02181  *
02182  *  Returns <code>true</code> if the given instance variable is
02183  *  defined in <i>obj</i>.
02184  *
02185  *     class Fred
02186  *       def initialize(p1, p2)
02187  *         @a, @b = p1, p2
02188  *       end
02189  *     end
02190  *     fred = Fred.new('cat', 99)
02191  *     fred.instance_variable_defined?(:@a)    #=> true
02192  *     fred.instance_variable_defined?("@b")   #=> true
02193  *     fred.instance_variable_defined?("@c")   #=> false
02194  */
02195 
02196 static VALUE
02197 rb_obj_ivar_defined(VALUE obj, VALUE iv)
02198 {
02199     ID id = rb_check_id(&iv);
02200 
02201     if (!id) {
02202         if (rb_is_instance_name(iv)) {
02203             return Qfalse;
02204         }
02205         else {
02206             rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02207                               QUOTE(iv));
02208         }
02209     }
02210     if (!rb_is_instance_id(id)) {
02211         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02212                       QUOTE_ID(id));
02213     }
02214     return rb_ivar_defined(obj, id);
02215 }
02216 
02217 /*
02218  *  call-seq:
02219  *     mod.class_variable_get(symbol)    -> obj
02220  *
02221  *  Returns the value of the given class variable (or throws a
02222  *  <code>NameError</code> exception). The <code>@@</code> part of the
02223  *  variable name should be included for regular class variables
02224  *
02225  *     class Fred
02226  *       @@foo = 99
02227  *     end
02228  *     Fred.class_variable_get(:@@foo)     #=> 99
02229  */
02230 
02231 static VALUE
02232 rb_mod_cvar_get(VALUE obj, VALUE iv)
02233 {
02234     ID id = rb_check_id(&iv);
02235 
02236     if (!id) {
02237         if (rb_is_class_name(iv)) {
02238             rb_name_error_str(iv, "uninitialized class variable %"PRIsVALUE" in %"PRIsVALUE"",
02239                               iv, rb_class_name(obj));
02240         }
02241         else {
02242             rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name",
02243                               QUOTE(iv));
02244         }
02245     }
02246     if (!rb_is_class_id(id)) {
02247         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
02248                       QUOTE_ID(id));
02249     }
02250     return rb_cvar_get(obj, id);
02251 }
02252 
02253 /*
02254  *  call-seq:
02255  *     obj.class_variable_set(symbol, obj)    -> obj
02256  *
02257  *  Sets the class variable names by <i>symbol</i> to
02258  *  <i>object</i>.
02259  *
02260  *     class Fred
02261  *       @@foo = 99
02262  *       def foo
02263  *         @@foo
02264  *       end
02265  *     end
02266  *     Fred.class_variable_set(:@@foo, 101)     #=> 101
02267  *     Fred.new.foo                             #=> 101
02268  */
02269 
02270 static VALUE
02271 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
02272 {
02273     ID id = rb_to_id(iv);
02274 
02275     if (!rb_is_class_id(id)) {
02276         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
02277                       QUOTE_ID(id));
02278     }
02279     rb_cvar_set(obj, id, val);
02280     return val;
02281 }
02282 
02283 /*
02284  *  call-seq:
02285  *     obj.class_variable_defined?(symbol)    -> true or false
02286  *
02287  *  Returns <code>true</code> if the given class variable is defined
02288  *  in <i>obj</i>.
02289  *
02290  *     class Fred
02291  *       @@foo = 99
02292  *     end
02293  *     Fred.class_variable_defined?(:@@foo)    #=> true
02294  *     Fred.class_variable_defined?(:@@bar)    #=> false
02295  */
02296 
02297 static VALUE
02298 rb_mod_cvar_defined(VALUE obj, VALUE iv)
02299 {
02300     ID id = rb_check_id(&iv);
02301 
02302     if (!id) {
02303         if (rb_is_class_name(iv)) {
02304             return Qfalse;
02305         }
02306         else {
02307             rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name",
02308                               QUOTE(iv));
02309         }
02310     }
02311     if (!rb_is_class_id(id)) {
02312         rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
02313                       QUOTE_ID(id));
02314     }
02315     return rb_cvar_defined(obj, id);
02316 }
02317 
02318 static struct conv_method_tbl {
02319     const char *method;
02320     ID id;
02321 } conv_method_names[] = {
02322     {"to_int", 0},
02323     {"to_ary", 0},
02324     {"to_str", 0},
02325     {"to_sym", 0},
02326     {"to_hash", 0},
02327     {"to_proc", 0},
02328     {"to_io", 0},
02329     {"to_a", 0},
02330     {"to_s", 0},
02331     {NULL, 0}
02332 };
02333 #define IMPLICIT_CONVERSIONS 7
02334 
02335 static VALUE
02336 convert_type(VALUE val, const char *tname, const char *method, int raise)
02337 {
02338     ID m = 0;
02339     int i;
02340     VALUE r;
02341 
02342     for (i=0; conv_method_names[i].method; i++) {
02343         if (conv_method_names[i].method[0] == method[0] &&
02344             strcmp(conv_method_names[i].method, method) == 0) {
02345             m = conv_method_names[i].id;
02346             break;
02347         }
02348     }
02349     if (!m) m = rb_intern(method);
02350     r = rb_check_funcall(val, m, 0, 0);
02351     if (r == Qundef) {
02352         if (raise) {
02353             const char *msg = i < IMPLICIT_CONVERSIONS ?
02354                 "no implicit conversion of" : "can't convert";
02355             const char *cname = NIL_P(val) ? "nil" :
02356                 val == Qtrue ? "true" :
02357                 val == Qfalse ? "false" :
02358                 NULL;
02359             if (cname)
02360                 rb_raise(rb_eTypeError, "%s %s into %s", msg, cname, tname);
02361             rb_raise(rb_eTypeError, "%s %"PRIsVALUE" into %s", msg,
02362                      rb_obj_class(val),
02363                      tname);
02364         }
02365         return Qnil;
02366     }
02367     return r;
02368 }
02369 
02370 NORETURN(static void conversion_mismatch(VALUE, const char *, const char *, VALUE));
02371 static void
02372 conversion_mismatch(VALUE val, const char *tname, const char *method, VALUE result)
02373 {
02374     VALUE cname = rb_obj_class(val);
02375     rb_raise(rb_eTypeError,
02376              "can't convert %"PRIsVALUE" to %s (%"PRIsVALUE"#%s gives %"PRIsVALUE")",
02377              cname, tname, cname, method, rb_obj_class(result));
02378 }
02379 
02380 VALUE
02381 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
02382 {
02383     VALUE v;
02384 
02385     if (TYPE(val) == type) return val;
02386     v = convert_type(val, tname, method, TRUE);
02387     if (TYPE(v) != type) {
02388         conversion_mismatch(val, tname, method, v);
02389     }
02390     return v;
02391 }
02392 
02393 VALUE
02394 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
02395 {
02396     VALUE v;
02397 
02398     /* always convert T_DATA */
02399     if (TYPE(val) == type && type != T_DATA) return val;
02400     v = convert_type(val, tname, method, FALSE);
02401     if (NIL_P(v)) return Qnil;
02402     if (TYPE(v) != type) {
02403         conversion_mismatch(val, tname, method, v);
02404     }
02405     return v;
02406 }
02407 
02408 
02409 static VALUE
02410 rb_to_integer(VALUE val, const char *method)
02411 {
02412     VALUE v;
02413 
02414     if (FIXNUM_P(val)) return val;
02415     if (RB_TYPE_P(val, T_BIGNUM)) return val;
02416     v = convert_type(val, "Integer", method, TRUE);
02417     if (!rb_obj_is_kind_of(v, rb_cInteger)) {
02418         conversion_mismatch(val, "Integer", method, v);
02419     }
02420     return v;
02421 }
02422 
02423 VALUE
02424 rb_check_to_integer(VALUE val, const char *method)
02425 {
02426     VALUE v;
02427 
02428     if (FIXNUM_P(val)) return val;
02429     if (RB_TYPE_P(val, T_BIGNUM)) return val;
02430     v = convert_type(val, "Integer", method, FALSE);
02431     if (!rb_obj_is_kind_of(v, rb_cInteger)) {
02432         return Qnil;
02433     }
02434     return v;
02435 }
02436 
02437 VALUE
02438 rb_to_int(VALUE val)
02439 {
02440     return rb_to_integer(val, "to_int");
02441 }
02442 
02443 VALUE
02444 rb_check_to_int(VALUE val)
02445 {
02446     return rb_check_to_integer(val, "to_int");
02447 }
02448 
02449 static VALUE
02450 rb_convert_to_integer(VALUE val, int base)
02451 {
02452     VALUE tmp;
02453 
02454     switch (TYPE(val)) {
02455       case T_FLOAT:
02456         if (base != 0) goto arg_error;
02457         if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX
02458             && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) {
02459             break;
02460         }
02461         return rb_dbl2big(RFLOAT_VALUE(val));
02462 
02463       case T_FIXNUM:
02464       case T_BIGNUM:
02465         if (base != 0) goto arg_error;
02466         return val;
02467 
02468       case T_STRING:
02469       string_conv:
02470         return rb_str_to_inum(val, base, TRUE);
02471 
02472       case T_NIL:
02473         if (base != 0) goto arg_error;
02474         rb_raise(rb_eTypeError, "can't convert nil into Integer");
02475         break;
02476 
02477       default:
02478         break;
02479     }
02480     if (base != 0) {
02481         tmp = rb_check_string_type(val);
02482         if (!NIL_P(tmp)) goto string_conv;
02483       arg_error:
02484         rb_raise(rb_eArgError, "base specified for non string value");
02485     }
02486     tmp = convert_type(val, "Integer", "to_int", FALSE);
02487     if (NIL_P(tmp)) {
02488         return rb_to_integer(val, "to_i");
02489     }
02490     return tmp;
02491 
02492 }
02493 
02494 VALUE
02495 rb_Integer(VALUE val)
02496 {
02497     return rb_convert_to_integer(val, 0);
02498 }
02499 
02500 /*
02501  *  call-seq:
02502  *     Integer(arg,base=0)    -> integer
02503  *
02504  *  Converts <i>arg</i> to a <code>Fixnum</code> or <code>Bignum</code>.
02505  *  Numeric types are converted directly (with floating point numbers
02506  *  being truncated).    <i>base</i> (0, or between 2 and 36) is a base for
02507  *  integer string representation.  If <i>arg</i> is a <code>String</code>,
02508  *  when <i>base</i> is omitted or equals to zero, radix indicators
02509  *  (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
02510  *  In any case, strings should be strictly conformed to numeric
02511  *  representation. This behavior is different from that of
02512  *  <code>String#to_i</code>.  Non string values will be converted using
02513  *  <code>to_int</code>, and <code>to_i</code>.
02514  *
02515  *     Integer(123.999)    #=> 123
02516  *     Integer("0x1a")     #=> 26
02517  *     Integer(Time.new)   #=> 1204973019
02518  *     Integer("0930", 10) #=> 930
02519  *     Integer("111", 2)   #=> 7
02520  */
02521 
02522 static VALUE
02523 rb_f_integer(int argc, VALUE *argv, VALUE obj)
02524 {
02525     VALUE arg = Qnil;
02526     int base = 0;
02527 
02528     switch (argc) {
02529       case 2:
02530         base = NUM2INT(argv[1]);
02531       case 1:
02532         arg = argv[0];
02533         break;
02534       default:
02535         /* should cause ArgumentError */
02536         rb_scan_args(argc, argv, "11", NULL, NULL);
02537     }
02538     return rb_convert_to_integer(arg, base);
02539 }
02540 
02541 double
02542 rb_cstr_to_dbl(const char *p, int badcheck)
02543 {
02544     const char *q;
02545     char *end;
02546     double d;
02547     const char *ellipsis = "";
02548     int w;
02549     enum {max_width = 20};
02550 #define OutOfRange() ((end - p > max_width) ? \
02551                       (w = max_width, ellipsis = "...") : \
02552                       (w = (int)(end - p), ellipsis = ""))
02553 
02554     if (!p) return 0.0;
02555     q = p;
02556     while (ISSPACE(*p)) p++;
02557 
02558     if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
02559         return 0.0;
02560     }
02561 
02562     d = strtod(p, &end);
02563     if (errno == ERANGE) {
02564         OutOfRange();
02565         rb_warning("Float %.*s%s out of range", w, p, ellipsis);
02566         errno = 0;
02567     }
02568     if (p == end) {
02569         if (badcheck) {
02570           bad:
02571             rb_invalid_str(q, "Float()");
02572         }
02573         return d;
02574     }
02575     if (*end) {
02576         char buf[DBL_DIG * 4 + 10];
02577         char *n = buf;
02578         char *e = buf + sizeof(buf) - 1;
02579         char prev = 0;
02580 
02581         while (p < end && n < e) prev = *n++ = *p++;
02582         while (*p) {
02583             if (*p == '_') {
02584                 /* remove underscores between digits */
02585                 if (badcheck) {
02586                     if (n == buf || !ISDIGIT(prev)) goto bad;
02587                     ++p;
02588                     if (!ISDIGIT(*p)) goto bad;
02589                 }
02590                 else {
02591                     while (*++p == '_');
02592                     continue;
02593                 }
02594             }
02595             prev = *p++;
02596             if (n < e) *n++ = prev;
02597         }
02598         *n = '\0';
02599         p = buf;
02600 
02601         if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
02602             return 0.0;
02603         }
02604 
02605         d = strtod(p, &end);
02606         if (errno == ERANGE) {
02607             OutOfRange();
02608             rb_warning("Float %.*s%s out of range", w, p, ellipsis);
02609             errno = 0;
02610         }
02611         if (badcheck) {
02612             if (!end || p == end) goto bad;
02613             while (*end && ISSPACE(*end)) end++;
02614             if (*end) goto bad;
02615         }
02616     }
02617     if (errno == ERANGE) {
02618         errno = 0;
02619         OutOfRange();
02620         rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
02621     }
02622     return d;
02623 }
02624 
02625 double
02626 rb_str_to_dbl(VALUE str, int badcheck)
02627 {
02628     char *s;
02629     long len;
02630     double ret;
02631     VALUE v = 0;
02632 
02633     StringValue(str);
02634     s = RSTRING_PTR(str);
02635     len = RSTRING_LEN(str);
02636     if (s) {
02637         if (badcheck && memchr(s, '\0', len)) {
02638             rb_raise(rb_eArgError, "string for Float contains null byte");
02639         }
02640         if (s[len]) {           /* no sentinel somehow */
02641             char *p =  ALLOCV(v, len);
02642             MEMCPY(p, s, char, len);
02643             p[len] = '\0';
02644             s = p;
02645         }
02646     }
02647     ret = rb_cstr_to_dbl(s, badcheck);
02648     if (v)
02649         ALLOCV_END(v);
02650     return ret;
02651 }
02652 
02653 VALUE
02654 rb_Float(VALUE val)
02655 {
02656     switch (TYPE(val)) {
02657       case T_FIXNUM:
02658         return DBL2NUM((double)FIX2LONG(val));
02659 
02660       case T_FLOAT:
02661         return val;
02662 
02663       case T_BIGNUM:
02664         return DBL2NUM(rb_big2dbl(val));
02665 
02666       case T_STRING:
02667         return DBL2NUM(rb_str_to_dbl(val, TRUE));
02668 
02669       case T_NIL:
02670         rb_raise(rb_eTypeError, "can't convert nil into Float");
02671         break;
02672 
02673       default:
02674         return rb_convert_type(val, T_FLOAT, "Float", "to_f");
02675     }
02676 
02677     UNREACHABLE;
02678 }
02679 
02680 /*
02681  *  call-seq:
02682  *     Float(arg)    -> float
02683  *
02684  *  Returns <i>arg</i> converted to a float. Numeric types are converted
02685  *  directly, the rest are converted using <i>arg</i>.to_f. As of Ruby
02686  *  1.8, converting <code>nil</code> generates a <code>TypeError</code>.
02687  *
02688  *     Float(1)           #=> 1.0
02689  *     Float("123.456")   #=> 123.456
02690  */
02691 
02692 static VALUE
02693 rb_f_float(VALUE obj, VALUE arg)
02694 {
02695     return rb_Float(arg);
02696 }
02697 
02698 VALUE
02699 rb_to_float(VALUE val)
02700 {
02701     if (RB_TYPE_P(val, T_FLOAT)) return val;
02702     if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
02703         rb_raise(rb_eTypeError, "can't convert %s into Float",
02704                  NIL_P(val) ? "nil" :
02705                  val == Qtrue ? "true" :
02706                  val == Qfalse ? "false" :
02707                  rb_obj_classname(val));
02708     }
02709     return rb_convert_type(val, T_FLOAT, "Float", "to_f");
02710 }
02711 
02712 VALUE
02713 rb_check_to_float(VALUE val)
02714 {
02715     if (RB_TYPE_P(val, T_FLOAT)) return val;
02716     if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
02717         return Qnil;
02718     }
02719     return rb_check_convert_type(val, T_FLOAT, "Float", "to_f");
02720 }
02721 
02722 double
02723 rb_num2dbl(VALUE val)
02724 {
02725     switch (TYPE(val)) {
02726       case T_FLOAT:
02727         return RFLOAT_VALUE(val);
02728 
02729       case T_STRING:
02730         rb_raise(rb_eTypeError, "no implicit conversion to float from string");
02731         break;
02732 
02733       case T_NIL:
02734         rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
02735         break;
02736 
02737       default:
02738         break;
02739     }
02740 
02741     return RFLOAT_VALUE(rb_Float(val));
02742 }
02743 
02744 VALUE
02745 rb_String(VALUE val)
02746 {
02747     VALUE tmp = rb_check_string_type(val);
02748     if (NIL_P(tmp))
02749         tmp = rb_convert_type(val, T_STRING, "String", "to_s");
02750     return tmp;
02751 }
02752 
02753 
02754 /*
02755  *  call-seq:
02756  *     String(arg)   -> string
02757  *
02758  *  Converts <i>arg</i> to a <code>String</code> by calling its
02759  *  <code>to_s</code> method.
02760  *
02761  *     String(self)        #=> "main"
02762  *     String(self.class)  #=> "Object"
02763  *     String(123456)      #=> "123456"
02764  */
02765 
02766 static VALUE
02767 rb_f_string(VALUE obj, VALUE arg)
02768 {
02769     return rb_String(arg);
02770 }
02771 
02772 VALUE
02773 rb_Array(VALUE val)
02774 {
02775     VALUE tmp = rb_check_array_type(val);
02776 
02777     if (NIL_P(tmp)) {
02778         tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
02779         if (NIL_P(tmp)) {
02780             return rb_ary_new3(1, val);
02781         }
02782     }
02783     return tmp;
02784 }
02785 
02786 /*
02787  *  call-seq:
02788  *     Array(arg)    -> array
02789  *
02790  *  Returns +arg+ as an Array.
02791  *
02792  *  First tries to call Array#to_ary on +arg+, then Array#to_a.
02793  *
02794  *     Array(1..5)   #=> [1, 2, 3, 4, 5]
02795  */
02796 
02797 static VALUE
02798 rb_f_array(VALUE obj, VALUE arg)
02799 {
02800     return rb_Array(arg);
02801 }
02802 
02803 VALUE
02804 rb_Hash(VALUE val)
02805 {
02806     VALUE tmp;
02807 
02808     if (NIL_P(val)) return rb_hash_new();
02809     tmp = rb_check_hash_type(val);
02810     if (NIL_P(tmp)) {
02811         if (RB_TYPE_P(val, T_ARRAY) && RARRAY_LEN(val) == 0)
02812             return rb_hash_new();
02813         rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
02814     }
02815     return tmp;
02816 }
02817 
02818 /*
02819  *  call-seq:
02820  *     Hash(arg)    -> hash
02821  *
02822  *  Converts <i>arg</i> to a <code>Hash</code> by calling
02823  *  <i>arg</i><code>.to_hash</code>. Returns an empty <code>Hash</code> when
02824  *  <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>.
02825  *
02826  *     Hash([])          #=> {}
02827  *     Hash(nil)         #=> nil
02828  *     Hash(key: :value) #=> {:key => :value}
02829  *     Hash([1, 2, 3])   #=> TypeError
02830  */
02831 
02832 static VALUE
02833 rb_f_hash(VALUE obj, VALUE arg)
02834 {
02835     return rb_Hash(arg);
02836 }
02837 
02838 /*
02839  *  Document-class: Class
02840  *
02841  *  Classes in Ruby are first-class objects---each is an instance of
02842  *  class <code>Class</code>.
02843  *
02844  *  Typically, you create a new class by using:
02845  *
02846  *    class Name
02847  *     # some class describing the class behavior
02848  *    end
02849  *
02850  *  When a new class is created, an object of type Class is initialized and
02851  *  assigned to a global constant (<code>Name</code> in this case).
02852  *
02853  *  When <code>Name.new</code> is called to create a new object, the
02854  *  <code>new</code> method in <code>Class</code> is run by default.
02855  *  This can be demonstrated by overriding <code>new</code> in
02856  *  <code>Class</code>:
02857  *
02858  *     class Class
02859  *        alias oldNew  new
02860  *        def new(*args)
02861  *          print "Creating a new ", self.name, "\n"
02862  *          oldNew(*args)
02863  *        end
02864  *      end
02865  *
02866  *
02867  *      class Name
02868  *      end
02869  *
02870  *
02871  *      n = Name.new
02872  *
02873  *  <em>produces:</em>
02874  *
02875  *     Creating a new Name
02876  *
02877  *  Classes, modules, and objects are interrelated. In the diagram
02878  *  that follows, the vertical arrows represent inheritance, and the
02879  *  parentheses meta-classes. All metaclasses are instances
02880  *  of the class `Class'.
02881  *                             +---------+             +-...
02882  *                             |         |             |
02883  *             BasicObject-----|-->(BasicObject)-------|-...
02884  *                 ^           |         ^             |
02885  *                 |           |         |             |
02886  *              Object---------|----->(Object)---------|-...
02887  *                 ^           |         ^             |
02888  *                 |           |         |             |
02889  *                 +-------+   |         +--------+    |
02890  *                 |       |   |         |        |    |
02891  *                 |    Module-|---------|--->(Module)-|-...
02892  *                 |       ^   |         |        ^    |
02893  *                 |       |   |         |        |    |
02894  *                 |     Class-|---------|---->(Class)-|-...
02895  *                 |       ^   |         |        ^    |
02896  *                 |       +---+         |        +----+
02897  *                 |                     |
02898  *    obj--->OtherClass---------->(OtherClass)-----------...
02899  *
02900  */
02901 
02902 
02921 /*  Document-class: BasicObject
02922  *
02923  *  BasicObject is the parent class of all classes in Ruby.  It's an explicit
02924  *  blank class.
02925  *
02926  *  BasicObject can be used for creating object hierarchies independent of
02927  *  Ruby's object hierarchy, proxy objects like the Delegator class, or other
02928  *  uses where namespace pollution from Ruby's methods and classes must be
02929  *  avoided.
02930  *
02931  *  To avoid polluting BasicObject for other users an appropriately named
02932  *  subclass of BasicObject should be created instead of directly modifying
02933  *  BasicObject:
02934  *
02935  *    class MyObjectSystem < BasicObject
02936  *    end
02937  *
02938  *  BasicObject does not include Kernel (for methods like +puts+) and
02939  *  BasicObject is outside of the namespace of the standard library so common
02940  *  classes will not be found without a using a full class path.
02941  *
02942  *  A variety of strategies can be used to provide useful portions of the
02943  *  standard library to subclasses of BasicObject.  A subclass could
02944  *  <code>include Kernel</code> to obtain +puts+, +exit+, etc.  A custom
02945  *  Kernel-like module could be created and included or delegation can be used
02946  *  via #method_missing:
02947  *
02948  *    class MyObjectSystem < BasicObject
02949  *      DELEGATE = [:puts, :p]
02950  *
02951  *      def method_missing(name, *args, &block)
02952  *        super unless DELEGATE.include? name
02953  *        ::Kernel.send(name, *args, &block)
02954  *      end
02955  *
02956  *      def respond_to_missing?(name, include_private = false)
02957  *        DELEGATE.include?(name) or super
02958  *      end
02959  *    end
02960  *
02961  *  Access to classes and modules from the Ruby standard library can be
02962  *  obtained in a BasicObject subclass by referencing the desired constant
02963  *  from the root like <code>::File</code> or <code>::Enumerator</code>.
02964  *  Like #method_missing, #const_missing can be used to delegate constant
02965  *  lookup to +Object+:
02966  *
02967  *    class MyObjectSystem < BasicObject
02968  *      def self.const_missing(name)
02969  *        ::Object.const_get(name)
02970  *      end
02971  *    end
02972  */
02973 
02974 /*  Document-class: Object
02975  *
02976  *  Object is the default root of all Ruby objects.  Object inherits from
02977  *  BasicObject which allows creating alternate object hierarchies.  Methods
02978  *  on object are available to all classes unless explicitly overridden.
02979  *
02980  *  Object mixes in the Kernel module, making the built-in kernel functions
02981  *  globally accessible.  Although the instance methods of Object are defined
02982  *  by the Kernel module, we have chosen to document them here for clarity.
02983  *
02984  *  When referencing constants in classes inheriting from Object you do not
02985  *  need to use the full namespace.  For example, referencing +File+ inside
02986  *  +YourClass+ will find the top-level File class.
02987  *
02988  *  In the descriptions of Object's methods, the parameter <i>symbol</i> refers
02989  *  to a symbol, which is either a quoted string or a Symbol (such as
02990  *  <code>:name</code>).
02991  */
02992 
02993 void
02994 Init_Object(void)
02995 {
02996     int i;
02997 
02998     Init_class_hierarchy();
02999 
03000 #if 0
03001     // teach RDoc about these classes
03002     rb_cBasicObject = rb_define_class("BasicObject", Qnil);
03003     rb_cObject = rb_define_class("Object", rb_cBasicObject);
03004     rb_cModule = rb_define_class("Module", rb_cObject);
03005     rb_cClass =  rb_define_class("Class",  rb_cModule);
03006 #endif
03007 
03008 #undef rb_intern
03009 #define rb_intern(str) rb_intern_const(str)
03010 
03011     rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0);
03012     rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
03013     rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
03014     rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
03015     rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
03016     rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);
03017 
03018     rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1);
03019     rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1);
03020     rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
03021 
03022     /* Document-module: Kernel
03023      *
03024      * The Kernel module is included by class Object, so its methods are
03025      * available in every Ruby object.
03026      *
03027      * The Kernel instance methods are documented in class Object while the
03028      * module methods are documented here.  These methods are called without a
03029      * receiver and thus can be called in functional form:
03030      *
03031      *   sprintf "%.1f", 1.234 #=> "1.2"
03032      *
03033      */
03034     rb_mKernel = rb_define_module("Kernel");
03035     rb_include_module(rb_cObject, rb_mKernel);
03036     rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1);
03037     rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1);
03038     rb_define_private_method(rb_cModule, "extended", rb_obj_dummy, 1);
03039     rb_define_private_method(rb_cModule, "prepended", rb_obj_dummy, 1);
03040     rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
03041     rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
03042     rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
03043 
03044     rb_define_method(rb_mKernel, "nil?", rb_false, 0);
03045     rb_define_method(rb_mKernel, "===", rb_equal, 1);
03046     rb_define_method(rb_mKernel, "=~", rb_obj_match, 1);
03047     rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
03048     rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
03049     rb_define_method(rb_mKernel, "hash", rb_obj_hash, 0);
03050     rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
03051 
03052     rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
03053     rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
03054     rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
03055     rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
03056     rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
03057     rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
03058     rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1);
03059 
03060     rb_define_method(rb_mKernel, "taint", rb_obj_taint, 0);
03061     rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
03062     rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
03063     rb_define_method(rb_mKernel, "untrust", rb_obj_untrust, 0);
03064     rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0);
03065     rb_define_method(rb_mKernel, "trust", rb_obj_trust, 0);
03066     rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
03067     rb_define_method(rb_mKernel, "frozen?", rb_obj_frozen_p, 0);
03068 
03069     rb_define_method(rb_mKernel, "to_s", rb_any_to_s, 0);
03070     rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
03071     rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */
03072     rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
03073     rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
03074     rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */
03075     rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */
03076     rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
03077     rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
03078     rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
03079     rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
03080     rb_define_method(rb_mKernel, "remove_instance_variable",
03081                      rb_obj_remove_instance_variable, 1); /* in variable.c */
03082 
03083     rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
03084     rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
03085     rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
03086     rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
03087 
03088     rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */
03089     rb_define_global_function("format", rb_f_sprintf, -1);  /* in sprintf.c */
03090 
03091     rb_define_global_function("Integer", rb_f_integer, -1);
03092     rb_define_global_function("Float", rb_f_float, 1);
03093 
03094     rb_define_global_function("String", rb_f_string, 1);
03095     rb_define_global_function("Array", rb_f_array, 1);
03096     rb_define_global_function("Hash", rb_f_hash, 1);
03097 
03098     rb_cNilClass = rb_define_class("NilClass", rb_cObject);
03099     rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
03100     rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
03101     rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
03102     rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
03103     rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0);
03104     rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
03105     rb_define_method(rb_cNilClass, "&", false_and, 1);
03106     rb_define_method(rb_cNilClass, "|", false_or, 1);
03107     rb_define_method(rb_cNilClass, "^", false_xor, 1);
03108 
03109     rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
03110     rb_undef_alloc_func(rb_cNilClass);
03111     rb_undef_method(CLASS_OF(rb_cNilClass), "new");
03112     /*
03113      * An alias of +nil+
03114      */
03115     rb_define_global_const("NIL", Qnil);
03116 
03117     rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
03118     rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
03119     rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
03120     rb_define_method(rb_cModule, "<=>",  rb_mod_cmp, 1);
03121     rb_define_method(rb_cModule, "<",  rb_mod_lt, 1);
03122     rb_define_method(rb_cModule, "<=", rb_class_inherited_p, 1);
03123     rb_define_method(rb_cModule, ">",  rb_mod_gt, 1);
03124     rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
03125     rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
03126     rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
03127     rb_define_alias(rb_cModule, "inspect", "to_s");
03128     rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
03129     rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
03130     rb_define_method(rb_cModule, "name", rb_mod_name, 0);  /* in variable.c */
03131     rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
03132 
03133     rb_define_private_method(rb_cModule, "attr", rb_mod_attr, -1);
03134     rb_define_private_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
03135     rb_define_private_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
03136     rb_define_private_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
03137 
03138     rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
03139     rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
03140     rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
03141     rb_define_method(rb_cModule, "public_instance_methods",
03142                      rb_class_public_instance_methods, -1);    /* in class.c */
03143     rb_define_method(rb_cModule, "protected_instance_methods",
03144                      rb_class_protected_instance_methods, -1); /* in class.c */
03145     rb_define_method(rb_cModule, "private_instance_methods",
03146                      rb_class_private_instance_methods, -1);   /* in class.c */
03147 
03148     rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
03149     rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
03150     rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
03151     rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
03152     rb_define_private_method(rb_cModule, "remove_const",
03153                              rb_mod_remove_const, 1); /* in variable.c */
03154     rb_define_method(rb_cModule, "const_missing",
03155                      rb_mod_const_missing, 1); /* in variable.c */
03156     rb_define_method(rb_cModule, "class_variables",
03157                      rb_mod_class_variables, -1); /* in variable.c */
03158     rb_define_method(rb_cModule, "remove_class_variable",
03159                      rb_mod_remove_cvar, 1); /* in variable.c */
03160     rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
03161     rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
03162     rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
03163     rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); /* in variable.c */
03164     rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); /* in variable.c */
03165 
03166     rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
03167     rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
03168     rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
03169     rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
03170     rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
03171     rb_undef_method(rb_cClass, "extend_object");
03172     rb_undef_method(rb_cClass, "append_features");
03173     rb_undef_method(rb_cClass, "prepend_features");
03174 
03175     /*
03176      * Document-class: Data
03177      *
03178      * This is a recommended base class for C extensions using Data_Make_Struct
03179      * or Data_Wrap_Struct, see README.EXT for details.
03180      */
03181     rb_cData = rb_define_class("Data", rb_cObject);
03182     rb_undef_alloc_func(rb_cData);
03183 
03184     rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
03185     rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
03186     rb_define_alias(rb_cTrueClass, "inspect", "to_s");
03187     rb_define_method(rb_cTrueClass, "&", true_and, 1);
03188     rb_define_method(rb_cTrueClass, "|", true_or, 1);
03189     rb_define_method(rb_cTrueClass, "^", true_xor, 1);
03190     rb_undef_alloc_func(rb_cTrueClass);
03191     rb_undef_method(CLASS_OF(rb_cTrueClass), "new");
03192     /*
03193      * An alias of +true+
03194      */
03195     rb_define_global_const("TRUE", Qtrue);
03196 
03197     rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
03198     rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
03199     rb_define_alias(rb_cFalseClass, "inspect", "to_s");
03200     rb_define_method(rb_cFalseClass, "&", false_and, 1);
03201     rb_define_method(rb_cFalseClass, "|", false_or, 1);
03202     rb_define_method(rb_cFalseClass, "^", false_xor, 1);
03203     rb_undef_alloc_func(rb_cFalseClass);
03204     rb_undef_method(CLASS_OF(rb_cFalseClass), "new");
03205     /*
03206      * An alias of +false+
03207      */
03208     rb_define_global_const("FALSE", Qfalse);
03209 
03210     id_eq = rb_intern("==");
03211     id_eql = rb_intern("eql?");
03212     id_match = rb_intern("=~");
03213     id_inspect = rb_intern("inspect");
03214     id_init_copy = rb_intern("initialize_copy");
03215     id_init_clone = rb_intern("initialize_clone");
03216     id_init_dup = rb_intern("initialize_dup");
03217     id_const_missing = rb_intern("const_missing");
03218 
03219     for (i=0; conv_method_names[i].method; i++) {
03220         conv_method_names[i].id = rb_intern(conv_method_names[i].method);
03221     }
03222 }
03223