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