object.c

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

Generated on 19 Jul 2016 for Ruby by  doxygen 1.4.7