Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals

object.c

Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   object.c -
00004 
00005   $Author: akr $
00006   $Date: 2005/12/12 03:36:49 $
00007   created at: Thu Jul 15 12:01:24 JST 1993
00008 
00009   Copyright (C) 1993-2003 Yukihiro Matsumoto
00010   Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
00011   Copyright (C) 2000  Information-technology Promotion Agency, Japan
00012 
00013 **********************************************************************/
00014 
00015 #include "ruby.h"
00016 #include "st.h"
00017 #include "util.h"
00018 #include <stdio.h>
00019 #include <errno.h>
00020 #include <ctype.h>
00021 #include <math.h>
00022 
00023 VALUE rb_mKernel;
00024 VALUE rb_cObject;
00025 VALUE rb_cModule;
00026 VALUE rb_cClass;
00027 VALUE rb_cData;
00028 
00029 VALUE rb_cNilClass;
00030 VALUE rb_cTrueClass;
00031 VALUE rb_cFalseClass;
00032 VALUE rb_cSymbol;
00033 
00034 static ID id_eq, id_eql, id_inspect, id_init_copy;
00035 
00036 /*
00037  *  call-seq:
00038  *     obj === other   => true or false
00039  *  
00040  *  Case Equality---For class <code>Object</code>, effectively the same
00041  *  as calling  <code>#==</code>, but typically overridden by descendents
00042  *  to provide meaningful semantics in <code>case</code> statements.
00043  */
00044 
00045 VALUE
00046 rb_equal(obj1, obj2)
00047     VALUE obj1, 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(obj1, obj2)
00059     VALUE obj1, obj2;
00060 {
00061     return RTEST(rb_funcall(obj1, id_eql, 1, obj2));
00062 }
00063 
00064 /*
00065  *  call-seq:
00066  *     obj == other        => true or false
00067  *     obj.equal?(other)   => true or false
00068  *     obj.eql?(other)     => true or false
00069  *  
00070  *  Equality---At the <code>Object</code> level, <code>==</code> returns
00071  *  <code>true</code> only if <i>obj</i> and <i>other</i> are the
00072  *  same object. Typically, this method is overridden in descendent
00073  *  classes to provide class-specific meaning.
00074  *
00075  *  Unlike <code>==</code>, the <code>equal?</code> method should never be
00076  *  overridden by subclasses: it is used to determine object identity
00077  *  (that is, <code>a.equal?(b)</code> iff <code>a</code> is the same
00078  *  object as <code>b</code>).
00079  *
00080  *  The <code>eql?</code> method returns <code>true</code> if
00081     <i>obj</i> and <i>anObject</i> have the
00082  *  same value. Used by <code>Hash</code> to test members for equality.
00083  *  For objects of class <code>Object</code>, <code>eql?</code> is
00084  *  synonymous with <code>==</code>. Subclasses normally continue this
00085  *  tradition, but there are exceptions. <code>Numeric</code> types, for
00086  *  example, perform type conversion across <code>==</code>, but not
00087  *  across <code>eql?</code>, so:
00088  *     
00089  *     1 == 1.0     #=> true
00090  *     1.eql? 1.0   #=> false
00091  */
00092 
00093 static VALUE
00094 rb_obj_equal(obj1, obj2)
00095     VALUE obj1, obj2;
00096 {
00097     if (obj1 == obj2) return Qtrue;
00098     return Qfalse;
00099 }
00100 
00101 
00102 /*
00103  *  Document-method: __id__
00104  *  Document-method: object_id
00105  *
00106  *  call-seq:
00107  *     obj.__id__       => fixnum
00108  *     obj.object_id    => fixnum
00109  *  
00110  *  Returns an integer identifier for <i>obj</i>. The same number will
00111  *  be returned on all calls to <code>id</code> for a given object, and
00112  *  no two active objects will share an id.
00113  *  <code>Object#object_id</code> is a different concept from the
00114  *  <code>:name</code> notation, which returns the symbol id of
00115  *  <code>name</code>. Replaces the deprecated <code>Object#id</code>.
00116  */
00117 
00118 
00119 
00120 /*
00121  *  call-seq:
00122  *     obj.hash    => fixnum
00123  *  
00124  *  Generates a <code>Fixnum</code> hash value for this object. This
00125  *  function must have the property that <code>a.eql?(b)</code> implies
00126  *  <code>a.hash == b.hash</code>. The hash value is used by class
00127  *  <code>Hash</code>. Any hash value that exceeds the capacity of a
00128  *  <code>Fixnum</code> will be truncated before being used.
00129  */
00130 
00131 VALUE
00132 rb_obj_id(obj)
00133     VALUE obj;
00134 {
00135     if (SPECIAL_CONST_P(obj)) {
00136         return LONG2NUM((long)obj);
00137     }
00138     return (VALUE)((long)obj|FIXNUM_FLAG);
00139 }
00140 
00141 /*
00142  *  call-seq:
00143  *     obj.id    => fixnum
00144  *  
00145  *  Soon-to-be deprecated version of <code>Object#object_id</code>.
00146  */
00147 
00148 VALUE
00149 rb_obj_id_obsolete(obj)
00150     VALUE obj;
00151 {
00152     rb_warn("Object#id will be deprecated; use Object#object_id");
00153     return rb_obj_id(obj);
00154 }
00155 
00156 VALUE
00157 rb_class_real(cl)
00158     VALUE cl;
00159 {
00160     while (FL_TEST(cl, FL_SINGLETON) || TYPE(cl) == T_ICLASS) {
00161         cl = RCLASS(cl)->super;
00162     }
00163     return cl;
00164 }
00165 
00166 /*
00167  *  call-seq:
00168  *     obj.type   => class
00169  *  
00170  *  Deprecated synonym for <code>Object#class</code>.
00171  */
00172 
00173 VALUE
00174 rb_obj_type(obj)
00175     VALUE obj;
00176 {
00177     rb_warn("Object#type is deprecated; use Object#class");
00178     return rb_class_real(CLASS_OF(obj));
00179 }
00180 
00181 
00182 /*
00183  *  call-seq:
00184  *     obj.class    => class
00185  *  
00186  *  Returns the class of <i>obj</i>, now preferred over
00187  *  <code>Object#type</code>, as an object's type in Ruby is only
00188  *  loosely tied to that object's class. This method must always be
00189  *  called with an explicit receiver, as <code>class</code> is also a
00190  *  reserved word in Ruby.
00191  *     
00192  *     1.class      #=> Fixnum
00193  *     self.class   #=> Object
00194  */
00195 
00196 VALUE
00197 rb_obj_class(obj)
00198     VALUE obj;
00199 {
00200     return rb_class_real(CLASS_OF(obj));
00201 }
00202 
00203 static void
00204 init_copy(dest, obj)
00205     VALUE dest, obj;
00206 {
00207     if (OBJ_FROZEN(dest)) {
00208         rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
00209     }
00210     RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
00211     RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR|FL_TAINT);
00212     if (FL_TEST(obj, FL_EXIVAR)) {
00213         rb_copy_generic_ivar(dest, obj);
00214     }
00215     rb_gc_copy_finalizer(dest, obj);
00216     switch (TYPE(obj)) {
00217       case T_OBJECT:
00218       case T_CLASS:
00219       case T_MODULE:
00220         if (ROBJECT(dest)->iv_tbl) {
00221             st_free_table(ROBJECT(dest)->iv_tbl);
00222             ROBJECT(dest)->iv_tbl = 0;
00223         }
00224         if (ROBJECT(obj)->iv_tbl) {
00225             ROBJECT(dest)->iv_tbl = st_copy(ROBJECT(obj)->iv_tbl);
00226         }
00227     }
00228     rb_funcall(dest, id_init_copy, 1, obj);
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(obj)
00257     VALUE obj;
00258 {
00259     VALUE clone;
00260 
00261     if (rb_special_const_p(obj)) {
00262         rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
00263     }
00264     clone = rb_obj_alloc(rb_obj_class(obj));
00265     RBASIC(clone)->klass = rb_singleton_class_clone(obj);
00266     RBASIC(clone)->flags = (RBASIC(obj)->flags | FL_TEST(clone, FL_TAINT)) & ~(FL_FREEZE|FL_FINALIZE);
00267     init_copy(clone, 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 descendent 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 descendent 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(obj)
00293     VALUE obj;
00294 {
00295     VALUE dup;
00296 
00297     if (rb_special_const_p(obj)) {
00298         rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj));
00299     }
00300     dup = rb_obj_alloc(rb_obj_class(obj));
00301     init_copy(dup, obj);
00302 
00303     return dup;
00304 }
00305 
00306 /* :nodoc: */
00307 VALUE
00308 rb_obj_init_copy(obj, orig)
00309     VALUE obj, orig;
00310 {
00311     if (obj == orig) return obj;
00312     rb_check_frozen(obj);
00313     if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
00314         rb_raise(rb_eTypeError, "initialize_copy should take same class object");
00315     }
00316     return obj;
00317 }
00318 
00319 /*
00320  *  call-seq:
00321  *     obj.to_a -> anArray
00322  *  
00323  *  Returns an array representation of <i>obj</i>. For objects of class
00324  *  <code>Object</code> and others that don't explicitly override the
00325  *  method, the return value is an array containing <code>self</code>. 
00326  *  However, this latter behavior will soon be obsolete.
00327  *     
00328  *     self.to_a       #=> -:1: warning: default `to_a' will be obsolete
00329  *     "hello".to_a    #=> ["hello"]
00330  *     Time.new.to_a   #=> [39, 54, 8, 9, 4, 2003, 3, 99, true, "CDT"]
00331  */
00332 
00333 
00334 static VALUE
00335 rb_any_to_a(obj)
00336     VALUE obj;
00337 {
00338     rb_warn("default `to_a' will be obsolete");
00339     return rb_ary_new3(1, obj);
00340 }
00341 
00342 
00343 /*
00344  *  call-seq:
00345  *     obj.to_s    => string
00346  *  
00347  *  Returns a string representing <i>obj</i>. The default
00348  *  <code>to_s</code> prints the object's class and an encoding of the
00349  *  object id. As a special case, the top-level object that is the
00350  *  initial execution context of Ruby programs returns ``main.''
00351  */
00352 
00353 VALUE
00354 rb_any_to_s(obj)
00355     VALUE obj;
00356 {
00357     char *cname = rb_obj_classname(obj);
00358     size_t len;
00359     VALUE str;
00360 
00361     len = strlen(cname)+6+16;
00362     str = rb_str_new(0, len); /* 6:tags 16:addr */
00363     snprintf(RSTRING(str)->ptr, len+1, "#<%s:0x%lx>", cname, obj);
00364     RSTRING(str)->len = strlen(RSTRING(str)->ptr);
00365     if (OBJ_TAINTED(obj)) OBJ_TAINT(str);
00366 
00367     return str;
00368 }
00369 
00370 VALUE
00371 rb_inspect(obj)
00372     VALUE obj;
00373 {
00374     return rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
00375 }
00376 
00377 static int
00378 inspect_i(id, value, str)
00379     ID id;
00380     VALUE value;
00381     VALUE str;
00382 {
00383     VALUE str2;
00384     char *ivname;
00385 
00386     /* need not to show internal data */
00387     if (CLASS_OF(value) == 0) return ST_CONTINUE;
00388     if (!rb_is_instance_id(id)) return ST_CONTINUE;
00389     if (RSTRING(str)->ptr[0] == '-') { /* first element */
00390         RSTRING(str)->ptr[0] = '#';
00391         rb_str_cat2(str, " ");
00392     }
00393     else {
00394         rb_str_cat2(str, ", ");
00395     }
00396     ivname = rb_id2name(id);
00397     rb_str_cat2(str, ivname);
00398     rb_str_cat2(str, "=");
00399     str2 = rb_inspect(value);
00400     rb_str_append(str, str2);
00401     OBJ_INFECT(str, str2);
00402 
00403     return ST_CONTINUE;
00404 }
00405 
00406 static VALUE
00407 inspect_obj(obj, str)
00408     VALUE obj, str;
00409 {
00410     st_foreach_safe(ROBJECT(obj)->iv_tbl, inspect_i, str);
00411     rb_str_cat2(str, ">");
00412     RSTRING(str)->ptr[0] = '#';
00413     OBJ_INFECT(str, obj);
00414 
00415     return str;
00416 }
00417 
00418 /*
00419  *  call-seq:
00420  *     obj.inspect   => string
00421  *  
00422  *  Returns a string containing a human-readable representation of
00423  *  <i>obj</i>. If not overridden, uses the <code>to_s</code> method to
00424  *  generate the string.
00425  *     
00426  *     [ 1, 2, 3..4, 'five' ].inspect   #=> "[1, 2, 3..4, \"five\"]"
00427  *     Time.new.inspect                 #=> "Wed Apr 09 08:54:39 CDT 2003"
00428  */
00429 
00430 
00431 static VALUE
00432 rb_obj_inspect(obj)
00433     VALUE obj;
00434 {
00435     if (TYPE(obj) == T_OBJECT
00436         && ROBJECT(obj)->iv_tbl
00437         && ROBJECT(obj)->iv_tbl->num_entries > 0) {
00438         VALUE str;
00439         size_t len;
00440         char *c;
00441 
00442         c = rb_obj_classname(obj);
00443         if (rb_inspecting_p(obj)) {
00444             len = strlen(c)+10+16+1;
00445             str = rb_str_new(0, len); /* 10:tags 16:addr 1:nul */
00446             snprintf(RSTRING(str)->ptr, len, "#<%s:0x%lx ...>", c, obj);
00447             RSTRING(str)->len = strlen(RSTRING(str)->ptr);
00448             return str;
00449         }
00450         len = strlen(c)+6+16+1;
00451         str = rb_str_new(0, len);     /* 6:tags 16:addr 1:nul */
00452         snprintf(RSTRING(str)->ptr, len, "-<%s:0x%lx", c, obj);
00453         RSTRING(str)->len = strlen(RSTRING(str)->ptr);
00454         return rb_protect_inspect(inspect_obj, obj, str);
00455     }
00456     return rb_funcall(obj, rb_intern("to_s"), 0, 0);
00457 }
00458 
00459 
00460 /*
00461  *  call-seq:
00462  *     obj.instance_of?(class)    => true or false
00463  *  
00464  *  Returns <code>true</code> if <i>obj</i> is an instance of the given
00465  *  class. See also <code>Object#kind_of?</code>.
00466  */
00467 
00468 VALUE
00469 rb_obj_is_instance_of(obj, c)
00470     VALUE obj, c;
00471 {
00472     switch (TYPE(c)) {
00473       case T_MODULE:
00474       case T_CLASS:
00475       case T_ICLASS:
00476         break;
00477       default:
00478         rb_raise(rb_eTypeError, "class or module required");
00479     }
00480 
00481     if (rb_obj_class(obj) == c) return Qtrue;
00482     return Qfalse;
00483 }
00484 
00485 
00486 /*
00487  *  call-seq:
00488  *     obj.is_a?(class)       => true or false
00489  *     obj.kind_of?(class)    => true or false
00490  *  
00491  *  Returns <code>true</code> if <i>class</i> is the class of
00492  *  <i>obj</i>, or if <i>class</i> is one of the superclasses of
00493  *  <i>obj</i> or modules included in <i>obj</i>.
00494  *     
00495  *     module M;    end
00496  *     class A
00497  *       include M
00498  *     end
00499  *     class B < A; end
00500  *     class C < B; end
00501  *     b = B.new
00502  *     b.instance_of? A   #=> false
00503  *     b.instance_of? B   #=> true
00504  *     b.instance_of? C   #=> false
00505  *     b.instance_of? M   #=> false
00506  *     b.kind_of? A       #=> true
00507  *     b.kind_of? B       #=> true
00508  *     b.kind_of? C       #=> false
00509  *     b.kind_of? M       #=> true
00510  */
00511 
00512 VALUE
00513 rb_obj_is_kind_of(obj, c)
00514     VALUE obj, c;
00515 {
00516     VALUE cl = CLASS_OF(obj);
00517 
00518     switch (TYPE(c)) {
00519       case T_MODULE:
00520       case T_CLASS:
00521       case T_ICLASS:
00522         break;
00523 
00524       default:
00525         rb_raise(rb_eTypeError, "class or module required");
00526     }
00527 
00528     while (cl) {
00529         if (cl == c || RCLASS(cl)->m_tbl == RCLASS(c)->m_tbl)
00530             return Qtrue;
00531         cl = RCLASS(cl)->super;
00532     }
00533     return Qfalse;
00534 }
00535 
00536 
00537 /*
00538  * Document-method: singleton_method_added
00539  *
00540  *  call-seq:
00541  *     singleton_method_added(symbol)
00542  *  
00543  *  Invoked as a callback whenever a singleton method is added to the
00544  *  receiver.
00545  *     
00546  *     module Chatty
00547  *       def Chatty.singleton_method_added(id)
00548  *         puts "Adding #{id.id2name}"
00549  *       end
00550  *       def self.one()     end
00551  *       def two()          end
00552  *       def Chatty.three() end
00553  *     end
00554  *     
00555  *  <em>produces:</em>
00556  *     
00557  *     Adding singleton_method_added
00558  *     Adding one
00559  *     Adding three
00560  *     
00561  */
00562 
00563 /*
00564  * Document-method: singleton_method_removed
00565  *
00566  *  call-seq:
00567  *     singleton_method_removed(symbol)
00568  *  
00569  *  Invoked as a callback whenever a singleton method is removed from
00570  *  the receiver.
00571  *     
00572  *     module Chatty
00573  *       def Chatty.singleton_method_removed(id)
00574  *         puts "Removing #{id.id2name}"
00575  *       end
00576  *       def self.one()     end
00577  *       def two()          end
00578  *       def Chatty.three() end
00579  *       class <<self
00580  *         remove_method :three
00581  *         remove_method :one
00582  *       end
00583  *     end
00584  *     
00585  *  <em>produces:</em>
00586  *     
00587  *     Removing three
00588  *     Removing one
00589  */
00590 
00591 /*
00592  * Document-method: singleton_method_undefined
00593  *
00594  *  call-seq:
00595  *     singleton_method_undefined(symbol)
00596  *  
00597  *  Invoked as a callback whenever a singleton method is undefined in
00598  *  the receiver.
00599  *     
00600  *     module Chatty
00601  *       def Chatty.singleton_method_undefined(id)
00602  *         puts "Undefining #{id.id2name}"
00603  *       end
00604  *       def Chatty.one()   end
00605  *       class << self
00606  *          undef_method(:one)
00607  *       end
00608  *     end
00609  *     
00610  *  <em>produces:</em>
00611  *     
00612  *     Undefining one
00613  */
00614 
00615 
00616 /*
00617  * Document-method: included
00618  *
00619  * call-seq:
00620  *    included( othermod )
00621  *
00622  * Callback invoked whenever the receiver is included in another
00623  * module or class. This should be used in preference to
00624  * <tt>Module.append_features</tt> if your code wants to perform some
00625  * action when a module is included in another.
00626  *
00627  *        module A
00628  *          def A.included(mod)
00629  *            puts "#{self} included in #{mod}"
00630  *          end
00631  *        end
00632  *        module Enumerable
00633  *          include A
00634  *        end
00635  */
00636 
00637 
00638 /*
00639  * Not documented
00640  */
00641 
00642 static VALUE
00643 rb_obj_dummy()
00644 {
00645     return Qnil;
00646 }
00647 
00648 
00649 /*
00650  *  call-seq:
00651  *     obj.tainted?    => true or false
00652  *  
00653  *  Returns <code>true</code> if the object is tainted.
00654  */
00655 
00656 VALUE
00657 rb_obj_tainted(obj)
00658     VALUE obj;
00659 {
00660     if (OBJ_TAINTED(obj))
00661         return Qtrue;
00662     return Qfalse;
00663 }
00664 
00665 /*
00666  *  call-seq:
00667  *     obj.taint -> obj
00668  *  
00669  *  Marks <i>obj</i> as tainted---if the <code>$SAFE</code> level is
00670  *  set appropriately, many method calls which might alter the running
00671  *  programs environment will refuse to accept tainted strings.
00672  */
00673 
00674 VALUE
00675 rb_obj_taint(obj)
00676     VALUE obj;
00677 {
00678     rb_secure(4);
00679     if (!OBJ_TAINTED(obj)) {
00680         if (OBJ_FROZEN(obj)) {
00681             rb_error_frozen("object");
00682         }
00683         OBJ_TAINT(obj);
00684     }
00685     return obj;
00686 }
00687 
00688 
00689 /*
00690  *  call-seq:
00691  *     obj.untaint    => obj
00692  *  
00693  *  Removes the taint from <i>obj</i>.
00694  */
00695 
00696 VALUE
00697 rb_obj_untaint(obj)
00698     VALUE obj;
00699 {
00700     rb_secure(3);
00701     if (OBJ_TAINTED(obj)) {
00702         if (OBJ_FROZEN(obj)) {
00703             rb_error_frozen("object");
00704         }
00705         FL_UNSET(obj, FL_TAINT);
00706     }
00707     return obj;
00708 }
00709 
00710 void
00711 rb_obj_infect(obj1, obj2)
00712     VALUE obj1, obj2;
00713 {
00714     OBJ_INFECT(obj1, obj2);
00715 }
00716 
00717 
00718 /*
00719  *  call-seq:
00720  *     obj.freeze    => obj
00721  *  
00722  *  Prevents further modifications to <i>obj</i>. A
00723  *  <code>TypeError</code> will be raised if modification is attempted.
00724  *  There is no way to unfreeze a frozen object. See also
00725  *  <code>Object#frozen?</code>.
00726  *     
00727  *     a = [ "a", "b", "c" ]
00728  *     a.freeze
00729  *     a << "z"
00730  *     
00731  *  <em>produces:</em>
00732  *     
00733  *     prog.rb:3:in `<<': can't modify frozen array (TypeError)
00734  *      from prog.rb:3
00735  */
00736 
00737 VALUE
00738 rb_obj_freeze(obj)
00739     VALUE obj;
00740 {
00741     if (!OBJ_FROZEN(obj)) {
00742         if (rb_safe_level() >= 4 && !OBJ_TAINTED(obj)) {
00743             rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
00744         }
00745         OBJ_FREEZE(obj);
00746     }
00747     return obj;
00748 }
00749 
00750 /*
00751  *  call-seq:
00752  *     obj.frozen?    => true or false
00753  *  
00754  *  Returns the freeze status of <i>obj</i>.
00755  *     
00756  *     a = [ "a", "b", "c" ]
00757  *     a.freeze    #=> ["a", "b", "c"]
00758  *     a.frozen?   #=> true
00759  */
00760 
00761 static VALUE
00762 rb_obj_frozen_p(obj)
00763     VALUE obj;
00764 {
00765     if (OBJ_FROZEN(obj)) return Qtrue;
00766     return Qfalse;
00767 }
00768 
00769 
00770 /*
00771  * Document-class: NilClass
00772  *
00773  *  The class of the singleton object <code>nil</code>.
00774  */
00775 
00776 /*
00777  *  call-seq:
00778  *     nil.to_i => 0
00779  *  
00780  *  Always returns zero.
00781  *     
00782  *     nil.to_i   #=> 0
00783  */
00784 
00785 
00786 static VALUE
00787 nil_to_i(obj)
00788     VALUE obj;
00789 {
00790     return INT2FIX(0);
00791 }
00792 
00793 /*
00794  *  call-seq:
00795  *     nil.to_f    => 0.0
00796  *  
00797  *  Always returns zero.
00798  *     
00799  *     nil.to_f   #=> 0.0
00800  */
00801 
00802 static VALUE
00803 nil_to_f(obj)
00804     VALUE obj;
00805 {
00806     return rb_float_new(0.0);
00807 }
00808 
00809 /*
00810  *  call-seq:
00811  *     nil.to_s    => ""
00812  *  
00813  *  Always returns the empty string.
00814  *     
00815  *     nil.to_s   #=> ""
00816  */
00817 
00818 static VALUE
00819 nil_to_s(obj)
00820     VALUE obj;
00821 {
00822     return rb_str_new2("");
00823 }
00824 
00825 /*
00826  *  call-seq:
00827  *     nil.to_a    => []
00828  *  
00829  *  Always returns an empty array.
00830  *     
00831  *     nil.to_a   #=> []
00832  */
00833 
00834 static VALUE
00835 nil_to_a(obj)
00836     VALUE obj;
00837 {
00838     return rb_ary_new2(0);
00839 }
00840 
00841 /*
00842  *  call-seq:
00843  *    nil.inspect  => "nil"
00844  *
00845  *  Always returns the string "nil".
00846  */
00847 
00848 static VALUE
00849 nil_inspect(obj)
00850     VALUE obj;
00851 {
00852     return rb_str_new2("nil");
00853 }
00854 
00855 #ifdef NIL_PLUS
00856 static VALUE
00857 nil_plus(x, y)
00858     VALUE x, y;
00859 {
00860     switch (TYPE(y)) {
00861       case T_NIL:
00862       case T_FIXNUM:
00863       case T_FLOAT:
00864       case T_BIGNUM:
00865       case T_STRING:
00866       case T_ARRAY:
00867         return y;
00868       default:
00869         rb_raise(rb_eTypeError, "tried to add %s(%s) to nil",
00870                  RSTRING(rb_inspect(y))->ptr,
00871                  rb_obj_classname(y));
00872     }
00873     /* not reached */
00874 }
00875 #endif
00876 
00877 static VALUE
00878 main_to_s(obj)
00879     VALUE obj;
00880 {
00881     return rb_str_new2("main");
00882 }
00883 
00884 
00885 /***********************************************************************
00886  *  Document-class: TrueClass
00887  *
00888  *  The global value <code>true</code> is the only instance of class
00889  *  <code>TrueClass</code> and represents a logically true value in
00890  *  boolean expressions. The class provides operators allowing
00891  *  <code>true</code> to be used in logical expressions.
00892  */
00893 
00894 
00895 /*
00896  * call-seq:
00897  *   true.to_s   =>  "true"
00898  *
00899  * The string representation of <code>true</code> is "true".
00900  */
00901 
00902 static VALUE
00903 true_to_s(obj)
00904     VALUE obj;
00905 {
00906     return rb_str_new2("true");
00907 }
00908 
00909 
00910 /*
00911  *  call-seq:
00912  *     true & obj    => true or false
00913  *  
00914  *  And---Returns <code>false</code> if <i>obj</i> is
00915  *  <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
00916  */
00917 
00918 static VALUE
00919 true_and(obj, obj2)
00920     VALUE obj, obj2;
00921 {
00922     return RTEST(obj2)?Qtrue:Qfalse;
00923 }
00924 
00925 /*
00926  *  call-seq:
00927  *     true | obj   => true
00928  *  
00929  *  Or---Returns <code>true</code>. As <i>anObject</i> is an argument to
00930  *  a method call, it is always evaluated; there is no short-circuit
00931  *  evaluation in this case.
00932  *     
00933  *     true |  puts("or")
00934  *     true || puts("logical or")
00935  *     
00936  *  <em>produces:</em>
00937  *     
00938  *     or
00939  */
00940 
00941 static VALUE
00942 true_or(obj, obj2)
00943     VALUE obj, obj2;
00944 {
00945     return Qtrue;
00946 }
00947 
00948 
00949 /*
00950  *  call-seq:
00951  *     true ^ obj   => !obj
00952  *  
00953  *  Exclusive Or---Returns <code>true</code> if <i>obj</i> is
00954  *  <code>nil</code> or <code>false</code>, <code>false</code>
00955  *  otherwise.
00956  */
00957 
00958 static VALUE
00959 true_xor(obj, obj2)
00960     VALUE obj, obj2;
00961 {
00962     return RTEST(obj2)?Qfalse:Qtrue;
00963 }
00964 
00965 
00966 /*
00967  *  Document-class: FalseClass
00968  *
00969  *  The global value <code>false</code> is the only instance of class
00970  *  <code>FalseClass</code> and represents a logically false value in
00971  *  boolean expressions. The class provides operators allowing
00972  *  <code>false</code> to participate correctly in logical expressions.
00973  *     
00974  */
00975 
00976 /*
00977  * call-seq:
00978  *   false.to_s   =>  "false"
00979  *
00980  * 'nuf said...
00981  */
00982 
00983 static VALUE
00984 false_to_s(obj)
00985     VALUE obj;
00986 {
00987     return rb_str_new2("false");
00988 }
00989 
00990 /*
00991  *  call-seq:
00992  *     false & obj   => false
00993  *     nil & obj     => false
00994  *  
00995  *  And---Returns <code>false</code>. <i>obj</i> is always
00996  *  evaluated as it is the argument to a method call---there is no
00997  *  short-circuit evaluation in this case.
00998  */
00999 
01000 static VALUE
01001 false_and(obj, obj2)
01002     VALUE obj, obj2;
01003 {
01004     return Qfalse;
01005 }
01006 
01007 
01008 /*
01009  *  call-seq:
01010  *     false | obj   =>   true or false
01011  *     nil   | obj   =>   true or false
01012  *  
01013  *  Or---Returns <code>false</code> if <i>obj</i> is
01014  *  <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
01015  */
01016 
01017 static VALUE
01018 false_or(obj, obj2)
01019     VALUE obj, obj2;
01020 {
01021     return RTEST(obj2)?Qtrue:Qfalse;
01022 }
01023 
01024 
01025 
01026 /*
01027  *  call-seq:
01028  *     false ^ obj    => true or false
01029  *     nil   ^ obj    => true or false
01030  *  
01031  *  Exclusive Or---If <i>obj</i> is <code>nil</code> or
01032  *  <code>false</code>, returns <code>false</code>; otherwise, returns
01033  *  <code>true</code>.
01034  *     
01035  */
01036 
01037 static VALUE
01038 false_xor(obj, obj2)
01039     VALUE obj, obj2;
01040 {
01041     return RTEST(obj2)?Qtrue:Qfalse;
01042 }
01043 
01044 /*
01045  * call_seq:
01046  *   nil.nil?               => true
01047  *
01048  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
01049  */
01050 
01051 static VALUE
01052 rb_true(obj)
01053     VALUE obj;
01054 {
01055     return Qtrue;
01056 }
01057 
01058 /*
01059  * call_seq:
01060  *   nil.nil?               => true
01061  *   <anything_else>.nil?   => false
01062  *
01063  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
01064  */
01065 
01066 
01067 static VALUE
01068 rb_false(obj)
01069     VALUE obj;
01070 {
01071     return Qfalse;
01072 }
01073 
01074 
01075 /*
01076  *  call-seq:
01077  *     obj =~ other  => false
01078  *  
01079  *  Pattern Match---Overridden by descendents (notably
01080  *  <code>Regexp</code> and <code>String</code>) to provide meaningful
01081  *  pattern-match semantics.
01082  */
01083 
01084 static VALUE
01085 rb_obj_pattern_match(obj1, obj2)
01086     VALUE obj1, obj2;
01087 {
01088     return Qfalse;
01089 }
01090 
01091 /**********************************************************************
01092  * Document-class: Symbol
01093  *
01094  *  <code>Symbol</code> objects represent names and some strings
01095  *  inside the Ruby
01096  *  interpreter. They are generated using the <code>:name</code> and
01097  *  <code>:"string"</code> literals
01098  *  syntax, and by the various <code>to_sym</code> methods. The same
01099  *  <code>Symbol</code> object will be created for a given name or string
01100  *  for the duration of a program's execution, regardless of the context
01101  *  or meaning of that name. Thus if <code>Fred</code> is a constant in
01102  *  one context, a method in another, and a class in a third, the
01103  *  <code>Symbol</code> <code>:Fred</code> will be the same object in
01104  *  all three contexts.
01105  *     
01106  *     module One
01107  *       class Fred
01108  *       end
01109  *       $f1 = :Fred
01110  *     end
01111  *     module Two
01112  *       Fred = 1
01113  *       $f2 = :Fred
01114  *     end
01115  *     def Fred()
01116  *     end
01117  *     $f3 = :Fred
01118  *     $f1.id   #=> 2514190
01119  *     $f2.id   #=> 2514190
01120  *     $f3.id   #=> 2514190
01121  *     
01122  */
01123 
01124 /*
01125  *  call-seq:
01126  *     sym.to_i      => fixnum
01127  *  
01128  *  Returns an integer that is unique for each symbol within a
01129  *  particular execution of a program.
01130  *     
01131  *     :fred.to_i           #=> 9809
01132  *     "fred".to_sym.to_i   #=> 9809
01133  */
01134 
01135 static VALUE
01136 sym_to_i(sym)
01137     VALUE sym;
01138 {
01139     ID id = SYM2ID(sym);
01140 
01141     return LONG2FIX(id);
01142 }
01143 
01144 
01145 /* :nodoc: */
01146 
01147 static VALUE
01148 sym_to_int(sym)
01149     VALUE sym;
01150 {
01151     rb_warning("treating Symbol as an integer");
01152     return sym_to_i(sym);
01153 }
01154 
01155 
01156 /*
01157  *  call-seq:
01158  *     sym.inspect    => string
01159  *  
01160  *  Returns the representation of <i>sym</i> as a symbol literal.
01161  *     
01162  *     :fred.inspect   #=> ":fred"
01163  */
01164 
01165 static VALUE
01166 sym_inspect(sym)
01167     VALUE sym;
01168 {
01169     VALUE str;
01170     char *name;
01171     ID id = SYM2ID(sym);
01172 
01173     name = rb_id2name(id);
01174     str = rb_str_new(0, strlen(name)+1);
01175     RSTRING(str)->ptr[0] = ':';
01176     strcpy(RSTRING(str)->ptr+1, name);
01177     if (!rb_symname_p(name)) {
01178         str = rb_str_dump(str);
01179         strncpy(RSTRING(str)->ptr, ":\"", 2);
01180     }
01181     return str;
01182 }
01183 
01184 
01185 /*
01186  *  call-seq:
01187  *     sym.id2name   => string
01188  *     sym.to_s      => string
01189  *  
01190  *  Returns the name or string corresponding to <i>sym</i>.
01191  *     
01192  *     :fred.id2name   #=> "fred"
01193  */
01194 
01195 
01196 static VALUE
01197 sym_to_s(sym)
01198     VALUE sym;
01199 {
01200     return rb_str_new2(rb_id2name(SYM2ID(sym)));
01201 }
01202 
01203 
01204 /*
01205  * call-seq:
01206  *   sym.to_sym   => sym
01207  *
01208  * In general, <code>to_sym</code> returns the <code>Symbol</code> corresponding
01209  * to an object. As <i>sym</i> is already a symbol, <code>self</code> is returned
01210  * in this case.
01211  */
01212 
01213 static VALUE
01214 sym_to_sym(sym)
01215     VALUE sym;
01216 {
01217     return sym;
01218 }
01219 
01220 
01221 /***********************************************************************
01222  *
01223  * Document-class: Module
01224  *
01225  *  A <code>Module</code> is a collection of methods and constants. The
01226  *  methods in a module may be instance methods or module methods.
01227  *  Instance methods appear as methods in a class when the module is
01228  *  included, module methods do not. Conversely, module methods may be
01229  *  called without creating an encapsulating object, while instance
01230  *  methods may not. (See <code>Module#module_function</code>)
01231  *     
01232  *  In the descriptions that follow, the parameter <i>syml</i> refers
01233  *  to a symbol, which is either a quoted string or a
01234  *  <code>Symbol</code> (such as <code>:name</code>).
01235  *     
01236  *     module Mod
01237  *       include Math
01238  *       CONST = 1
01239  *       def meth
01240  *         #  ...
01241  *       end
01242  *     end
01243  *     Mod.class              #=> Module
01244  *     Mod.constants          #=> ["E", "PI", "CONST"]
01245  *     Mod.instance_methods   #=> ["meth"]
01246  *     
01247  */
01248 
01249 /*
01250  * call-seq:
01251  *   mod.to_s   => string
01252  *
01253  * Return a string representing this module or class. For basic
01254  * classes and modules, this is the name. For singletons, we
01255  * show information on the thing we're attached to as well.
01256  */
01257 
01258 static VALUE
01259 rb_mod_to_s(klass)
01260     VALUE klass;
01261 
01262 {
01263     if (FL_TEST(klass, FL_SINGLETON)) {
01264         VALUE s = rb_str_new2("#<");
01265         VALUE v = rb_iv_get(klass, "__attached__");
01266 
01267         rb_str_cat2(s, "Class:");
01268         switch (TYPE(v)) {
01269           case T_CLASS: case T_MODULE:
01270             rb_str_append(s, rb_inspect(v));
01271             break;
01272           default:
01273             rb_str_append(s, rb_any_to_s(v));
01274             break;
01275         }
01276         rb_str_cat2(s, ">");
01277 
01278         return s;
01279     }
01280     return rb_str_dup(rb_class_name(klass));
01281 }
01282 
01283 /*
01284  *  call-seq:
01285  *     mod.freeze
01286  *  
01287  *  Prevents further modifications to <i>mod</i>.
01288  */
01289 
01290 static VALUE
01291 rb_mod_freeze(mod)
01292     VALUE mod;
01293 {
01294     rb_mod_to_s(mod);
01295     return rb_obj_freeze(mod);
01296 }
01297 
01298 /*
01299  *  call-seq:
01300  *     mod === obj    => true or false
01301  *  
01302  *  Case Equality---Returns <code>true</code> if <i>anObject</i> is an
01303  *  instance of <i>mod</i> or one of <i>mod</i>'s descendents. Of
01304  *  limited use for modules, but can be used in <code>case</code>
01305  *  statements to classify objects by class.
01306  */
01307 
01308 static VALUE
01309 rb_mod_eqq(mod, arg)
01310     VALUE mod, arg;
01311 {
01312     return rb_obj_is_kind_of(arg, mod);
01313 }
01314 
01315 /*
01316  * call-seq:
01317  *   mod <= other   =>  true, false, or nil
01318  *
01319  * Returns true if <i>mod</i> is a subclass of <i>other</i> or
01320  * is the same as <i>other</i>. Returns 
01321  * <code>nil</code> if there's no relationship between the two. 
01322  * (Think of the relationship in terms of the class definition: 
01323  * "class A<B" implies "A<B").
01324  *
01325  */
01326 
01327 VALUE
01328 rb_class_inherited_p(mod, arg)
01329     VALUE mod, arg;
01330 {
01331     VALUE start = mod;
01332 
01333     if (mod == arg) return Qtrue;
01334     switch (TYPE(arg)) {
01335       case T_MODULE:
01336       case T_CLASS:
01337         break;
01338       default:
01339         rb_raise(rb_eTypeError, "compared with non class/module");
01340     }
01341 
01342     if (FL_TEST(mod, FL_SINGLETON)) {
01343         if (RCLASS(mod)->m_tbl == RCLASS(arg)->m_tbl)
01344             return Qtrue;
01345         mod = RBASIC(mod)->klass;
01346     }
01347     while (mod) {
01348         if (RCLASS(mod)->m_tbl == RCLASS(arg)->m_tbl)
01349             return Qtrue;
01350         mod = RCLASS(mod)->super;
01351     }
01352     /* not mod < arg; check if mod > arg */
01353     while (arg) {
01354         if (RCLASS(arg)->m_tbl == RCLASS(start)->m_tbl)
01355             return Qfalse;
01356         arg = RCLASS(arg)->super;
01357     }
01358     return Qnil;
01359 }
01360 
01361 /*
01362  * call-seq:
01363  *   mod < other   =>  true, false, or nil
01364  *
01365  * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns 
01366  * <code>nil</code> if there's no relationship between the two. 
01367  * (Think of the relationship in terms of the class definition: 
01368  * "class A<B" implies "A<B").
01369  *
01370  */
01371 
01372 static VALUE
01373 rb_mod_lt(mod, arg)
01374     VALUE mod, arg;
01375 {
01376     if (mod == arg) return Qfalse;
01377     return rb_class_inherited_p(mod, arg);
01378 }
01379 
01380 
01381 /*
01382  * call-seq:
01383  *   mod >= other   =>  true, false, or nil
01384  *
01385  * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
01386  * two modules are the same. Returns 
01387  * <code>nil</code> if there's no relationship between the two. 
01388  * (Think of the relationship in terms of the class definition: 
01389  * "class A<B" implies "B>A").
01390  *
01391  */
01392 
01393 static VALUE
01394 rb_mod_ge(mod, arg)
01395     VALUE mod, arg;
01396 {
01397     switch (TYPE(arg)) {
01398       case T_MODULE:
01399       case T_CLASS:
01400         break;
01401       default:
01402         rb_raise(rb_eTypeError, "compared with non class/module");
01403     }
01404 
01405     return rb_class_inherited_p(arg, mod);
01406 }
01407 
01408 /*
01409  * call-seq:
01410  *   mod > other   =>  true, false, or nil
01411  *
01412  * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns 
01413  * <code>nil</code> if there's no relationship between the two. 
01414  * (Think of the relationship in terms of the class definition: 
01415  * "class A<B" implies "B>A").
01416  *
01417  */
01418 
01419 static VALUE
01420 rb_mod_gt(mod, arg)
01421     VALUE mod, arg;
01422 {
01423     if (mod == arg) return Qfalse;
01424     return rb_mod_ge(mod, arg);
01425 }
01426 
01427 /*
01428  *  call-seq:
01429  *     mod <=> other_mod   => -1, 0, +1, or nil
01430  *  
01431  *  Comparison---Returns -1 if <i>mod</i> includes <i>other_mod</i>, 0 if
01432  *  <i>mod</i> is the same as <i>other_mod</i>, and +1 if <i>mod</i> is
01433  *  included by <i>other_mod</i> or if <i>mod</i> has no relationship with
01434  *  <i>other_mod</i>. Returns <code>nil</code> if <i>other_mod</i> is
01435  *  not a module.
01436  */
01437 
01438 static VALUE
01439 rb_mod_cmp(mod, arg)
01440     VALUE mod, arg;
01441 {
01442     VALUE cmp;
01443 
01444     if (mod == arg) return INT2FIX(0);
01445     switch (TYPE(arg)) {
01446       case T_MODULE:
01447       case T_CLASS:
01448         break;
01449       default:
01450         return Qnil;
01451     }
01452 
01453     cmp = rb_class_inherited_p(mod, arg);
01454     if (NIL_P(cmp)) return Qnil;
01455     if (cmp) {
01456         return INT2FIX(-1);
01457     }
01458     return INT2FIX(1);
01459 }
01460 
01461 static VALUE rb_module_s_alloc (VALUE);
01462 static VALUE
01463 rb_module_s_alloc(klass)
01464     VALUE klass;
01465 {
01466     VALUE mod = rb_module_new();
01467 
01468     RBASIC(mod)->klass = klass;
01469     return mod;
01470 }
01471 
01472 static VALUE rb_class_s_alloc (VALUE);
01473 static VALUE
01474 rb_class_s_alloc(klass)
01475     VALUE klass;
01476 {
01477     return rb_class_boot(0);
01478 }
01479 
01480 /*
01481  *  call-seq:
01482  *    Module.new                  => mod
01483  *    Module.new {|mod| block }   => mod
01484  *  
01485  *  Creates a new anonymous module. If a block is given, it is passed
01486  *  the module object, and the block is evaluated in the context of this
01487  *  module using <code>module_eval</code>.
01488  *     
01489  *     Fred = Module.new do
01490  *       def meth1
01491  *         "hello"
01492  *       end
01493  *       def meth2
01494  *         "bye"
01495  *       end
01496  *     end
01497  *     a = "my string"
01498  *     a.extend(Fred)   #=> "my string"
01499  *     a.meth1          #=> "hello"
01500  *     a.meth2          #=> "bye"
01501  */
01502 
01503 static VALUE
01504 rb_mod_initialize(module)
01505     VALUE module;
01506 {
01507     if (rb_block_given_p()) {
01508         rb_mod_module_eval(0, 0, module);
01509     }
01510     return Qnil;
01511 }
01512 
01513 /*
01514  *  call-seq:
01515  *     Class.new(super_class=Object)   =>    a_class
01516  *  
01517  *  Creates a new anonymous (unnamed) class with the given superclass
01518  *  (or <code>Object</code> if no parameter is given). You can give a
01519  *  class a name by assigning the class object to a constant.
01520  *     
01521  */
01522 
01523 static VALUE
01524 rb_class_initialize(argc, argv, klass)
01525     int argc;
01526     VALUE *argv;
01527     VALUE klass;
01528 {
01529     VALUE super;
01530 
01531     if (RCLASS(klass)->super != 0) {
01532         rb_raise(rb_eTypeError, "already initialized class");
01533     }
01534     if (rb_scan_args(argc, argv, "01", &super) == 0) {
01535         super = rb_cObject;
01536     }
01537     else {
01538         rb_check_inheritable(super);
01539     }
01540     RCLASS(klass)->super = super;
01541     rb_make_metaclass(klass, RBASIC(super)->klass);
01542     rb_mod_initialize(klass);
01543     rb_class_inherited(super, klass);
01544 
01545     return klass;
01546 }
01547 
01548 /*
01549  *  call-seq:
01550  *     class.allocate()   =>   obj
01551  *  
01552  *  Allocates space for a new object of <i>class</i>'s class. The
01553  *  returned object must be an instance of <i>class</i>.
01554  *     
01555  */
01556 
01557 VALUE
01558 rb_obj_alloc(klass)
01559     VALUE klass;
01560 {
01561     VALUE obj;
01562 
01563     if (RCLASS(klass)->super == 0) {
01564         rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
01565     }
01566     if (FL_TEST(klass, FL_SINGLETON)) {
01567         rb_raise(rb_eTypeError, "can't create instance of virtual class");
01568     }
01569     obj = rb_funcall(klass, ID_ALLOCATOR, 0, 0);
01570     if (rb_obj_class(obj) != rb_class_real(klass)) {
01571         rb_raise(rb_eTypeError, "wrong instance allocation");
01572     }
01573     return obj;
01574 }
01575 
01576 static VALUE rb_class_allocate_instance (VALUE);
01577 static VALUE
01578 rb_class_allocate_instance(klass)
01579     VALUE klass;
01580 {
01581     NEWOBJ(obj, struct RObject);
01582     OBJSETUP(obj, klass, T_OBJECT);
01583     return (VALUE)obj;
01584 }
01585 
01586 /*
01587  *  call-seq:
01588  *     class.new(args, ...)    =>  obj
01589  *  
01590  *  Calls <code>allocate</code> to create a new object of
01591  *  <i>class</i>'s class, then invokes that object's
01592  *  <code>initialize</code> method, passing it <i>args</i>.
01593  *  This is the method that ends up getting called whenever
01594  *  an object is constructed using .new.
01595  *     
01596  */
01597 
01598 VALUE
01599 rb_class_new_instance(argc, argv, klass)
01600     int argc;
01601     VALUE *argv;
01602     VALUE klass;
01603 {
01604     VALUE obj;
01605 
01606     obj = rb_obj_alloc(klass);
01607     rb_obj_call_init(obj, argc, argv);
01608 
01609     return obj;
01610 }
01611 
01612 /*
01613  *  call-seq:
01614  *     class.superclass -> a_super_class or nil
01615  *  
01616  *  Returns the superclass of <i>class</i>, or <code>nil</code>.
01617  *     
01618  *     File.superclass     #=> IO
01619  *     IO.superclass       #=> Object
01620  *     Object.superclass   #=> nil
01621  *     
01622  */
01623 
01624 static VALUE
01625 rb_class_superclass(klass)
01626     VALUE klass;
01627 {
01628     VALUE super = RCLASS(klass)->super;
01629 
01630     if (!super) {
01631         rb_raise(rb_eTypeError, "uninitialized class");
01632     }
01633     if (FL_TEST(klass, FL_SINGLETON)) {
01634         super = RBASIC(klass)->klass;
01635     }
01636     while (TYPE(super) == T_ICLASS) {
01637         super = RCLASS(super)->super;
01638     }
01639     if (!super) {
01640         return Qnil;
01641     }
01642     return super;
01643 }
01644 
01645 static ID
01646 str_to_id(str)
01647     VALUE str;
01648 {
01649     if (!RSTRING(str)->ptr || RSTRING(str)->len == 0) {
01650         rb_raise(rb_eArgError, "empty symbol string");
01651     }
01652     if (RSTRING(str)->len != strlen(RSTRING(str)->ptr)) {
01653         rb_warn("Symbols should not contain NUL (\\0)");
01654     }
01655     return rb_intern(RSTRING(str)->ptr);
01656 }
01657 
01658 ID
01659 rb_to_id(name)
01660     VALUE name;
01661 {
01662     VALUE tmp;
01663     ID id;
01664 
01665     switch (TYPE(name)) {
01666       case T_STRING:
01667         return str_to_id(name);
01668       case T_FIXNUM:
01669         rb_warn("do not use Fixnums as Symbols");
01670         id = FIX2LONG(name);
01671         if (!rb_id2name(id)) {
01672             rb_raise(rb_eArgError, "%ld is not a symbol", id);
01673         }
01674         break;
01675       case T_SYMBOL:
01676         id = SYM2ID(name);
01677         break;
01678       default:
01679         tmp = rb_check_string_type(name);
01680         if (!NIL_P(tmp)) {
01681             return str_to_id(tmp);
01682         }
01683         rb_raise(rb_eTypeError, "%s is not a symbol", RSTRING(rb_inspect(name))->ptr);
01684     }
01685     return id;
01686 }
01687 
01688 /*
01689  *  call-seq:
01690  *     attr(symbol, writable=false)    => nil
01691  *  
01692  *  Defines a named attribute for this module, where the name is
01693  *  <i>symbol.</i><code>id2name</code>, creating an instance variable
01694  *  (<code>@name</code>) and a corresponding access method to read it.
01695  *  If the optional <i>writable</i> argument is <code>true</code>, also
01696  *  creates a method called <code>name=</code> to set the attribute.
01697  *     
01698  *     module Mod
01699  *       attr  :size, true
01700  *     end
01701  *     
01702  *  <em>is equivalent to:</em>
01703  *     
01704  *     module Mod
01705  *       def size
01706  *         @size
01707  *       end
01708  *       def size=(val)
01709  *         @size = val
01710  *       end
01711  *     end
01712  */
01713 
01714 static VALUE
01715 rb_mod_attr(argc, argv, klass)
01716     int argc;
01717     VALUE *argv;
01718     VALUE klass;
01719 {
01720     VALUE name, pub;
01721 
01722     rb_scan_args(argc, argv, "11", &name, &pub);
01723     rb_attr(klass, rb_to_id(name), 1, RTEST(pub), Qtrue);
01724     return Qnil;
01725 }
01726 
01727 /*
01728  *  call-seq:
01729  *     attr_reader(symbol, ...)    => nil
01730  *  
01731  *  Creates instance variables and corresponding methods that return the
01732  *  value of each instance variable. Equivalent to calling
01733  *  ``<code>attr</code><i>:name</i>'' on each name in turn.
01734  */
01735 
01736 static VALUE
01737 rb_mod_attr_reader(argc, argv, klass)
01738     int argc;
01739     VALUE *argv;
01740     VALUE klass;
01741 {
01742     int i;
01743 
01744     for (i=0; i<argc; i++) {
01745         rb_attr(klass, rb_to_id(argv[i]), 1, 0, Qtrue);
01746     }
01747     return Qnil;
01748 }
01749 
01750 /*
01751  *  call-seq:
01752  *      attr_writer(symbol, ...)    => nil
01753  *  
01754  *  Creates an accessor method to allow assignment to the attribute
01755  *  <i>aSymbol</i><code>.id2name</code>.
01756  */
01757 
01758 static VALUE
01759 rb_mod_attr_writer(argc, argv, klass)
01760     int argc;
01761     VALUE *argv;
01762     VALUE klass;
01763 {
01764     int i;
01765 
01766     for (i=0; i<argc; i++) {
01767         rb_attr(klass, rb_to_id(argv[i]), 0, 1, Qtrue);
01768     }
01769     return Qnil;
01770 }
01771 
01772 /*
01773  *  call-seq:
01774  *     attr_accessor(symbol, ...)    => nil
01775  *  
01776  *  Equivalent to calling ``<code>attr</code><i>symbol</i><code>,
01777  *  true</code>'' on each <i>symbol</i> in turn.
01778  *     
01779  *     module Mod
01780  *       attr_accessor(:one, :two)
01781  *     end
01782  *     Mod.instance_methods.sort   #=> ["one", "one=", "two", "two="]
01783  */
01784 
01785 static VALUE
01786 rb_mod_attr_accessor(argc, argv, klass)
01787     int argc;
01788     VALUE *argv;
01789     VALUE klass;
01790 {
01791     int i;
01792 
01793     for (i=0; i<argc; i++) {
01794         rb_attr(klass, rb_to_id(argv[i]), 1, 1, Qtrue);
01795     }
01796     return Qnil;
01797 }
01798 
01799 /*
01800  *  call-seq:
01801  *     mod.const_get(sym)    => obj
01802  *  
01803  *  Returns the value of the named constant in <i>mod</i>.
01804  *     
01805  *     Math.const_get(:PI)   #=> 3.14159265358979
01806  */
01807 
01808 static VALUE
01809 rb_mod_const_get(mod, name)
01810     VALUE mod, name;
01811 {
01812     ID id = rb_to_id(name);
01813 
01814     if (!rb_is_const_id(id)) {
01815         rb_name_error(id, "wrong constant name %s", rb_id2name(id));
01816     }
01817     return rb_const_get(mod, id);
01818 }
01819 
01820 /*
01821  *  call-seq:
01822  *     mod.const_set(sym, obj)    => obj
01823  *  
01824  *  Sets the named constant to the given object, returning that object.
01825  *  Creates a new constant if no constant with the given name previously
01826  *  existed.
01827  *     
01828  *     Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0)   #=> 3.14285714285714
01829  *     Math::HIGH_SCHOOL_PI - Math::PI              #=> 0.00126448926734968
01830  */
01831 
01832 static VALUE
01833 rb_mod_const_set(mod, name, value)
01834     VALUE mod, name, value;
01835 {
01836     ID id = rb_to_id(name);
01837 
01838     if (!rb_is_const_id(id)) {
01