Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# object.c

Go to the documentation of this file.

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