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