• Main Page
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

object.c

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

Generated on Wed Sep 8 2010 21:55:05 for Ruby by  doxygen 1.7.1