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