|
Ruby
1.9.3p537(2014-02-19revision0)
|
00001 /********************************************************************** 00002 00003 object.c - 00004 00005 $Author$ 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) | (RBASIC(clone)->flags&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+ and +other+ are the same object 01238 * or <code>obj == other</code>, otherwise nil. 01239 */ 01240 static VALUE 01241 rb_obj_cmp(VALUE obj1, VALUE obj2) 01242 { 01243 if (obj1 == obj2 || rb_equal(obj1, obj2)) 01244 return INT2FIX(0); 01245 return Qnil; 01246 } 01247 01248 /*********************************************************************** 01249 * 01250 * Document-class: Module 01251 * 01252 * A <code>Module</code> is a collection of methods and constants. The 01253 * methods in a module may be instance methods or module methods. 01254 * Instance methods appear as methods in a class when the module is 01255 * included, module methods do not. Conversely, module methods may be 01256 * called without creating an encapsulating object, while instance 01257 * methods may not. (See <code>Module#module_function</code>) 01258 * 01259 * In the descriptions that follow, the parameter <i>sym</i> refers 01260 * to a symbol, which is either a quoted string or a 01261 * <code>Symbol</code> (such as <code>:name</code>). 01262 * 01263 * module Mod 01264 * include Math 01265 * CONST = 1 01266 * def meth 01267 * # ... 01268 * end 01269 * end 01270 * Mod.class #=> Module 01271 * Mod.constants #=> [:CONST, :PI, :E] 01272 * Mod.instance_methods #=> [:meth] 01273 * 01274 */ 01275 01276 /* 01277 * call-seq: 01278 * mod.to_s -> string 01279 * 01280 * Return a string representing this module or class. For basic 01281 * classes and modules, this is the name. For singletons, we 01282 * show information on the thing we're attached to as well. 01283 */ 01284 01285 static VALUE 01286 rb_mod_to_s(VALUE klass) 01287 { 01288 if (FL_TEST(klass, FL_SINGLETON)) { 01289 VALUE s = rb_usascii_str_new2("#<"); 01290 VALUE v = rb_iv_get(klass, "__attached__"); 01291 01292 rb_str_cat2(s, "Class:"); 01293 switch (TYPE(v)) { 01294 case T_CLASS: case T_MODULE: 01295 rb_str_append(s, rb_inspect(v)); 01296 break; 01297 default: 01298 rb_str_append(s, rb_any_to_s(v)); 01299 break; 01300 } 01301 rb_str_cat2(s, ">"); 01302 01303 return s; 01304 } 01305 return rb_str_dup(rb_class_name(klass)); 01306 } 01307 01308 /* 01309 * call-seq: 01310 * mod.freeze -> mod 01311 * 01312 * Prevents further modifications to <i>mod</i>. 01313 * 01314 * This method returns self. 01315 */ 01316 01317 static VALUE 01318 rb_mod_freeze(VALUE mod) 01319 { 01320 rb_class_name(mod); 01321 return rb_obj_freeze(mod); 01322 } 01323 01324 /* 01325 * call-seq: 01326 * mod === obj -> true or false 01327 * 01328 * Case Equality---Returns <code>true</code> if <i>anObject</i> is an 01329 * instance of <i>mod</i> or one of <i>mod</i>'s descendants. Of 01330 * limited use for modules, but can be used in <code>case</code> 01331 * statements to classify objects by class. 01332 */ 01333 01334 static VALUE 01335 rb_mod_eqq(VALUE mod, VALUE arg) 01336 { 01337 return rb_obj_is_kind_of(arg, mod); 01338 } 01339 01340 /* 01341 * call-seq: 01342 * mod <= other -> true, false, or nil 01343 * 01344 * Returns true if <i>mod</i> is a subclass of <i>other</i> or 01345 * is the same as <i>other</i>. Returns 01346 * <code>nil</code> if there's no relationship between the two. 01347 * (Think of the relationship in terms of the class definition: 01348 * "class A<B" implies "A<B"). 01349 * 01350 */ 01351 01352 VALUE 01353 rb_class_inherited_p(VALUE mod, VALUE arg) 01354 { 01355 VALUE start = mod; 01356 01357 if (mod == arg) return Qtrue; 01358 switch (TYPE(arg)) { 01359 case T_MODULE: 01360 case T_CLASS: 01361 break; 01362 default: 01363 rb_raise(rb_eTypeError, "compared with non class/module"); 01364 } 01365 while (mod) { 01366 if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg)) 01367 return Qtrue; 01368 mod = RCLASS_SUPER(mod); 01369 } 01370 /* not mod < arg; check if mod > arg */ 01371 while (arg) { 01372 if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start)) 01373 return Qfalse; 01374 arg = RCLASS_SUPER(arg); 01375 } 01376 return Qnil; 01377 } 01378 01379 /* 01380 * call-seq: 01381 * mod < other -> true, false, or nil 01382 * 01383 * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns 01384 * <code>nil</code> if there's no relationship between the two. 01385 * (Think of the relationship in terms of the class definition: 01386 * "class A<B" implies "A<B"). 01387 * 01388 */ 01389 01390 static VALUE 01391 rb_mod_lt(VALUE mod, VALUE arg) 01392 { 01393 if (mod == arg) return Qfalse; 01394 return rb_class_inherited_p(mod, arg); 01395 } 01396 01397 01398 /* 01399 * call-seq: 01400 * mod >= other -> true, false, or nil 01401 * 01402 * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the 01403 * two modules are the same. Returns 01404 * <code>nil</code> if there's no relationship between the two. 01405 * (Think of the relationship in terms of the class definition: 01406 * "class A<B" implies "B>A"). 01407 * 01408 */ 01409 01410 static VALUE 01411 rb_mod_ge(VALUE mod, VALUE arg) 01412 { 01413 switch (TYPE(arg)) { 01414 case T_MODULE: 01415 case T_CLASS: 01416 break; 01417 default: 01418 rb_raise(rb_eTypeError, "compared with non class/module"); 01419 } 01420 01421 return rb_class_inherited_p(arg, mod); 01422 } 01423 01424 /* 01425 * call-seq: 01426 * mod > other -> true, false, or nil 01427 * 01428 * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns 01429 * <code>nil</code> if there's no relationship between the two. 01430 * (Think of the relationship in terms of the class definition: 01431 * "class A<B" implies "B>A"). 01432 * 01433 */ 01434 01435 static VALUE 01436 rb_mod_gt(VALUE mod, VALUE arg) 01437 { 01438 if (mod == arg) return Qfalse; 01439 return rb_mod_ge(mod, arg); 01440 } 01441 01442 /* 01443 * call-seq: 01444 * mod <=> other_mod -> -1, 0, +1, or nil 01445 * 01446 * Comparison---Returns -1 if <i>mod</i> includes <i>other_mod</i>, 0 if 01447 * <i>mod</i> is the same as <i>other_mod</i>, and +1 if <i>mod</i> is 01448 * included by <i>other_mod</i>. Returns <code>nil</code> if <i>mod</i> 01449 * has no relationship with <i>other_mod</i> or if <i>other_mod</i> is 01450 * not a module. 01451 */ 01452 01453 static VALUE 01454 rb_mod_cmp(VALUE mod, VALUE arg) 01455 { 01456 VALUE cmp; 01457 01458 if (mod == arg) return INT2FIX(0); 01459 switch (TYPE(arg)) { 01460 case T_MODULE: 01461 case T_CLASS: 01462 break; 01463 default: 01464 return Qnil; 01465 } 01466 01467 cmp = rb_class_inherited_p(mod, arg); 01468 if (NIL_P(cmp)) return Qnil; 01469 if (cmp) { 01470 return INT2FIX(-1); 01471 } 01472 return INT2FIX(1); 01473 } 01474 01475 static VALUE 01476 rb_module_s_alloc(VALUE klass) 01477 { 01478 VALUE mod = rb_module_new(); 01479 01480 RBASIC(mod)->klass = klass; 01481 return mod; 01482 } 01483 01484 static VALUE 01485 rb_class_s_alloc(VALUE klass) 01486 { 01487 return rb_class_boot(0); 01488 } 01489 01490 /* 01491 * call-seq: 01492 * Module.new -> mod 01493 * Module.new {|mod| block } -> mod 01494 * 01495 * Creates a new anonymous module. If a block is given, it is passed 01496 * the module object, and the block is evaluated in the context of this 01497 * module using <code>module_eval</code>. 01498 * 01499 * fred = Module.new do 01500 * def meth1 01501 * "hello" 01502 * end 01503 * def meth2 01504 * "bye" 01505 * end 01506 * end 01507 * a = "my string" 01508 * a.extend(fred) #=> "my string" 01509 * a.meth1 #=> "hello" 01510 * a.meth2 #=> "bye" 01511 * 01512 * Assign the module to a constant (name starting uppercase) if you 01513 * want to treat it like a regular module. 01514 */ 01515 01516 static VALUE 01517 rb_mod_initialize(VALUE module) 01518 { 01519 if (rb_block_given_p()) { 01520 rb_mod_module_exec(1, &module, module); 01521 } 01522 return Qnil; 01523 } 01524 01525 /* 01526 * call-seq: 01527 * Class.new(super_class=Object) -> a_class 01528 * Class.new(super_class=Object) { |mod| ... } -> a_class 01529 * 01530 * Creates a new anonymous (unnamed) class with the given superclass 01531 * (or <code>Object</code> if no parameter is given). You can give a 01532 * class a name by assigning the class object to a constant. 01533 * 01534 * If a block is given, it is passed the class object, and the block 01535 * is evaluated in the context of this class using 01536 * <code>class_eval</code>. 01537 * 01538 * fred = Class.new do 01539 * def meth1 01540 * "hello" 01541 * end 01542 * def meth2 01543 * "bye" 01544 * end 01545 * end 01546 * 01547 * a = fred.new #=> #<#<Class:0x100381890>:0x100376b98> 01548 * a.meth1 #=> "hello" 01549 * a.meth2 #=> "bye" 01550 * 01551 * Assign the class to a constant (name starting uppercase) if you 01552 * want to treat it like a regular class. 01553 */ 01554 01555 static VALUE 01556 rb_class_initialize(int argc, VALUE *argv, VALUE klass) 01557 { 01558 VALUE super; 01559 01560 if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) { 01561 rb_raise(rb_eTypeError, "already initialized class"); 01562 } 01563 if (argc == 0) { 01564 super = rb_cObject; 01565 } 01566 else { 01567 rb_scan_args(argc, argv, "01", &super); 01568 rb_check_inheritable(super); 01569 } 01570 RCLASS_SUPER(klass) = super; 01571 rb_make_metaclass(klass, RBASIC(super)->klass); 01572 rb_class_inherited(super, klass); 01573 rb_mod_initialize(klass); 01574 01575 return klass; 01576 } 01577 01578 /* 01579 * call-seq: 01580 * class.allocate() -> obj 01581 * 01582 * Allocates space for a new object of <i>class</i>'s class and does not 01583 * call initialize on the new instance. The returned object must be an 01584 * instance of <i>class</i>. 01585 * 01586 * klass = Class.new do 01587 * def initialize(*args) 01588 * @initialized = true 01589 * end 01590 * 01591 * def initialized? 01592 * @initialized || false 01593 * end 01594 * end 01595 * 01596 * klass.allocate.initialized? #=> false 01597 * 01598 */ 01599 01600 VALUE 01601 rb_obj_alloc(VALUE klass) 01602 { 01603 VALUE obj; 01604 01605 if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) { 01606 rb_raise(rb_eTypeError, "can't instantiate uninitialized class"); 01607 } 01608 if (FL_TEST(klass, FL_SINGLETON)) { 01609 rb_raise(rb_eTypeError, "can't create instance of singleton class"); 01610 } 01611 obj = rb_funcall(klass, ID_ALLOCATOR, 0, 0); 01612 if (rb_obj_class(obj) != rb_class_real(klass)) { 01613 rb_raise(rb_eTypeError, "wrong instance allocation"); 01614 } 01615 return obj; 01616 } 01617 01618 static VALUE 01619 rb_class_allocate_instance(VALUE klass) 01620 { 01621 NEWOBJ(obj, struct RObject); 01622 OBJSETUP(obj, klass, T_OBJECT); 01623 return (VALUE)obj; 01624 } 01625 01626 /* 01627 * call-seq: 01628 * class.new(args, ...) -> obj 01629 * 01630 * Calls <code>allocate</code> to create a new object of 01631 * <i>class</i>'s class, then invokes that object's 01632 * <code>initialize</code> method, passing it <i>args</i>. 01633 * This is the method that ends up getting called whenever 01634 * an object is constructed using .new. 01635 * 01636 */ 01637 01638 VALUE 01639 rb_class_new_instance(int argc, VALUE *argv, VALUE klass) 01640 { 01641 VALUE obj; 01642 01643 obj = rb_obj_alloc(klass); 01644 rb_obj_call_init(obj, argc, argv); 01645 01646 return obj; 01647 } 01648 01649 /* 01650 * call-seq: 01651 * class.superclass -> a_super_class or nil 01652 * 01653 * Returns the superclass of <i>class</i>, or <code>nil</code>. 01654 * 01655 * File.superclass #=> IO 01656 * IO.superclass #=> Object 01657 * Object.superclass #=> BasicObject 01658 * class Foo; end 01659 * class Bar < Foo; end 01660 * Bar.superclass #=> Foo 01661 * 01662 * returns nil when the given class hasn't a parent class: 01663 * 01664 * BasicObject.superclass #=> nil 01665 * 01666 */ 01667 01668 VALUE 01669 rb_class_superclass(VALUE klass) 01670 { 01671 VALUE super = RCLASS_SUPER(klass); 01672 01673 if (!super) { 01674 if (klass == rb_cBasicObject) return Qnil; 01675 rb_raise(rb_eTypeError, "uninitialized class"); 01676 } 01677 while (TYPE(super) == T_ICLASS) { 01678 super = RCLASS_SUPER(super); 01679 } 01680 if (!super) { 01681 return Qnil; 01682 } 01683 return super; 01684 } 01685 01686 VALUE 01687 rb_class_get_superclass(VALUE klass) 01688 { 01689 return RCLASS_SUPER(klass); 01690 } 01691 01692 /* 01693 * call-seq: 01694 * attr_reader(symbol, ...) -> nil 01695 * attr(symbol, ...) -> nil 01696 * 01697 * Creates instance variables and corresponding methods that return the 01698 * value of each instance variable. Equivalent to calling 01699 * ``<code>attr</code><i>:name</i>'' on each name in turn. 01700 */ 01701 01702 static VALUE 01703 rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass) 01704 { 01705 int i; 01706 01707 for (i=0; i<argc; i++) { 01708 rb_attr(klass, rb_to_id(argv[i]), TRUE, FALSE, TRUE); 01709 } 01710 return Qnil; 01711 } 01712 01713 VALUE 01714 rb_mod_attr(int argc, VALUE *argv, VALUE klass) 01715 { 01716 if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) { 01717 rb_warning("optional boolean argument is obsoleted"); 01718 rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), TRUE); 01719 return Qnil; 01720 } 01721 return rb_mod_attr_reader(argc, argv, klass); 01722 } 01723 01724 /* 01725 * call-seq: 01726 * attr_writer(symbol, ...) -> nil 01727 * 01728 * Creates an accessor method to allow assignment to the attribute 01729 * <i>aSymbol</i><code>.id2name</code>. 01730 */ 01731 01732 static VALUE 01733 rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass) 01734 { 01735 int i; 01736 01737 for (i=0; i<argc; i++) { 01738 rb_attr(klass, rb_to_id(argv[i]), FALSE, TRUE, TRUE); 01739 } 01740 return Qnil; 01741 } 01742 01743 /* 01744 * call-seq: 01745 * attr_accessor(symbol, ...) -> nil 01746 * 01747 * Defines a named attribute for this module, where the name is 01748 * <i>symbol.</i><code>id2name</code>, creating an instance variable 01749 * (<code>@name</code>) and a corresponding access method to read it. 01750 * Also creates a method called <code>name=</code> to set the attribute. 01751 * 01752 * module Mod 01753 * attr_accessor(:one, :two) 01754 * end 01755 * Mod.instance_methods.sort #=> [:one, :one=, :two, :two=] 01756 */ 01757 01758 static VALUE 01759 rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass) 01760 { 01761 int i; 01762 01763 for (i=0; i<argc; i++) { 01764 rb_attr(klass, rb_to_id(argv[i]), TRUE, TRUE, TRUE); 01765 } 01766 return Qnil; 01767 } 01768 01769 /* 01770 * call-seq: 01771 * mod.const_get(sym, inherit=true) -> obj 01772 * 01773 * Checks for a constant with the given name in <i>mod</i> 01774 * If +inherit+ is set, the lookup will also search 01775 * the ancestors (and +Object+ if <i>mod</i> is a +Module+.) 01776 * 01777 * The value of the constant is returned if a definition is found, 01778 * otherwise a +NameError+ is raised. 01779 * 01780 * Math.const_get(:PI) #=> 3.14159265358979 01781 */ 01782 01783 static VALUE 01784 rb_mod_const_get(int argc, VALUE *argv, VALUE mod) 01785 { 01786 VALUE name, recur; 01787 ID id; 01788 01789 if (argc == 1) { 01790 name = argv[0]; 01791 recur = Qtrue; 01792 } 01793 else { 01794 rb_scan_args(argc, argv, "11", &name, &recur); 01795 } 01796 id = rb_to_id(name); 01797 if (!rb_is_const_id(id)) { 01798 rb_name_error(id, "wrong constant name %s", rb_id2name(id)); 01799 } 01800 return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id); 01801 } 01802 01803 /* 01804 * call-seq: 01805 * mod.const_set(sym, obj) -> obj 01806 * 01807 * Sets the named constant to the given object, returning that object. 01808 * Creates a new constant if no constant with the given name previously 01809 * existed. 01810 * 01811 * Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714 01812 * Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968 01813 */ 01814 01815 static VALUE 01816 rb_mod_const_set(VALUE mod, VALUE name, VALUE value) 01817 { 01818 ID id = rb_to_id(name); 01819 01820 if (!rb_is_const_id(id)) { 01821 rb_name_error(id, "wrong constant name %s", rb_id2name(id)); 01822 } 01823 rb_const_set(mod, id, value); 01824 return value; 01825 } 01826 01827 /* 01828 * call-seq: 01829 * mod.const_defined?(sym, inherit=true) -> true or false 01830 * 01831 * Checks for a constant with the given name in <i>mod</i> 01832 * If +inherit+ is set, the lookup will also search 01833 * the ancestors (and +Object+ if <i>mod</i> is a +Module+.) 01834 * 01835 * Returns whether or not a definition is found: 01836 * 01837 * Math.const_defined? "PI" #=> true 01838 * IO.const_defined? :SYNC #=> true 01839 * IO.const_defined? :SYNC, false #=> false 01840 */ 01841 01842 static VALUE 01843 rb_mod_const_defined(int argc, VALUE *argv, VALUE mod) 01844 { 01845 VALUE name, recur; 01846 ID id; 01847 01848 if (argc == 1) { 01849 name = argv[0]; 01850 recur = Qtrue; 01851 } 01852 else { 01853 rb_scan_args(argc, argv, "11", &name, &recur); 01854 } 01855 id = rb_to_id(name); 01856 if (!rb_is_const_id(id)) { 01857 rb_name_error(id, "wrong constant name %s", rb_id2name(id)); 01858 } 01859 return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id); 01860 } 01861 01862 /* 01863 * call-seq: 01864 * obj.instance_variable_get(symbol) -> obj 01865 * 01866 * Returns the value of the given instance variable, or nil if the 01867 * instance variable is not set. The <code>@</code> part of the 01868 * variable name should be included for regular instance 01869 * variables. Throws a <code>NameError</code> exception if the 01870 * supplied symbol is not valid as an instance variable name. 01871 * 01872 * class Fred 01873 * def initialize(p1, p2) 01874 * @a, @b = p1, p2 01875 * end 01876 * end 01877 * fred = Fred.new('cat', 99) 01878 * fred.instance_variable_get(:@a) #=> "cat" 01879 * fred.instance_variable_get("@b") #=> 99 01880 */ 01881 01882 static VALUE 01883 rb_obj_ivar_get(VALUE obj, VALUE iv) 01884 { 01885 ID id = rb_to_id(iv); 01886 01887 if (!rb_is_instance_id(id)) { 01888 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id)); 01889 } 01890 return rb_ivar_get(obj, id); 01891 } 01892 01893 /* 01894 * call-seq: 01895 * obj.instance_variable_set(symbol, obj) -> obj 01896 * 01897 * Sets the instance variable names by <i>symbol</i> to 01898 * <i>object</i>, thereby frustrating the efforts of the class's 01899 * author to attempt to provide proper encapsulation. The variable 01900 * did not have to exist prior to this call. 01901 * 01902 * class Fred 01903 * def initialize(p1, p2) 01904 * @a, @b = p1, p2 01905 * end 01906 * end 01907 * fred = Fred.new('cat', 99) 01908 * fred.instance_variable_set(:@a, 'dog') #=> "dog" 01909 * fred.instance_variable_set(:@c, 'cat') #=> "cat" 01910 * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">" 01911 */ 01912 01913 static VALUE 01914 rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val) 01915 { 01916 ID id = rb_to_id(iv); 01917 01918 if (!rb_is_instance_id(id)) { 01919 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id)); 01920 } 01921 return rb_ivar_set(obj, id, val); 01922 } 01923 01924 /* 01925 * call-seq: 01926 * obj.instance_variable_defined?(symbol) -> true or false 01927 * 01928 * Returns <code>true</code> if the given instance variable is 01929 * defined in <i>obj</i>. 01930 * 01931 * class Fred 01932 * def initialize(p1, p2) 01933 * @a, @b = p1, p2 01934 * end 01935 * end 01936 * fred = Fred.new('cat', 99) 01937 * fred.instance_variable_defined?(:@a) #=> true 01938 * fred.instance_variable_defined?("@b") #=> true 01939 * fred.instance_variable_defined?("@c") #=> false 01940 */ 01941 01942 static VALUE 01943 rb_obj_ivar_defined(VALUE obj, VALUE iv) 01944 { 01945 ID id = rb_to_id(iv); 01946 01947 if (!rb_is_instance_id(id)) { 01948 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id)); 01949 } 01950 return rb_ivar_defined(obj, id); 01951 } 01952 01953 /* 01954 * call-seq: 01955 * mod.class_variable_get(symbol) -> obj 01956 * 01957 * Returns the value of the given class variable (or throws a 01958 * <code>NameError</code> exception). The <code>@@</code> part of the 01959 * variable name should be included for regular class variables 01960 * 01961 * class Fred 01962 * @@foo = 99 01963 * end 01964 * Fred.class_variable_get(:@@foo) #=> 99 01965 */ 01966 01967 static VALUE 01968 rb_mod_cvar_get(VALUE obj, VALUE iv) 01969 { 01970 ID id = rb_to_id(iv); 01971 01972 if (!rb_is_class_id(id)) { 01973 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id)); 01974 } 01975 return rb_cvar_get(obj, id); 01976 } 01977 01978 /* 01979 * call-seq: 01980 * obj.class_variable_set(symbol, obj) -> obj 01981 * 01982 * Sets the class variable names by <i>symbol</i> to 01983 * <i>object</i>. 01984 * 01985 * class Fred 01986 * @@foo = 99 01987 * def foo 01988 * @@foo 01989 * end 01990 * end 01991 * Fred.class_variable_set(:@@foo, 101) #=> 101 01992 * Fred.new.foo #=> 101 01993 */ 01994 01995 static VALUE 01996 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val) 01997 { 01998 ID id = rb_to_id(iv); 01999 02000 if (!rb_is_class_id(id)) { 02001 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id)); 02002 } 02003 rb_cvar_set(obj, id, val); 02004 return val; 02005 } 02006 02007 /* 02008 * call-seq: 02009 * obj.class_variable_defined?(symbol) -> true or false 02010 * 02011 * Returns <code>true</code> if the given class variable is defined 02012 * in <i>obj</i>. 02013 * 02014 * class Fred 02015 * @@foo = 99 02016 * end 02017 * Fred.class_variable_defined?(:@@foo) #=> true 02018 * Fred.class_variable_defined?(:@@bar) #=> false 02019 */ 02020 02021 static VALUE 02022 rb_mod_cvar_defined(VALUE obj, VALUE iv) 02023 { 02024 ID id = rb_to_id(iv); 02025 02026 if (!rb_is_class_id(id)) { 02027 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id)); 02028 } 02029 return rb_cvar_defined(obj, id); 02030 } 02031 02032 static struct conv_method_tbl { 02033 const char *method; 02034 ID id; 02035 } conv_method_names[] = { 02036 {"to_int", 0}, 02037 {"to_ary", 0}, 02038 {"to_str", 0}, 02039 {"to_sym", 0}, 02040 {"to_hash", 0}, 02041 {"to_proc", 0}, 02042 {"to_io", 0}, 02043 {"to_a", 0}, 02044 {"to_s", 0}, 02045 {NULL, 0} 02046 }; 02047 02048 static VALUE 02049 convert_type(VALUE val, const char *tname, const char *method, int raise) 02050 { 02051 ID m = 0; 02052 int i; 02053 VALUE r; 02054 02055 for (i=0; conv_method_names[i].method; i++) { 02056 if (conv_method_names[i].method[0] == method[0] && 02057 strcmp(conv_method_names[i].method, method) == 0) { 02058 m = conv_method_names[i].id; 02059 break; 02060 } 02061 } 02062 if (!m) m = rb_intern(method); 02063 r = rb_check_funcall(val, m, 0, 0); 02064 if (r == Qundef) { 02065 if (raise) { 02066 rb_raise(rb_eTypeError, "can't convert %s into %s", 02067 NIL_P(val) ? "nil" : 02068 val == Qtrue ? "true" : 02069 val == Qfalse ? "false" : 02070 rb_obj_classname(val), 02071 tname); 02072 } 02073 return Qnil; 02074 } 02075 return r; 02076 } 02077 02078 VALUE 02079 rb_convert_type(VALUE val, int type, const char *tname, const char *method) 02080 { 02081 VALUE v; 02082 02083 if (TYPE(val) == type) return val; 02084 v = convert_type(val, tname, method, TRUE); 02085 if (TYPE(v) != type) { 02086 const char *cname = rb_obj_classname(val); 02087 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)", 02088 cname, tname, cname, method, rb_obj_classname(v)); 02089 } 02090 return v; 02091 } 02092 02093 VALUE 02094 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method) 02095 { 02096 VALUE v; 02097 02098 /* always convert T_DATA */ 02099 if (TYPE(val) == type && type != T_DATA) return val; 02100 v = convert_type(val, tname, method, FALSE); 02101 if (NIL_P(v)) return Qnil; 02102 if (TYPE(v) != type) { 02103 const char *cname = rb_obj_classname(val); 02104 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)", 02105 cname, tname, cname, method, rb_obj_classname(v)); 02106 } 02107 return v; 02108 } 02109 02110 02111 static VALUE 02112 rb_to_integer(VALUE val, const char *method) 02113 { 02114 VALUE v; 02115 02116 if (FIXNUM_P(val)) return val; 02117 if (TYPE(val) == T_BIGNUM) return val; 02118 v = convert_type(val, "Integer", method, TRUE); 02119 if (!rb_obj_is_kind_of(v, rb_cInteger)) { 02120 const char *cname = rb_obj_classname(val); 02121 rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)", 02122 cname, cname, method, rb_obj_classname(v)); 02123 } 02124 return v; 02125 } 02126 02127 VALUE 02128 rb_check_to_integer(VALUE val, const char *method) 02129 { 02130 VALUE v; 02131 02132 if (FIXNUM_P(val)) return val; 02133 if (TYPE(val) == T_BIGNUM) return val; 02134 v = convert_type(val, "Integer", method, FALSE); 02135 if (!rb_obj_is_kind_of(v, rb_cInteger)) { 02136 return Qnil; 02137 } 02138 return v; 02139 } 02140 02141 VALUE 02142 rb_to_int(VALUE val) 02143 { 02144 return rb_to_integer(val, "to_int"); 02145 } 02146 02147 static VALUE 02148 rb_convert_to_integer(VALUE val, int base) 02149 { 02150 VALUE tmp; 02151 02152 switch (TYPE(val)) { 02153 case T_FLOAT: 02154 if (base != 0) goto arg_error; 02155 if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX 02156 && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) { 02157 break; 02158 } 02159 return rb_dbl2big(RFLOAT_VALUE(val)); 02160 02161 case T_FIXNUM: 02162 case T_BIGNUM: 02163 if (base != 0) goto arg_error; 02164 return val; 02165 02166 case T_STRING: 02167 string_conv: 02168 return rb_str_to_inum(val, base, TRUE); 02169 02170 case T_NIL: 02171 if (base != 0) goto arg_error; 02172 rb_raise(rb_eTypeError, "can't convert nil into Integer"); 02173 break; 02174 02175 default: 02176 break; 02177 } 02178 if (base != 0) { 02179 tmp = rb_check_string_type(val); 02180 if (!NIL_P(tmp)) goto string_conv; 02181 arg_error: 02182 rb_raise(rb_eArgError, "base specified for non string value"); 02183 } 02184 tmp = convert_type(val, "Integer", "to_int", FALSE); 02185 if (NIL_P(tmp)) { 02186 return rb_to_integer(val, "to_i"); 02187 } 02188 return tmp; 02189 02190 } 02191 02192 VALUE 02193 rb_Integer(VALUE val) 02194 { 02195 return rb_convert_to_integer(val, 0); 02196 } 02197 02198 /* 02199 * call-seq: 02200 * Integer(arg,base=0) -> integer 02201 * 02202 * Converts <i>arg</i> to a <code>Fixnum</code> or <code>Bignum</code>. 02203 * Numeric types are converted directly (with floating point numbers 02204 * being truncated). <i>base</i> (0, or between 2 and 36) is a base for 02205 * integer string representation. If <i>arg</i> is a <code>String</code>, 02206 * when <i>base</i> is omitted or equals to zero, radix indicators 02207 * (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored. 02208 * In any case, strings should be strictly conformed to numeric 02209 * representation. This behavior is different from that of 02210 * <code>String#to_i</code>. Non string values will be converted using 02211 * <code>to_int</code>, and <code>to_i</code>. 02212 * 02213 * Integer(123.999) #=> 123 02214 * Integer("0x1a") #=> 26 02215 * Integer(Time.new) #=> 1204973019 02216 * Integer("0930", 10) #=> 930 02217 * Integer("111", 2) #=> 7 02218 */ 02219 02220 static VALUE 02221 rb_f_integer(int argc, VALUE *argv, VALUE obj) 02222 { 02223 VALUE arg = Qnil; 02224 int base = 0; 02225 02226 switch (argc) { 02227 case 2: 02228 base = NUM2INT(argv[1]); 02229 case 1: 02230 arg = argv[0]; 02231 break; 02232 default: 02233 /* should cause ArgumentError */ 02234 rb_scan_args(argc, argv, "11", NULL, NULL); 02235 } 02236 return rb_convert_to_integer(arg, base); 02237 } 02238 02239 double 02240 rb_cstr_to_dbl(const char *p, int badcheck) 02241 { 02242 const char *q; 02243 char *end; 02244 double d; 02245 const char *ellipsis = ""; 02246 int w; 02247 enum {max_width = 20}; 02248 #define OutOfRange() ((end - p > max_width) ? \ 02249 (w = max_width, ellipsis = "...") : \ 02250 (w = (int)(end - p), ellipsis = "")) 02251 02252 if (!p) return 0.0; 02253 q = p; 02254 while (ISSPACE(*p)) p++; 02255 02256 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 02257 return 0.0; 02258 } 02259 02260 d = strtod(p, &end); 02261 if (errno == ERANGE) { 02262 OutOfRange(); 02263 rb_warning("Float %.*s%s out of range", w, p, ellipsis); 02264 errno = 0; 02265 } 02266 if (p == end) { 02267 if (badcheck) { 02268 bad: 02269 rb_invalid_str(q, "Float()"); 02270 } 02271 return d; 02272 } 02273 if (*end) { 02274 char buf[DBL_DIG * 4 + 10]; 02275 char *n = buf; 02276 char *e = buf + sizeof(buf) - 1; 02277 char prev = 0; 02278 02279 while (p < end && n < e) prev = *n++ = *p++; 02280 while (*p) { 02281 if (*p == '_') { 02282 /* remove underscores between digits */ 02283 if (badcheck) { 02284 if (n == buf || !ISDIGIT(prev)) goto bad; 02285 ++p; 02286 if (!ISDIGIT(*p)) goto bad; 02287 } 02288 else { 02289 while (*++p == '_'); 02290 continue; 02291 } 02292 } 02293 prev = *p++; 02294 if (n < e) *n++ = prev; 02295 } 02296 *n = '\0'; 02297 p = buf; 02298 02299 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 02300 return 0.0; 02301 } 02302 02303 d = strtod(p, &end); 02304 if (errno == ERANGE) { 02305 OutOfRange(); 02306 rb_warning("Float %.*s%s out of range", w, p, ellipsis); 02307 errno = 0; 02308 } 02309 if (badcheck) { 02310 if (!end || p == end) goto bad; 02311 while (*end && ISSPACE(*end)) end++; 02312 if (*end) goto bad; 02313 } 02314 } 02315 if (errno == ERANGE) { 02316 errno = 0; 02317 OutOfRange(); 02318 rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis); 02319 } 02320 return d; 02321 } 02322 02323 double 02324 rb_str_to_dbl(VALUE str, int badcheck) 02325 { 02326 char *s; 02327 long len; 02328 double ret; 02329 VALUE v = 0; 02330 02331 StringValue(str); 02332 s = RSTRING_PTR(str); 02333 len = RSTRING_LEN(str); 02334 if (s) { 02335 if (badcheck && memchr(s, '\0', len)) { 02336 rb_raise(rb_eArgError, "string for Float contains null byte"); 02337 } 02338 if (s[len]) { /* no sentinel somehow */ 02339 char *p = ALLOCV(v, len); 02340 MEMCPY(p, s, char, len); 02341 p[len] = '\0'; 02342 s = p; 02343 } 02344 } 02345 ret = rb_cstr_to_dbl(s, badcheck); 02346 if (v) 02347 ALLOCV_END(v); 02348 return ret; 02349 } 02350 02351 VALUE 02352 rb_Float(VALUE val) 02353 { 02354 switch (TYPE(val)) { 02355 case T_FIXNUM: 02356 return DBL2NUM((double)FIX2LONG(val)); 02357 02358 case T_FLOAT: 02359 return val; 02360 02361 case T_BIGNUM: 02362 return DBL2NUM(rb_big2dbl(val)); 02363 02364 case T_STRING: 02365 return DBL2NUM(rb_str_to_dbl(val, TRUE)); 02366 02367 case T_NIL: 02368 rb_raise(rb_eTypeError, "can't convert nil into Float"); 02369 break; 02370 02371 default: 02372 return rb_convert_type(val, T_FLOAT, "Float", "to_f"); 02373 } 02374 } 02375 02376 /* 02377 * call-seq: 02378 * Float(arg) -> float 02379 * 02380 * Returns <i>arg</i> converted to a float. Numeric types are converted 02381 * directly, the rest are converted using <i>arg</i>.to_f. As of Ruby 02382 * 1.8, converting <code>nil</code> generates a <code>TypeError</code>. 02383 * 02384 * Float(1) #=> 1.0 02385 * Float("123.456") #=> 123.456 02386 */ 02387 02388 static VALUE 02389 rb_f_float(VALUE obj, VALUE arg) 02390 { 02391 return rb_Float(arg); 02392 } 02393 02394 VALUE 02395 rb_to_float(VALUE val) 02396 { 02397 if (TYPE(val) == T_FLOAT) return val; 02398 if (!rb_obj_is_kind_of(val, rb_cNumeric)) { 02399 rb_raise(rb_eTypeError, "can't convert %s into Float", 02400 NIL_P(val) ? "nil" : 02401 val == Qtrue ? "true" : 02402 val == Qfalse ? "false" : 02403 rb_obj_classname(val)); 02404 } 02405 return rb_convert_type(val, T_FLOAT, "Float", "to_f"); 02406 } 02407 02408 VALUE 02409 rb_check_to_float(VALUE val) 02410 { 02411 if (TYPE(val) == T_FLOAT) return val; 02412 if (!rb_obj_is_kind_of(val, rb_cNumeric)) { 02413 return Qnil; 02414 } 02415 return rb_check_convert_type(val, T_FLOAT, "Float", "to_f"); 02416 } 02417 02418 double 02419 rb_num2dbl(VALUE val) 02420 { 02421 switch (TYPE(val)) { 02422 case T_FLOAT: 02423 return RFLOAT_VALUE(val); 02424 02425 case T_STRING: 02426 rb_raise(rb_eTypeError, "no implicit conversion to float from string"); 02427 break; 02428 02429 case T_NIL: 02430 rb_raise(rb_eTypeError, "no implicit conversion to float from nil"); 02431 break; 02432 02433 default: 02434 break; 02435 } 02436 02437 return RFLOAT_VALUE(rb_Float(val)); 02438 } 02439 02440 VALUE 02441 rb_String(VALUE val) 02442 { 02443 VALUE tmp = rb_check_string_type(val); 02444 if (NIL_P(tmp)) 02445 tmp = rb_convert_type(val, T_STRING, "String", "to_s"); 02446 return tmp; 02447 } 02448 02449 02450 /* 02451 * call-seq: 02452 * String(arg) -> string 02453 * 02454 * Converts <i>arg</i> to a <code>String</code> by calling its 02455 * <code>to_s</code> method. 02456 * 02457 * String(self) #=> "main" 02458 * String(self.class) #=> "Object" 02459 * String(123456) #=> "123456" 02460 */ 02461 02462 static VALUE 02463 rb_f_string(VALUE obj, VALUE arg) 02464 { 02465 return rb_String(arg); 02466 } 02467 02468 VALUE 02469 rb_Array(VALUE val) 02470 { 02471 VALUE tmp = rb_check_array_type(val); 02472 02473 if (NIL_P(tmp)) { 02474 tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a"); 02475 if (NIL_P(tmp)) { 02476 return rb_ary_new3(1, val); 02477 } 02478 } 02479 return tmp; 02480 } 02481 02482 /* 02483 * call-seq: 02484 * Array(arg) -> array 02485 * 02486 * Returns <i>arg</i> as an <code>Array</code>. First tries to call 02487 * <i>arg</i><code>.to_ary</code>, then <i>arg</i><code>.to_a</code>. 02488 * 02489 * Array(1..5) #=> [1, 2, 3, 4, 5] 02490 */ 02491 02492 static VALUE 02493 rb_f_array(VALUE obj, VALUE arg) 02494 { 02495 return rb_Array(arg); 02496 } 02497 02498 /* 02499 * Document-class: Class 02500 * 02501 * Classes in Ruby are first-class objects---each is an instance of 02502 * class <code>Class</code>. 02503 * 02504 * When a new class is created (typically using <code>class Name ... 02505 * end</code>), an object of type <code>Class</code> is created and 02506 * assigned to a global constant (<code>Name</code> in this case). When 02507 * <code>Name.new</code> is called to create a new object, the 02508 * <code>new</code> method in <code>Class</code> is run by default. 02509 * This can be demonstrated by overriding <code>new</code> in 02510 * <code>Class</code>: 02511 * 02512 * class Class 02513 * alias oldNew new 02514 * def new(*args) 02515 * print "Creating a new ", self.name, "\n" 02516 * oldNew(*args) 02517 * end 02518 * end 02519 * 02520 * 02521 * class Name 02522 * end 02523 * 02524 * 02525 * n = Name.new 02526 * 02527 * <em>produces:</em> 02528 * 02529 * Creating a new Name 02530 * 02531 * Classes, modules, and objects are interrelated. In the diagram 02532 * that follows, the vertical arrows represent inheritance, and the 02533 * parentheses meta-classes. All metaclasses are instances 02534 * of the class `Class'. 02535 * +---------+ +-... 02536 * | | | 02537 * BasicObject-----|-->(BasicObject)-------|-... 02538 * ^ | ^ | 02539 * | | | | 02540 * Object---------|----->(Object)---------|-... 02541 * ^ | ^ | 02542 * | | | | 02543 * +-------+ | +--------+ | 02544 * | | | | | | 02545 * | Module-|---------|--->(Module)-|-... 02546 * | ^ | | ^ | 02547 * | | | | | | 02548 * | Class-|---------|---->(Class)-|-... 02549 * | ^ | | ^ | 02550 * | +---+ | +----+ 02551 * | | 02552 * obj--->OtherClass---------->(OtherClass)-----------... 02553 * 02554 */ 02555 02556 02575 /* Document-class: BasicObject 02576 * 02577 * BasicObject is the parent class of all classes in Ruby. It's an explicit 02578 * blank class. 02579 * 02580 * BasicObject can be used for creating object hierarchies independent of 02581 * Ruby's object hierarchy, proxy objects like the Delegator class, or other 02582 * uses where namespace pollution from Ruby's methods and classes must be 02583 * avoided. 02584 * 02585 * To avoid polluting BasicObject for other users an appropriately named 02586 * subclass of BasicObject should be created instead of directly modifying 02587 * BasicObject: 02588 * 02589 * class MyObjectSystem < BasicObject 02590 * end 02591 * 02592 * BasicObject does not include Kernel (for methods like +puts+) and 02593 * BasicObject is outside of the namespace of the standard library so common 02594 * classes will not be found without a using a full class path. 02595 * 02596 * A variety of strategies can be used to provide useful portions of the 02597 * standard library to subclasses of BasicObject. A subclass could 02598 * <code>include Kernel</code> to obtain +puts+, +exit+, etc. A custom 02599 * Kernel-like module could be created and included or delegation can be used 02600 * via #method_missing: 02601 * 02602 * class MyObjectSystem < BasicObject 02603 * DELEGATE = [:puts, :p] 02604 * 02605 * def method_missing(name, *args, &block) 02606 * super unless DELEGATE.include? name 02607 * ::Kernel.send(name, *args, &block) 02608 * end 02609 * 02610 * def respond_to_missing?(name, include_private = false) 02611 * DELGATE.include?(name) or super 02612 * end 02613 * end 02614 * 02615 * Access to classes and modules from the Ruby standard library can be 02616 * obtained in a BasicObject subclass by referencing the desired constant 02617 * from the root like <code>::File</code> or <code>::Enumerator</code>. 02618 * Like #method_missing, #const_missing can be used to delegate constant 02619 * lookup to +Object+: 02620 * 02621 * class MyObjectSystem < BasicObject 02622 * def self.const_missing(name) 02623 * ::Object.const_get(name) 02624 * end 02625 * end 02626 */ 02627 02628 /* Document-class: Object 02629 * 02630 * Object is the root of Ruby's class hierarchy. Its methods are available 02631 * to all classes unless explicitly overridden. 02632 * 02633 * Object mixes in the Kernel module, making the built-in kernel functions 02634 * globally accessible. Although the instance methods of Object are defined 02635 * by the Kernel module, we have chosen to document them here for clarity. 02636 * 02637 * In the descriptions of Object's methods, the parameter <i>symbol</i> refers 02638 * to a symbol, which is either a quoted string or a Symbol (such as 02639 * <code>:name</code>). 02640 */ 02641 02642 void 02643 Init_Object(void) 02644 { 02645 int i; 02646 02647 Init_class_hierarchy(); 02648 02649 #if 0 02650 // teach RDoc about these classes 02651 rb_cBasicObject = rb_define_class("BasicObject", Qnil); 02652 rb_cObject = rb_define_class("Object", rb_cBasicObject); 02653 rb_cModule = rb_define_class("Module", rb_cObject); 02654 rb_cClass = rb_define_class("Class", rb_cModule); 02655 #endif 02656 02657 #undef rb_intern 02658 #define rb_intern(str) rb_intern_const(str) 02659 02660 rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0); 02661 rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance); 02662 rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1); 02663 rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1); 02664 rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0); 02665 rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1); 02666 02667 rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1); 02668 rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1); 02669 rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1); 02670 02671 rb_mKernel = rb_define_module("Kernel"); 02672 rb_include_module(rb_cObject, rb_mKernel); 02673 rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1); 02674 rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1); 02675 rb_define_private_method(rb_cModule, "extended", rb_obj_dummy, 1); 02676 rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1); 02677 rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1); 02678 rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1); 02679 02680 rb_define_method(rb_mKernel, "nil?", rb_false, 0); 02681 rb_define_method(rb_mKernel, "===", rb_equal, 1); 02682 rb_define_method(rb_mKernel, "=~", rb_obj_match, 1); 02683 rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1); 02684 rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1); 02685 rb_define_method(rb_mKernel, "hash", rb_obj_hash, 0); 02686 rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1); 02687 02688 rb_define_method(rb_mKernel, "class", rb_obj_class, 0); 02689 rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0); 02690 rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0); 02691 rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0); 02692 rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1); 02693 rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1); 02694 rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1); 02695 02696 rb_define_method(rb_mKernel, "taint", rb_obj_taint, 0); 02697 rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0); 02698 rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0); 02699 rb_define_method(rb_mKernel, "untrust", rb_obj_untrust, 0); 02700 rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0); 02701 rb_define_method(rb_mKernel, "trust", rb_obj_trust, 0); 02702 rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0); 02703 rb_define_method(rb_mKernel, "frozen?", rb_obj_frozen_p, 0); 02704 02705 rb_define_method(rb_mKernel, "to_s", rb_any_to_s, 0); 02706 rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0); 02707 rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */ 02708 rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */ 02709 rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */ 02710 rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */ 02711 rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */ 02712 rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */ 02713 rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1); 02714 rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2); 02715 rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1); 02716 rb_define_private_method(rb_mKernel, "remove_instance_variable", 02717 rb_obj_remove_instance_variable, 1); /* in variable.c */ 02718 02719 rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1); 02720 rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1); 02721 rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1); 02722 rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0); 02723 02724 rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */ 02725 rb_define_global_function("format", rb_f_sprintf, -1); /* in sprintf.c */ 02726 02727 rb_define_global_function("Integer", rb_f_integer, -1); 02728 rb_define_global_function("Float", rb_f_float, 1); 02729 02730 rb_define_global_function("String", rb_f_string, 1); 02731 rb_define_global_function("Array", rb_f_array, 1); 02732 02733 rb_cNilClass = rb_define_class("NilClass", rb_cObject); 02734 rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0); 02735 rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0); 02736 rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0); 02737 rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0); 02738 rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0); 02739 rb_define_method(rb_cNilClass, "&", false_and, 1); 02740 rb_define_method(rb_cNilClass, "|", false_or, 1); 02741 rb_define_method(rb_cNilClass, "^", false_xor, 1); 02742 02743 rb_define_method(rb_cNilClass, "nil?", rb_true, 0); 02744 rb_undef_alloc_func(rb_cNilClass); 02745 rb_undef_method(CLASS_OF(rb_cNilClass), "new"); 02746 /* 02747 * An alias of +nil+ 02748 */ 02749 rb_define_global_const("NIL", Qnil); 02750 02751 rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0); 02752 rb_define_method(rb_cModule, "===", rb_mod_eqq, 1); 02753 rb_define_method(rb_cModule, "==", rb_obj_equal, 1); 02754 rb_define_method(rb_cModule, "<=>", rb_mod_cmp, 1); 02755 rb_define_method(rb_cModule, "<", rb_mod_lt, 1); 02756 rb_define_method(rb_cModule, "<=", rb_class_inherited_p, 1); 02757 rb_define_method(rb_cModule, ">", rb_mod_gt, 1); 02758 rb_define_method(rb_cModule, ">=", rb_mod_ge, 1); 02759 rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */ 02760 rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0); 02761 rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */ 02762 rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */ 02763 rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */ 02764 rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */ 02765 02766 rb_define_private_method(rb_cModule, "attr", rb_mod_attr, -1); 02767 rb_define_private_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1); 02768 rb_define_private_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1); 02769 rb_define_private_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1); 02770 02771 rb_define_alloc_func(rb_cModule, rb_module_s_alloc); 02772 rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0); 02773 rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */ 02774 rb_define_method(rb_cModule, "public_instance_methods", 02775 rb_class_public_instance_methods, -1); /* in class.c */ 02776 rb_define_method(rb_cModule, "protected_instance_methods", 02777 rb_class_protected_instance_methods, -1); /* in class.c */ 02778 rb_define_method(rb_cModule, "private_instance_methods", 02779 rb_class_private_instance_methods, -1); /* in class.c */ 02780 02781 rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */ 02782 rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1); 02783 rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2); 02784 rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1); 02785 rb_define_private_method(rb_cModule, "remove_const", 02786 rb_mod_remove_const, 1); /* in variable.c */ 02787 rb_define_method(rb_cModule, "const_missing", 02788 rb_mod_const_missing, 1); /* in variable.c */ 02789 rb_define_method(rb_cModule, "class_variables", 02790 rb_mod_class_variables, 0); /* in variable.c */ 02791 rb_define_method(rb_cModule, "remove_class_variable", 02792 rb_mod_remove_cvar, 1); /* in variable.c */ 02793 rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1); 02794 rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2); 02795 rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1); 02796 rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); 02797 rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); 02798 02799 rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0); 02800 rb_define_method(rb_cClass, "new", rb_class_new_instance, -1); 02801 rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1); 02802 rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0); 02803 rb_define_alloc_func(rb_cClass, rb_class_s_alloc); 02804 rb_undef_method(rb_cClass, "extend_object"); 02805 rb_undef_method(rb_cClass, "append_features"); 02806 02807 rb_cData = rb_define_class("Data", rb_cObject); 02808 rb_undef_alloc_func(rb_cData); 02809 02810 rb_cTrueClass = rb_define_class("TrueClass", rb_cObject); 02811 rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0); 02812 rb_define_method(rb_cTrueClass, "&", true_and, 1); 02813 rb_define_method(rb_cTrueClass, "|", true_or, 1); 02814 rb_define_method(rb_cTrueClass, "^", true_xor, 1); 02815 rb_undef_alloc_func(rb_cTrueClass); 02816 rb_undef_method(CLASS_OF(rb_cTrueClass), "new"); 02817 /* 02818 * An alias of +true+ 02819 */ 02820 rb_define_global_const("TRUE", Qtrue); 02821 02822 rb_cFalseClass = rb_define_class("FalseClass", rb_cObject); 02823 rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0); 02824 rb_define_method(rb_cFalseClass, "&", false_and, 1); 02825 rb_define_method(rb_cFalseClass, "|", false_or, 1); 02826 rb_define_method(rb_cFalseClass, "^", false_xor, 1); 02827 rb_undef_alloc_func(rb_cFalseClass); 02828 rb_undef_method(CLASS_OF(rb_cFalseClass), "new"); 02829 /* 02830 * An alias of +false+ 02831 */ 02832 rb_define_global_const("FALSE", Qfalse); 02833 02834 id_eq = rb_intern("=="); 02835 id_eql = rb_intern("eql?"); 02836 id_match = rb_intern("=~"); 02837 id_inspect = rb_intern("inspect"); 02838 id_init_copy = rb_intern("initialize_copy"); 02839 id_init_clone = rb_intern("initialize_clone"); 02840 id_init_dup = rb_intern("initialize_dup"); 02841 02842 for (i=0; conv_method_names[i].method; i++) { 02843 conv_method_names[i].id = rb_intern(conv_method_names[i].method); 02844 } 02845 } 02846
1.7.6.1