* file.c (rb_find_file_ext): guard load_path from GC.
[ruby-svn.git] / object.c
blob39d6d866fe0f390c2d26ee943caa16a00282135d
1 /**********************************************************************
3 object.c -
5 $Author$
6 created at: Thu Jul 15 12:01:24 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
12 **********************************************************************/
14 #include "ruby/ruby.h"
15 #include "ruby/st.h"
16 #include "ruby/util.h"
17 #include "debug.h"
18 #include <stdio.h>
19 #include <errno.h>
20 #include <ctype.h>
21 #include <math.h>
22 #include <float.h>
24 VALUE rb_cBasicObject;
25 VALUE rb_mKernel;
26 VALUE rb_cObject;
27 VALUE rb_cModule;
28 VALUE rb_cClass;
29 VALUE rb_cData;
31 VALUE rb_cNilClass;
32 VALUE rb_cTrueClass;
33 VALUE rb_cFalseClass;
35 static ID id_eq, id_eql, id_match, id_inspect, id_init_copy;
38 * call-seq:
39 * obj === other => true or false
41 * Case Equality---For class <code>Object</code>, effectively the same
42 * as calling <code>#==</code>, but typically overridden by descendents
43 * to provide meaningful semantics in <code>case</code> statements.
46 VALUE
47 rb_equal(VALUE obj1, VALUE obj2)
49 VALUE result;
51 if (obj1 == obj2) return Qtrue;
52 result = rb_funcall(obj1, id_eq, 1, obj2);
53 if (RTEST(result)) return Qtrue;
54 return Qfalse;
57 int
58 rb_eql(VALUE obj1, VALUE obj2)
60 return RTEST(rb_funcall(obj1, id_eql, 1, obj2));
64 * call-seq:
65 * obj == other => true or false
66 * obj.equal?(other) => true or false
67 * obj.eql?(other) => true or false
69 * Equality---At the <code>Object</code> level, <code>==</code> returns
70 * <code>true</code> only if <i>obj</i> and <i>other</i> are the
71 * same object. Typically, this method is overridden in descendent
72 * classes to provide class-specific meaning.
74 * Unlike <code>==</code>, the <code>equal?</code> method should never be
75 * overridden by subclasses: it is used to determine object identity
76 * (that is, <code>a.equal?(b)</code> iff <code>a</code> is the same
77 * object as <code>b</code>).
79 * The <code>eql?</code> method returns <code>true</code> if
80 * <i>obj</i> and <i>anObject</i> have the same value. Used by
81 * <code>Hash</code> to test members for equality. For objects of
82 * class <code>Object</code>, <code>eql?</code> is synonymous with
83 * <code>==</code>. Subclasses normally continue this tradition, but
84 * there are exceptions. <code>Numeric</code> types, for example,
85 * perform type conversion across <code>==</code>, but not across
86 * <code>eql?</code>, so:
88 * 1 == 1.0 #=> true
89 * 1.eql? 1.0 #=> false
92 VALUE
93 rb_obj_equal(VALUE obj1, VALUE obj2)
95 if (obj1 == obj2) return Qtrue;
96 return Qfalse;
100 * call-seq:
101 * !obj => true or false
103 * Boolean negate.
106 VALUE
107 rb_obj_not(VALUE obj)
109 return RTEST(obj) ? Qfalse : Qtrue;
113 * call-seq:
114 * obj != other => true or false
116 * Returns true if two objects are not-equal, otherwise false.
119 VALUE
120 rb_obj_not_equal(VALUE obj1, VALUE obj2)
122 VALUE result = rb_funcall(obj1, id_eq, 1, obj2);
123 return RTEST(result) ? Qfalse : Qtrue;
126 VALUE
127 rb_class_real(VALUE cl)
129 if (cl == 0)
130 return 0;
131 while ((RBASIC(cl)->flags & FL_SINGLETON) || BUILTIN_TYPE(cl) == T_ICLASS) {
132 cl = RCLASS_SUPER(cl);
134 return cl;
138 * call-seq:
139 * obj.class => class
141 * Returns the class of <i>obj</i>, now preferred over
142 * <code>Object#type</code>, as an object's type in Ruby is only
143 * loosely tied to that object's class. This method must always be
144 * called with an explicit receiver, as <code>class</code> is also a
145 * reserved word in Ruby.
147 * 1.class #=> Fixnum
148 * self.class #=> Object
151 VALUE
152 rb_obj_class(VALUE obj)
154 return rb_class_real(CLASS_OF(obj));
157 static void
158 init_copy(VALUE dest, VALUE obj)
160 if (OBJ_FROZEN(dest)) {
161 rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
163 RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
164 RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR|FL_TAINT);
165 rb_copy_generic_ivar(dest, obj);
166 rb_gc_copy_finalizer(dest, obj);
167 switch (TYPE(obj)) {
168 case T_OBJECT:
169 if (!(RBASIC(dest)->flags & ROBJECT_EMBED) && ROBJECT_IVPTR(dest)) {
170 xfree(ROBJECT_IVPTR(dest));
171 ROBJECT(dest)->as.heap.ivptr = 0;
172 ROBJECT(dest)->as.heap.numiv = 0;
173 ROBJECT(dest)->as.heap.iv_index_tbl = 0;
175 if (RBASIC(obj)->flags & ROBJECT_EMBED) {
176 MEMCPY(ROBJECT(dest)->as.ary, ROBJECT(obj)->as.ary, VALUE, ROBJECT_EMBED_LEN_MAX);
177 RBASIC(dest)->flags |= ROBJECT_EMBED;
179 else {
180 long len = ROBJECT(obj)->as.heap.numiv;
181 VALUE *ptr = ALLOC_N(VALUE, len);
182 MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
183 ROBJECT(dest)->as.heap.ivptr = ptr;
184 ROBJECT(dest)->as.heap.numiv = len;
185 ROBJECT(dest)->as.heap.iv_index_tbl = ROBJECT(obj)->as.heap.iv_index_tbl;
186 RBASIC(dest)->flags &= ~ROBJECT_EMBED;
188 break;
189 case T_CLASS:
190 case T_MODULE:
191 if (RCLASS_IV_TBL(dest)) {
192 st_free_table(RCLASS_IV_TBL(dest));
193 RCLASS_IV_TBL(dest) = 0;
195 if (RCLASS_IV_TBL(obj)) {
196 RCLASS_IV_TBL(dest) = st_copy(RCLASS_IV_TBL(obj));
198 break;
200 rb_funcall(dest, id_init_copy, 1, obj);
204 * call-seq:
205 * obj.clone -> an_object
207 * Produces a shallow copy of <i>obj</i>---the instance variables of
208 * <i>obj</i> are copied, but not the objects they reference. Copies
209 * the frozen and tainted state of <i>obj</i>. See also the discussion
210 * under <code>Object#dup</code>.
212 * class Klass
213 * attr_accessor :str
214 * end
215 * s1 = Klass.new #=> #<Klass:0x401b3a38>
216 * s1.str = "Hello" #=> "Hello"
217 * s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello">
218 * s2.str[1,4] = "i" #=> "i"
219 * s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
220 * s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
222 * This method may have class-specific behavior. If so, that
223 * behavior will be documented under the #+initialize_copy+ method of
224 * the class.
227 VALUE
228 rb_obj_clone(VALUE obj)
230 VALUE clone;
232 if (rb_special_const_p(obj)) {
233 rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
235 clone = rb_obj_alloc(rb_obj_class(obj));
236 RBASIC(clone)->klass = rb_singleton_class_clone(obj);
237 RBASIC(clone)->flags = (RBASIC(obj)->flags | FL_TEST(clone, FL_TAINT)) & ~(FL_FREEZE|FL_FINALIZE);
238 init_copy(clone, obj);
239 RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
241 return clone;
245 * call-seq:
246 * obj.dup -> an_object
248 * Produces a shallow copy of <i>obj</i>---the instance variables of
249 * <i>obj</i> are copied, but not the objects they reference.
250 * <code>dup</code> copies the tainted state of <i>obj</i>. See also
251 * the discussion under <code>Object#clone</code>. In general,
252 * <code>clone</code> and <code>dup</code> may have different semantics
253 * in descendent classes. While <code>clone</code> is used to duplicate
254 * an object, including its internal state, <code>dup</code> typically
255 * uses the class of the descendent object to create the new instance.
257 * This method may have class-specific behavior. If so, that
258 * behavior will be documented under the #+initialize_copy+ method of
259 * the class.
262 VALUE
263 rb_obj_dup(VALUE obj)
265 VALUE dup;
267 if (rb_special_const_p(obj)) {
268 rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj));
270 dup = rb_obj_alloc(rb_obj_class(obj));
271 init_copy(dup, obj);
273 return dup;
276 /* :nodoc: */
277 VALUE
278 rb_obj_init_copy(VALUE obj, VALUE orig)
280 if (obj == orig) return obj;
281 rb_check_frozen(obj);
282 if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
283 rb_raise(rb_eTypeError, "initialize_copy should take same class object");
285 return obj;
289 * call-seq:
290 * obj.to_s => string
292 * Returns a string representing <i>obj</i>. The default
293 * <code>to_s</code> prints the object's class and an encoding of the
294 * object id. As a special case, the top-level object that is the
295 * initial execution context of Ruby programs returns ``main.''
298 VALUE
299 rb_any_to_s(VALUE obj)
301 char *cname = rb_obj_classname(obj);
302 VALUE str;
304 str = rb_sprintf("#<%s:%p>", cname, (void*)obj);
305 if (OBJ_TAINTED(obj)) OBJ_TAINT(str);
307 return str;
310 VALUE
311 rb_inspect(VALUE obj)
313 return rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
316 static int
317 inspect_i(ID id, VALUE value, VALUE str)
319 VALUE str2;
320 const char *ivname;
322 /* need not to show internal data */
323 if (CLASS_OF(value) == 0) return ST_CONTINUE;
324 if (!rb_is_instance_id(id)) return ST_CONTINUE;
325 if (RSTRING_PTR(str)[0] == '-') { /* first element */
326 RSTRING_PTR(str)[0] = '#';
327 rb_str_cat2(str, " ");
329 else {
330 rb_str_cat2(str, ", ");
332 ivname = rb_id2name(id);
333 rb_str_cat2(str, ivname);
334 rb_str_cat2(str, "=");
335 str2 = rb_inspect(value);
336 rb_str_append(str, str2);
337 OBJ_INFECT(str, str2);
339 return ST_CONTINUE;
342 static VALUE
343 inspect_obj(VALUE obj, VALUE str, int recur)
345 if (recur) {
346 rb_str_cat2(str, " ...");
348 else {
349 rb_ivar_foreach(obj, inspect_i, str);
351 rb_str_cat2(str, ">");
352 RSTRING_PTR(str)[0] = '#';
353 OBJ_INFECT(str, obj);
355 return str;
359 * call-seq:
360 * obj.inspect => string
362 * Returns a string containing a human-readable representation of
363 * <i>obj</i>. If not overridden, uses the <code>to_s</code> method to
364 * generate the string.
366 * [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
367 * Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
371 static VALUE
372 rb_obj_inspect(VALUE obj)
375 if (TYPE(obj) == T_OBJECT) {
376 int has_ivar = 0;
377 VALUE *ptr = ROBJECT_IVPTR(obj);
378 long len = ROBJECT_NUMIV(obj);
379 long i;
381 for (i = 0; i < len; i++) {
382 if (ptr[i] != Qundef) {
383 has_ivar = 1;
384 break;
388 if (has_ivar) {
389 VALUE str;
390 char *c;
392 c = rb_obj_classname(obj);
393 str = rb_sprintf("-<%s:%p", c, (void*)obj);
394 return rb_exec_recursive(inspect_obj, obj, str);
397 return rb_funcall(obj, rb_intern("to_s"), 0, 0);
402 * call-seq:
403 * obj.instance_of?(class) => true or false
405 * Returns <code>true</code> if <i>obj</i> is an instance of the given
406 * class. See also <code>Object#kind_of?</code>.
409 VALUE
410 rb_obj_is_instance_of(VALUE obj, VALUE c)
412 switch (TYPE(c)) {
413 case T_MODULE:
414 case T_CLASS:
415 case T_ICLASS:
416 break;
417 default:
418 rb_raise(rb_eTypeError, "class or module required");
421 if (rb_obj_class(obj) == c) return Qtrue;
422 return Qfalse;
427 * call-seq:
428 * obj.is_a?(class) => true or false
429 * obj.kind_of?(class) => true or false
431 * Returns <code>true</code> if <i>class</i> is the class of
432 * <i>obj</i>, or if <i>class</i> is one of the superclasses of
433 * <i>obj</i> or modules included in <i>obj</i>.
435 * module M; end
436 * class A
437 * include M
438 * end
439 * class B < A; end
440 * class C < B; end
441 * b = B.new
442 * b.instance_of? A #=> false
443 * b.instance_of? B #=> true
444 * b.instance_of? C #=> false
445 * b.instance_of? M #=> false
446 * b.kind_of? A #=> true
447 * b.kind_of? B #=> true
448 * b.kind_of? C #=> false
449 * b.kind_of? M #=> true
452 VALUE
453 rb_obj_is_kind_of(VALUE obj, VALUE c)
455 VALUE cl = CLASS_OF(obj);
457 switch (TYPE(c)) {
458 case T_MODULE:
459 case T_CLASS:
460 case T_ICLASS:
461 break;
463 default:
464 rb_raise(rb_eTypeError, "class or module required");
467 while (cl) {
468 if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
469 return Qtrue;
470 cl = RCLASS_SUPER(cl);
472 return Qfalse;
477 * call-seq:
478 * obj.tap{|x|...} => obj
480 * Yields <code>x</code> to the block, and then returns <code>x</code>.
481 * The primary purpose of this method is to "tap into" a method chain,
482 * in order to perform operations on intermediate results within the chain.
484 * (1..10) .tap {|x| puts "original: #{x.inspect}"}
485 * .to_a .tap {|x| puts "array: #{x.inspect}"}
486 * .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"}
487 * .map { |x| x*x } .tap {|x| puts "squares: #{x.inspect}"}
491 VALUE
492 rb_obj_tap(VALUE obj)
494 rb_yield(obj);
495 return obj;
500 * Document-method: inherited
502 * call-seq:
503 * inherited(subclass)
505 * Callback invoked whenever a subclass of the current class is created.
507 * Example:
509 * class Foo
510 * def self.inherited(subclass)
511 * puts "New subclass: #{subclass}"
512 * end
513 * end
515 * class Bar < Foo
516 * end
518 * class Baz < Bar
519 * end
521 * produces:
523 * New subclass: Bar
524 * New subclass: Baz
528 * Document-method: singleton_method_added
530 * call-seq:
531 * singleton_method_added(symbol)
533 * Invoked as a callback whenever a singleton method is added to the
534 * receiver.
536 * module Chatty
537 * def Chatty.singleton_method_added(id)
538 * puts "Adding #{id.id2name}"
539 * end
540 * def self.one() end
541 * def two() end
542 * def Chatty.three() end
543 * end
545 * <em>produces:</em>
547 * Adding singleton_method_added
548 * Adding one
549 * Adding three
554 * Document-method: singleton_method_removed
556 * call-seq:
557 * singleton_method_removed(symbol)
559 * Invoked as a callback whenever a singleton method is removed from
560 * the receiver.
562 * module Chatty
563 * def Chatty.singleton_method_removed(id)
564 * puts "Removing #{id.id2name}"
565 * end
566 * def self.one() end
567 * def two() end
568 * def Chatty.three() end
569 * class <<self
570 * remove_method :three
571 * remove_method :one
572 * end
573 * end
575 * <em>produces:</em>
577 * Removing three
578 * Removing one
582 * Document-method: singleton_method_undefined
584 * call-seq:
585 * singleton_method_undefined(symbol)
587 * Invoked as a callback whenever a singleton method is undefined in
588 * the receiver.
590 * module Chatty
591 * def Chatty.singleton_method_undefined(id)
592 * puts "Undefining #{id.id2name}"
593 * end
594 * def Chatty.one() end
595 * class << self
596 * undef_method(:one)
597 * end
598 * end
600 * <em>produces:</em>
602 * Undefining one
607 * Document-method: included
609 * call-seq:
610 * included( othermod )
612 * Callback invoked whenever the receiver is included in another
613 * module or class. This should be used in preference to
614 * <tt>Module.append_features</tt> if your code wants to perform some
615 * action when a module is included in another.
617 * module A
618 * def A.included(mod)
619 * puts "#{self} included in #{mod}"
620 * end
621 * end
622 * module Enumerable
623 * include A
624 * end
629 * Not documented
632 static VALUE
633 rb_obj_dummy(void)
635 return Qnil;
639 * call-seq:
640 * obj.tainted? => true or false
642 * Returns <code>true</code> if the object is tainted.
645 VALUE
646 rb_obj_tainted(VALUE obj)
648 if (OBJ_TAINTED(obj))
649 return Qtrue;
650 return Qfalse;
654 * call-seq:
655 * obj.taint -> obj
657 * Marks <i>obj</i> as tainted---if the <code>$SAFE</code> level is
658 * set appropriately, many method calls which might alter the running
659 * programs environment will refuse to accept tainted strings.
662 VALUE
663 rb_obj_taint(VALUE obj)
665 rb_secure(4);
666 if (!OBJ_TAINTED(obj)) {
667 if (OBJ_FROZEN(obj)) {
668 rb_error_frozen("object");
670 OBJ_TAINT(obj);
672 return obj;
677 * call-seq:
678 * obj.untaint => obj
680 * Removes the taint from <i>obj</i>.
683 VALUE
684 rb_obj_untaint(VALUE obj)
686 rb_secure(3);
687 if (OBJ_TAINTED(obj)) {
688 if (OBJ_FROZEN(obj)) {
689 rb_error_frozen("object");
691 FL_UNSET(obj, FL_TAINT);
693 return obj;
696 void
697 rb_obj_infect(VALUE obj1, VALUE obj2)
699 OBJ_INFECT(obj1, obj2);
702 static st_table *immediate_frozen_tbl = 0;
705 * call-seq:
706 * obj.freeze => obj
708 * Prevents further modifications to <i>obj</i>. A
709 * <code>TypeError</code> will be raised if modification is attempted.
710 * There is no way to unfreeze a frozen object. See also
711 * <code>Object#frozen?</code>.
713 * a = [ "a", "b", "c" ]
714 * a.freeze
715 * a << "z"
717 * <em>produces:</em>
719 * prog.rb:3:in `<<': can't modify frozen array (TypeError)
720 * from prog.rb:3
723 VALUE
724 rb_obj_freeze(VALUE obj)
726 if (!OBJ_FROZEN(obj)) {
727 if (rb_safe_level() >= 4 && !OBJ_TAINTED(obj)) {
728 rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
730 OBJ_FREEZE(obj);
731 if (SPECIAL_CONST_P(obj)) {
732 if (!immediate_frozen_tbl) {
733 immediate_frozen_tbl = st_init_numtable();
735 st_insert(immediate_frozen_tbl, obj, (st_data_t)Qtrue);
738 return obj;
742 * call-seq:
743 * obj.frozen? => true or false
745 * Returns the freeze status of <i>obj</i>.
747 * a = [ "a", "b", "c" ]
748 * a.freeze #=> ["a", "b", "c"]
749 * a.frozen? #=> true
752 VALUE
753 rb_obj_frozen_p(VALUE obj)
755 if (OBJ_FROZEN(obj)) return Qtrue;
756 if (SPECIAL_CONST_P(obj)) {
757 if (!immediate_frozen_tbl) return Qfalse;
758 if (st_lookup(immediate_frozen_tbl, obj, 0)) return Qtrue;
760 return Qfalse;
765 * Document-class: NilClass
767 * The class of the singleton object <code>nil</code>.
771 * call-seq:
772 * nil.to_i => 0
774 * Always returns zero.
776 * nil.to_i #=> 0
780 static VALUE
781 nil_to_i(VALUE obj)
783 return INT2FIX(0);
787 * call-seq:
788 * nil.to_f => 0.0
790 * Always returns zero.
792 * nil.to_f #=> 0.0
795 static VALUE
796 nil_to_f(VALUE obj)
798 return DOUBLE2NUM(0.0);
802 * call-seq:
803 * nil.to_s => ""
805 * Always returns the empty string.
808 static VALUE
809 nil_to_s(VALUE obj)
811 return rb_usascii_str_new(0, 0);
815 * Document-method: to_a
817 * call-seq:
818 * nil.to_a => []
820 * Always returns an empty array.
822 * nil.to_a #=> []
825 static VALUE
826 nil_to_a(VALUE obj)
828 return rb_ary_new2(0);
832 * call-seq:
833 * nil.inspect => "nil"
835 * Always returns the string "nil".
838 static VALUE
839 nil_inspect(VALUE obj)
841 return rb_usascii_str_new2("nil");
844 /***********************************************************************
845 * Document-class: TrueClass
847 * The global value <code>true</code> is the only instance of class
848 * <code>TrueClass</code> and represents a logically true value in
849 * boolean expressions. The class provides operators allowing
850 * <code>true</code> to be used in logical expressions.
855 * call-seq:
856 * true.to_s => "true"
858 * The string representation of <code>true</code> is "true".
861 static VALUE
862 true_to_s(VALUE obj)
864 return rb_usascii_str_new2("true");
869 * call-seq:
870 * true & obj => true or false
872 * And---Returns <code>false</code> if <i>obj</i> is
873 * <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
876 static VALUE
877 true_and(VALUE obj, VALUE obj2)
879 return RTEST(obj2)?Qtrue:Qfalse;
883 * call-seq:
884 * true | obj => true
886 * Or---Returns <code>true</code>. As <i>anObject</i> is an argument to
887 * a method call, it is always evaluated; there is no short-circuit
888 * evaluation in this case.
890 * true | puts("or")
891 * true || puts("logical or")
893 * <em>produces:</em>
895 * or
898 static VALUE
899 true_or(VALUE obj, VALUE obj2)
901 return Qtrue;
906 * call-seq:
907 * true ^ obj => !obj
909 * Exclusive Or---Returns <code>true</code> if <i>obj</i> is
910 * <code>nil</code> or <code>false</code>, <code>false</code>
911 * otherwise.
914 static VALUE
915 true_xor(VALUE obj, VALUE obj2)
917 return RTEST(obj2)?Qfalse:Qtrue;
922 * Document-class: FalseClass
924 * The global value <code>false</code> is the only instance of class
925 * <code>FalseClass</code> and represents a logically false value in
926 * boolean expressions. The class provides operators allowing
927 * <code>false</code> to participate correctly in logical expressions.
932 * call-seq:
933 * false.to_s => "false"
935 * 'nuf said...
938 static VALUE
939 false_to_s(VALUE obj)
941 return rb_usascii_str_new2("false");
945 * call-seq:
946 * false & obj => false
947 * nil & obj => false
949 * And---Returns <code>false</code>. <i>obj</i> is always
950 * evaluated as it is the argument to a method call---there is no
951 * short-circuit evaluation in this case.
954 static VALUE
955 false_and(VALUE obj, VALUE obj2)
957 return Qfalse;
962 * call-seq:
963 * false | obj => true or false
964 * nil | obj => true or false
966 * Or---Returns <code>false</code> if <i>obj</i> is
967 * <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
970 static VALUE
971 false_or(VALUE obj, VALUE obj2)
973 return RTEST(obj2)?Qtrue:Qfalse;
979 * call-seq:
980 * false ^ obj => true or false
981 * nil ^ obj => true or false
983 * Exclusive Or---If <i>obj</i> is <code>nil</code> or
984 * <code>false</code>, returns <code>false</code>; otherwise, returns
985 * <code>true</code>.
989 static VALUE
990 false_xor(VALUE obj, VALUE obj2)
992 return RTEST(obj2)?Qtrue:Qfalse;
996 * call_seq:
997 * nil.nil? => true
999 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1002 static VALUE
1003 rb_true(VALUE obj)
1005 return Qtrue;
1009 * call_seq:
1010 * nil.nil? => true
1011 * <anything_else>.nil? => false
1013 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1017 static VALUE
1018 rb_false(VALUE obj)
1020 return Qfalse;
1025 * call-seq:
1026 * obj =~ other => nil
1028 * Pattern Match---Overridden by descendents (notably
1029 * <code>Regexp</code> and <code>String</code>) to provide meaningful
1030 * pattern-match semantics.
1033 static VALUE
1034 rb_obj_match(VALUE obj1, VALUE obj2)
1036 return Qnil;
1040 * call-seq:
1041 * obj !~ other => nil
1043 * Returns true if two objects does not match, using <i>=~</i> method.
1046 static VALUE
1047 rb_obj_not_match(VALUE obj1, VALUE obj2)
1049 VALUE result = rb_funcall(obj1, id_match, 1, obj2);
1050 return RTEST(result) ? Qfalse : Qtrue;
1054 /***********************************************************************
1056 * Document-class: Module
1058 * A <code>Module</code> is a collection of methods and constants. The
1059 * methods in a module may be instance methods or module methods.
1060 * Instance methods appear as methods in a class when the module is
1061 * included, module methods do not. Conversely, module methods may be
1062 * called without creating an encapsulating object, while instance
1063 * methods may not. (See <code>Module#module_function</code>)
1065 * In the descriptions that follow, the parameter <i>syml</i> refers
1066 * to a symbol, which is either a quoted string or a
1067 * <code>Symbol</code> (such as <code>:name</code>).
1069 * module Mod
1070 * include Math
1071 * CONST = 1
1072 * def meth
1073 * # ...
1074 * end
1075 * end
1076 * Mod.class #=> Module
1077 * Mod.constants #=> [:CONST, :PI, :E]
1078 * Mod.instance_methods #=> [:meth]
1083 * call-seq:
1084 * mod.to_s => string
1086 * Return a string representing this module or class. For basic
1087 * classes and modules, this is the name. For singletons, we
1088 * show information on the thing we're attached to as well.
1091 static VALUE
1092 rb_mod_to_s(VALUE klass)
1094 if (FL_TEST(klass, FL_SINGLETON)) {
1095 VALUE s = rb_usascii_str_new2("#<");
1096 VALUE v = rb_iv_get(klass, "__attached__");
1098 rb_str_cat2(s, "Class:");
1099 switch (TYPE(v)) {
1100 case T_CLASS: case T_MODULE:
1101 rb_str_append(s, rb_inspect(v));
1102 break;
1103 default:
1104 rb_str_append(s, rb_any_to_s(v));
1105 break;
1107 rb_str_cat2(s, ">");
1109 return s;
1111 return rb_str_dup(rb_class_name(klass));
1115 * call-seq:
1116 * mod.freeze
1118 * Prevents further modifications to <i>mod</i>.
1121 static VALUE
1122 rb_mod_freeze(VALUE mod)
1124 rb_class_name(mod);
1125 return rb_obj_freeze(mod);
1129 * call-seq:
1130 * mod === obj => true or false
1132 * Case Equality---Returns <code>true</code> if <i>anObject</i> is an
1133 * instance of <i>mod</i> or one of <i>mod</i>'s descendents. Of
1134 * limited use for modules, but can be used in <code>case</code>
1135 * statements to classify objects by class.
1138 static VALUE
1139 rb_mod_eqq(VALUE mod, VALUE arg)
1141 return rb_obj_is_kind_of(arg, mod);
1145 * call-seq:
1146 * mod <= other => true, false, or nil
1148 * Returns true if <i>mod</i> is a subclass of <i>other</i> or
1149 * is the same as <i>other</i>. Returns
1150 * <code>nil</code> if there's no relationship between the two.
1151 * (Think of the relationship in terms of the class definition:
1152 * "class A<B" implies "A<B").
1156 VALUE
1157 rb_class_inherited_p(VALUE mod, VALUE arg)
1159 VALUE start = mod;
1161 if (mod == arg) return Qtrue;
1162 switch (TYPE(arg)) {
1163 case T_MODULE:
1164 case T_CLASS:
1165 break;
1166 default:
1167 rb_raise(rb_eTypeError, "compared with non class/module");
1169 while (mod) {
1170 if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg))
1171 return Qtrue;
1172 mod = RCLASS_SUPER(mod);
1174 /* not mod < arg; check if mod > arg */
1175 while (arg) {
1176 if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start))
1177 return Qfalse;
1178 arg = RCLASS_SUPER(arg);
1180 return Qnil;
1184 * call-seq:
1185 * mod < other => true, false, or nil
1187 * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
1188 * <code>nil</code> if there's no relationship between the two.
1189 * (Think of the relationship in terms of the class definition:
1190 * "class A<B" implies "A<B").
1194 static VALUE
1195 rb_mod_lt(VALUE mod, VALUE arg)
1197 if (mod == arg) return Qfalse;
1198 return rb_class_inherited_p(mod, arg);
1203 * call-seq:
1204 * mod >= other => true, false, or nil
1206 * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
1207 * two modules are the same. Returns
1208 * <code>nil</code> if there's no relationship between the two.
1209 * (Think of the relationship in terms of the class definition:
1210 * "class A<B" implies "B>A").
1214 static VALUE
1215 rb_mod_ge(VALUE mod, VALUE arg)
1217 switch (TYPE(arg)) {
1218 case T_MODULE:
1219 case T_CLASS:
1220 break;
1221 default:
1222 rb_raise(rb_eTypeError, "compared with non class/module");
1225 return rb_class_inherited_p(arg, mod);
1229 * call-seq:
1230 * mod > other => true, false, or nil
1232 * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
1233 * <code>nil</code> if there's no relationship between the two.
1234 * (Think of the relationship in terms of the class definition:
1235 * "class A<B" implies "B>A").
1239 static VALUE
1240 rb_mod_gt(VALUE mod, VALUE arg)
1242 if (mod == arg) return Qfalse;
1243 return rb_mod_ge(mod, arg);
1247 * call-seq:
1248 * mod <=> other_mod => -1, 0, +1, or nil
1250 * Comparison---Returns -1 if <i>mod</i> includes <i>other_mod</i>, 0 if
1251 * <i>mod</i> is the same as <i>other_mod</i>, and +1 if <i>mod</i> is
1252 * included by <i>other_mod</i> or if <i>mod</i> has no relationship with
1253 * <i>other_mod</i>. Returns <code>nil</code> if <i>other_mod</i> is
1254 * not a module.
1257 static VALUE
1258 rb_mod_cmp(VALUE mod, VALUE arg)
1260 VALUE cmp;
1262 if (mod == arg) return INT2FIX(0);
1263 switch (TYPE(arg)) {
1264 case T_MODULE:
1265 case T_CLASS:
1266 break;
1267 default:
1268 return Qnil;
1271 cmp = rb_class_inherited_p(mod, arg);
1272 if (NIL_P(cmp)) return Qnil;
1273 if (cmp) {
1274 return INT2FIX(-1);
1276 return INT2FIX(1);
1279 static VALUE
1280 rb_module_s_alloc(VALUE klass)
1282 VALUE mod = rb_module_new();
1284 RBASIC(mod)->klass = klass;
1285 return mod;
1288 static VALUE
1289 rb_class_s_alloc(VALUE klass)
1291 return rb_class_boot(0);
1295 * call-seq:
1296 * Module.new => mod
1297 * Module.new {|mod| block } => mod
1299 * Creates a new anonymous module. If a block is given, it is passed
1300 * the module object, and the block is evaluated in the context of this
1301 * module using <code>module_eval</code>.
1303 * Fred = Module.new do
1304 * def meth1
1305 * "hello"
1306 * end
1307 * def meth2
1308 * "bye"
1309 * end
1310 * end
1311 * a = "my string"
1312 * a.extend(Fred) #=> "my string"
1313 * a.meth1 #=> "hello"
1314 * a.meth2 #=> "bye"
1317 static VALUE
1318 rb_mod_initialize(VALUE module)
1320 extern VALUE rb_mod_module_exec(int argc, VALUE *argv, VALUE mod);
1322 if (rb_block_given_p()) {
1323 rb_mod_module_exec(1, &module, module);
1325 return Qnil;
1329 * call-seq:
1330 * Class.new(super_class=Object) => a_class
1332 * Creates a new anonymous (unnamed) class with the given superclass
1333 * (or <code>Object</code> if no parameter is given). You can give a
1334 * class a name by assigning the class object to a constant.
1338 static VALUE
1339 rb_class_initialize(int argc, VALUE *argv, VALUE klass)
1341 VALUE super;
1343 if (RCLASS_SUPER(klass) != 0) {
1344 rb_raise(rb_eTypeError, "already initialized class");
1346 if (argc == 0) {
1347 super = rb_cObject;
1349 else {
1350 rb_scan_args(argc, argv, "01", &super);
1351 rb_check_inheritable(super);
1353 RCLASS_SUPER(klass) = super;
1354 rb_make_metaclass(klass, RBASIC(super)->klass);
1355 rb_class_inherited(super, klass);
1356 rb_mod_initialize(klass);
1358 return klass;
1362 * call-seq:
1363 * class.allocate() => obj
1365 * Allocates space for a new object of <i>class</i>'s class. The
1366 * returned object must be an instance of <i>class</i>.
1370 VALUE
1371 rb_obj_alloc(VALUE klass)
1373 VALUE obj;
1375 if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
1376 rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
1378 if (FL_TEST(klass, FL_SINGLETON)) {
1379 rb_raise(rb_eTypeError, "can't create instance of singleton class");
1381 obj = rb_funcall(klass, ID_ALLOCATOR, 0, 0);
1382 if (rb_obj_class(obj) != rb_class_real(klass)) {
1383 rb_raise(rb_eTypeError, "wrong instance allocation");
1385 return obj;
1388 static VALUE
1389 rb_class_allocate_instance(VALUE klass)
1391 NEWOBJ(obj, struct RObject);
1392 OBJSETUP(obj, klass, T_OBJECT);
1393 return (VALUE)obj;
1397 * call-seq:
1398 * class.new(args, ...) => obj
1400 * Calls <code>allocate</code> to create a new object of
1401 * <i>class</i>'s class, then invokes that object's
1402 * <code>initialize</code> method, passing it <i>args</i>.
1403 * This is the method that ends up getting called whenever
1404 * an object is constructed using .new.
1408 VALUE
1409 rb_class_new_instance(int argc, VALUE *argv, VALUE klass)
1411 VALUE obj;
1413 obj = rb_obj_alloc(klass);
1414 rb_obj_call_init(obj, argc, argv);
1416 return obj;
1420 * call-seq:
1421 * class.superclass -> a_super_class or nil
1423 * Returns the superclass of <i>class</i>, or <code>nil</code>.
1425 * File.superclass #=> IO
1426 * IO.superclass #=> Object
1427 * Object.superclass #=> BasicObject
1428 * BasicObject.superclass #=> nil
1432 static VALUE
1433 rb_class_superclass(VALUE klass)
1435 VALUE super = RCLASS_SUPER(klass);
1437 if (!super) {
1438 if (klass == rb_cBasicObject) return Qnil;
1439 rb_raise(rb_eTypeError, "uninitialized class");
1441 while (TYPE(super) == T_ICLASS) {
1442 super = RCLASS_SUPER(super);
1444 if (!super) {
1445 return Qnil;
1447 return super;
1451 * call-seq:
1452 * attr_reader(symbol, ...) => nil
1453 * attr(symbol, ...) => nil
1455 * Creates instance variables and corresponding methods that return the
1456 * value of each instance variable. Equivalent to calling
1457 * ``<code>attr</code><i>:name</i>'' on each name in turn.
1460 static VALUE
1461 rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
1463 int i;
1465 for (i=0; i<argc; i++) {
1466 rb_attr(klass, rb_to_id(argv[i]), Qtrue, Qfalse, Qtrue);
1468 return Qnil;
1471 VALUE
1472 rb_mod_attr(int argc, VALUE *argv, VALUE klass)
1474 if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
1475 rb_warning("optional boolean argument is obsoleted");
1476 rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), Qtrue);
1477 return Qnil;
1479 return rb_mod_attr_reader(argc, argv, klass);
1483 * call-seq:
1484 * attr_writer(symbol, ...) => nil
1486 * Creates an accessor method to allow assignment to the attribute
1487 * <i>aSymbol</i><code>.id2name</code>.
1490 static VALUE
1491 rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
1493 int i;
1495 for (i=0; i<argc; i++) {
1496 rb_attr(klass, rb_to_id(argv[i]), Qfalse, Qtrue, Qtrue);
1498 return Qnil;
1502 * call-seq:
1503 * attr_accessor(symbol, ...) => nil
1505 * Defines a named attribute for this module, where the name is
1506 * <i>symbol.</i><code>id2name</code>, creating an instance variable
1507 * (<code>@name</code>) and a corresponding access method to read it.
1508 * Also creates a method called <code>name=</code> to set the attribute.
1510 * module Mod
1511 * attr_accessor(:one, :two)
1512 * end
1513 * Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
1516 static VALUE
1517 rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
1519 int i;
1521 for (i=0; i<argc; i++) {
1522 rb_attr(klass, rb_to_id(argv[i]), Qtrue, Qtrue, Qtrue);
1524 return Qnil;
1528 * call-seq:
1529 * mod.const_get(sym, inherit=true) => obj
1531 * Returns the value of the named constant in <i>mod</i>.
1533 * Math.const_get(:PI) #=> 3.14159265358979
1535 * If the constant is not defined or is defined by the ancestors and
1536 * +inherit+ is false, +NameError+ will be raised.
1539 static VALUE
1540 rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
1542 VALUE name, recur;
1543 ID id;
1545 if (argc == 1) {
1546 name = argv[0];
1547 recur = Qtrue;
1549 else {
1550 rb_scan_args(argc, argv, "11", &name, &recur);
1552 id = rb_to_id(name);
1553 if (!rb_is_const_id(id)) {
1554 rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1556 return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
1560 * call-seq:
1561 * mod.const_set(sym, obj) => obj
1563 * Sets the named constant to the given object, returning that object.
1564 * Creates a new constant if no constant with the given name previously
1565 * existed.
1567 * Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
1568 * Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
1571 static VALUE
1572 rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
1574 ID id = rb_to_id(name);
1576 if (!rb_is_const_id(id)) {
1577 rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1579 rb_const_set(mod, id, value);
1580 return value;
1584 * call-seq:
1585 * mod.const_defined?(sym, inherit=true) => true or false
1587 * Returns <code>true</code> if a constant with the given name is
1588 * defined by <i>mod</i>, or its ancestors if +inherit+ is not false.
1590 * Math.const_defined? "PI" #=> true
1591 * IO.const_defined? "SYNC" #=> true
1592 * IO.const_defined? "SYNC", false #=> false
1595 static VALUE
1596 rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
1598 VALUE name, recur;
1599 ID id;
1601 if (argc == 1) {
1602 name = argv[0];
1603 recur = Qtrue;
1605 else {
1606 rb_scan_args(argc, argv, "11", &name, &recur);
1608 id = rb_to_id(name);
1609 if (!rb_is_const_id(id)) {
1610 rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1612 return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
1616 * call-seq:
1617 * obj.methods => array
1619 * Returns a list of the names of methods publicly accessible in
1620 * <i>obj</i>. This will include all the methods accessible in
1621 * <i>obj</i>'s ancestors.
1623 * class Klass
1624 * def kMethod()
1625 * end
1626 * end
1627 * k = Klass.new
1628 * k.methods[0..9] #=> ["kMethod", "freeze", "nil?", "is_a?",
1629 * # "class", "instance_variable_set",
1630 * # "methods", "extend", "__send__", "instance_eval"]
1631 * k.methods.length #=> 42
1634 static VALUE
1635 rb_obj_methods(int argc, VALUE *argv, VALUE obj)
1637 retry:
1638 if (argc == 0) {
1639 VALUE args[1];
1641 args[0] = Qtrue;
1642 return rb_class_instance_methods(1, args, CLASS_OF(obj));
1644 else {
1645 VALUE recur;
1647 rb_scan_args(argc, argv, "1", &recur);
1648 if (RTEST(recur)) {
1649 argc = 0;
1650 goto retry;
1652 return rb_obj_singleton_methods(argc, argv, obj);
1657 * call-seq:
1658 * obj.protected_methods(all=true) => array
1660 * Returns the list of protected methods accessible to <i>obj</i>. If
1661 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1662 * in the receiver will be listed.
1665 static VALUE
1666 rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
1668 if (argc == 0) { /* hack to stop warning */
1669 VALUE args[1];
1671 args[0] = Qtrue;
1672 return rb_class_protected_instance_methods(1, args, CLASS_OF(obj));
1674 return rb_class_protected_instance_methods(argc, argv, CLASS_OF(obj));
1678 * call-seq:
1679 * obj.private_methods(all=true) => array
1681 * Returns the list of private methods accessible to <i>obj</i>. If
1682 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1683 * in the receiver will be listed.
1686 static VALUE
1687 rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
1689 if (argc == 0) { /* hack to stop warning */
1690 VALUE args[1];
1692 args[0] = Qtrue;
1693 return rb_class_private_instance_methods(1, args, CLASS_OF(obj));
1695 return rb_class_private_instance_methods(argc, argv, CLASS_OF(obj));
1699 * call-seq:
1700 * obj.public_methods(all=true) => array
1702 * Returns the list of public methods accessible to <i>obj</i>. If
1703 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1704 * in the receiver will be listed.
1707 static VALUE
1708 rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
1710 if (argc == 0) { /* hack to stop warning */
1711 VALUE args[1];
1713 args[0] = Qtrue;
1714 return rb_class_public_instance_methods(1, args, CLASS_OF(obj));
1716 return rb_class_public_instance_methods(argc, argv, CLASS_OF(obj));
1720 * call-seq:
1721 * obj.instance_variable_get(symbol) => obj
1723 * Returns the value of the given instance variable, or nil if the
1724 * instance variable is not set. The <code>@</code> part of the
1725 * variable name should be included for regular instance
1726 * variables. Throws a <code>NameError</code> exception if the
1727 * supplied symbol is not valid as an instance variable name.
1729 * class Fred
1730 * def initialize(p1, p2)
1731 * @a, @b = p1, p2
1732 * end
1733 * end
1734 * fred = Fred.new('cat', 99)
1735 * fred.instance_variable_get(:@a) #=> "cat"
1736 * fred.instance_variable_get("@b") #=> 99
1739 static VALUE
1740 rb_obj_ivar_get(VALUE obj, VALUE iv)
1742 ID id = rb_to_id(iv);
1744 if (!rb_is_instance_id(id)) {
1745 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1747 return rb_ivar_get(obj, id);
1751 * call-seq:
1752 * obj.instance_variable_set(symbol, obj) => obj
1754 * Sets the instance variable names by <i>symbol</i> to
1755 * <i>object</i>, thereby frustrating the efforts of the class's
1756 * author to attempt to provide proper encapsulation. The variable
1757 * did not have to exist prior to this call.
1759 * class Fred
1760 * def initialize(p1, p2)
1761 * @a, @b = p1, p2
1762 * end
1763 * end
1764 * fred = Fred.new('cat', 99)
1765 * fred.instance_variable_set(:@a, 'dog') #=> "dog"
1766 * fred.instance_variable_set(:@c, 'cat') #=> "cat"
1767 * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
1770 static VALUE
1771 rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
1773 ID id = rb_to_id(iv);
1775 if (!rb_is_instance_id(id)) {
1776 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1778 return rb_ivar_set(obj, id, val);
1782 * call-seq:
1783 * obj.instance_variable_defined?(symbol) => true or false
1785 * Returns <code>true</code> if the given instance variable is
1786 * defined in <i>obj</i>.
1788 * class Fred
1789 * def initialize(p1, p2)
1790 * @a, @b = p1, p2
1791 * end
1792 * end
1793 * fred = Fred.new('cat', 99)
1794 * fred.instance_variable_defined?(:@a) #=> true
1795 * fred.instance_variable_defined?("@b") #=> true
1796 * fred.instance_variable_defined?("@c") #=> false
1799 static VALUE
1800 rb_obj_ivar_defined(VALUE obj, VALUE iv)
1802 ID id = rb_to_id(iv);
1804 if (!rb_is_instance_id(id)) {
1805 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1807 return rb_ivar_defined(obj, id);
1811 * call-seq:
1812 * mod.class_variable_get(symbol) => obj
1814 * Returns the value of the given class variable (or throws a
1815 * <code>NameError</code> exception). The <code>@@</code> part of the
1816 * variable name should be included for regular class variables
1818 * class Fred
1819 * @@foo = 99
1820 * end
1821 * Fred.class_variable_get(:@@foo) #=> 99
1824 static VALUE
1825 rb_mod_cvar_get(VALUE obj, VALUE iv)
1827 ID id = rb_to_id(iv);
1829 if (!rb_is_class_id(id)) {
1830 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
1832 return rb_cvar_get(obj, id);
1836 * call-seq:
1837 * obj.class_variable_set(symbol, obj) => obj
1839 * Sets the class variable names by <i>symbol</i> to
1840 * <i>object</i>.
1842 * class Fred
1843 * @@foo = 99
1844 * def foo
1845 * @@foo
1846 * end
1847 * end
1848 * Fred.class_variable_set(:@@foo, 101) #=> 101
1849 * Fred.new.foo #=> 101
1852 static VALUE
1853 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
1855 ID id = rb_to_id(iv);
1857 if (!rb_is_class_id(id)) {
1858 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
1860 rb_cvar_set(obj, id, val);
1861 return val;
1865 * call-seq:
1866 * obj.class_variable_defined?(symbol) => true or false
1868 * Returns <code>true</code> if the given class variable is defined
1869 * in <i>obj</i>.
1871 * class Fred
1872 * @@foo = 99
1873 * end
1874 * Fred.class_variable_defined?(:@@foo) #=> true
1875 * Fred.class_variable_defined?(:@@bar) #=> false
1878 static VALUE
1879 rb_mod_cvar_defined(VALUE obj, VALUE iv)
1881 ID id = rb_to_id(iv);
1883 if (!rb_is_class_id(id)) {
1884 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
1886 return rb_cvar_defined(obj, id);
1889 static VALUE
1890 convert_type(VALUE val, const char *tname, const char *method, int raise)
1892 ID m;
1894 m = rb_intern(method);
1895 if (!rb_respond_to(val, m)) {
1896 if (raise) {
1897 rb_raise(rb_eTypeError, "can't convert %s into %s",
1898 NIL_P(val) ? "nil" :
1899 val == Qtrue ? "true" :
1900 val == Qfalse ? "false" :
1901 rb_obj_classname(val),
1902 tname);
1904 else {
1905 return Qnil;
1908 return rb_funcall(val, m, 0);
1911 VALUE
1912 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
1914 VALUE v;
1916 if (TYPE(val) == type) return val;
1917 v = convert_type(val, tname, method, Qtrue);
1918 if (TYPE(v) != type) {
1919 char *cname = rb_obj_classname(val);
1920 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
1921 cname, tname, cname, method, rb_obj_classname(v));
1923 return v;
1926 VALUE
1927 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
1929 VALUE v;
1931 /* always convert T_DATA */
1932 if (TYPE(val) == type && type != T_DATA) return val;
1933 v = convert_type(val, tname, method, Qfalse);
1934 if (NIL_P(v)) return Qnil;
1935 if (TYPE(v) != type) {
1936 char *cname = rb_obj_classname(val);
1937 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
1938 cname, tname, cname, method, rb_obj_classname(v));
1940 return v;
1944 static VALUE
1945 rb_to_integer(VALUE val, const char *method)
1947 VALUE v;
1949 if (FIXNUM_P(val)) return val;
1950 v = convert_type(val, "Integer", method, Qtrue);
1951 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
1952 char *cname = rb_obj_classname(val);
1953 rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)",
1954 cname, cname, method, rb_obj_classname(v));
1956 return v;
1959 VALUE
1960 rb_check_to_integer(VALUE val, const char *method)
1962 VALUE v;
1964 if (FIXNUM_P(val)) return val;
1965 v = convert_type(val, "Integer", method, Qfalse);
1966 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
1967 return Qnil;
1969 return v;
1972 VALUE
1973 rb_to_int(VALUE val)
1975 return rb_to_integer(val, "to_int");
1978 VALUE
1979 rb_Integer(VALUE val)
1981 VALUE tmp;
1983 switch (TYPE(val)) {
1984 case T_FLOAT:
1985 if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX
1986 && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) {
1987 break;
1989 return rb_dbl2big(RFLOAT_VALUE(val));
1991 case T_FIXNUM:
1992 case T_BIGNUM:
1993 return val;
1995 case T_STRING:
1996 return rb_str_to_inum(val, 0, Qtrue);
1998 case T_NIL:
1999 rb_raise(rb_eTypeError, "can't convert nil into Integer");
2000 break;
2002 default:
2003 break;
2005 tmp = convert_type(val, "Integer", "to_int", Qfalse);
2006 if (NIL_P(tmp)) {
2007 return rb_to_integer(val, "to_i");
2009 return tmp;
2013 * call-seq:
2014 * Integer(arg) => integer
2016 * Converts <i>arg</i> to a <code>Fixnum</code> or <code>Bignum</code>.
2017 * Numeric types are converted directly (with floating point numbers
2018 * being truncated). If <i>arg</i> is a <code>String</code>, leading
2019 * radix indicators (<code>0</code>, <code>0b</code>, and
2020 * <code>0x</code>) are honored. Others are converted using
2021 * <code>to_int</code> and <code>to_i</code>. This behavior is
2022 * different from that of <code>String#to_i</code>.
2024 * Integer(123.999) #=> 123
2025 * Integer("0x1a") #=> 26
2026 * Integer(Time.new) #=> 1204973019
2029 static VALUE
2030 rb_f_integer(VALUE obj, VALUE arg)
2032 return rb_Integer(arg);
2035 double
2036 rb_cstr_to_dbl(const char *p, int badcheck)
2038 const char *q;
2039 char *end;
2040 double d;
2041 const char *ellipsis = "";
2042 int w;
2043 #define OutOfRange() (((w = end - p) > 20) ? (w = 20, ellipsis = "...") : (ellipsis = ""))
2045 if (!p) return 0.0;
2046 q = p;
2047 while (ISSPACE(*p)) p++;
2048 d = strtod(p, &end);
2049 if (errno == ERANGE) {
2050 OutOfRange();
2051 rb_warn("Float %.*s%s out of range", w, p, ellipsis);
2052 errno = 0;
2054 if (p == end) {
2055 if (badcheck) {
2056 bad:
2057 rb_invalid_str(q, "Float()");
2059 return d;
2061 if (*end) {
2062 char buf[DBL_DIG * 4 + 10];
2063 char *n = buf;
2064 char *e = buf + sizeof(buf) - 1;
2065 char prev = 0;
2067 while (p < end && n < e) prev = *n++ = *p++;
2068 while (*p) {
2069 if (*p == '_') {
2070 /* remove underscores between digits */
2071 if (badcheck) {
2072 if (n == buf || !ISDIGIT(prev)) goto bad;
2073 ++p;
2074 if (!ISDIGIT(*p)) goto bad;
2076 else {
2077 while (*++p == '_');
2078 continue;
2081 prev = *p++;
2082 if (n < e) *n++ = prev;
2084 *n = '\0';
2085 p = buf;
2086 d = strtod(p, &end);
2087 if (errno == ERANGE) {
2088 OutOfRange();
2089 rb_warn("Float %.*s%s out of range", w, p, ellipsis);
2090 errno = 0;
2092 if (badcheck) {
2093 if (!end || p == end) goto bad;
2094 while (*end && ISSPACE(*end)) end++;
2095 if (*end) goto bad;
2098 if (errno == ERANGE) {
2099 errno = 0;
2100 OutOfRange();
2101 rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
2103 return d;
2106 double
2107 rb_str_to_dbl(VALUE str, int badcheck)
2109 char *s;
2110 long len;
2112 StringValue(str);
2113 s = RSTRING_PTR(str);
2114 len = RSTRING_LEN(str);
2115 if (s) {
2116 if (s[len]) { /* no sentinel somehow */
2117 char *p = ALLOCA_N(char, len+1);
2119 MEMCPY(p, s, char, len);
2120 p[len] = '\0';
2121 s = p;
2123 if (badcheck && len != strlen(s)) {
2124 rb_raise(rb_eArgError, "string for Float contains null byte");
2127 return rb_cstr_to_dbl(s, badcheck);
2130 VALUE
2131 rb_Float(VALUE val)
2133 switch (TYPE(val)) {
2134 case T_FIXNUM:
2135 return DOUBLE2NUM((double)FIX2LONG(val));
2137 case T_FLOAT:
2138 return val;
2140 case T_BIGNUM:
2141 return DOUBLE2NUM(rb_big2dbl(val));
2143 case T_STRING:
2144 return DOUBLE2NUM(rb_str_to_dbl(val, Qtrue));
2146 case T_NIL:
2147 rb_raise(rb_eTypeError, "can't convert nil into Float");
2148 break;
2150 default:
2152 VALUE f = rb_convert_type(val, T_FLOAT, "Float", "to_f");
2153 if (isnan(RFLOAT_VALUE(f))) {
2154 rb_raise(rb_eArgError, "invalid value for Float()");
2156 return f;
2162 * call-seq:
2163 * Float(arg) => float
2165 * Returns <i>arg</i> converted to a float. Numeric types are converted
2166 * directly, the rest are converted using <i>arg</i>.to_f. As of Ruby
2167 * 1.8, converting <code>nil</code> generates a <code>TypeError</code>.
2169 * Float(1) #=> 1.0
2170 * Float("123.456") #=> 123.456
2173 static VALUE
2174 rb_f_float(VALUE obj, VALUE arg)
2176 return rb_Float(arg);
2179 double
2180 rb_num2dbl(VALUE val)
2182 switch (TYPE(val)) {
2183 case T_FLOAT:
2184 return RFLOAT_VALUE(val);
2186 case T_STRING:
2187 rb_raise(rb_eTypeError, "no implicit conversion to float from string");
2188 break;
2190 case T_NIL:
2191 rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
2192 break;
2194 default:
2195 break;
2198 return RFLOAT_VALUE(rb_Float(val));
2201 char*
2202 rb_str2cstr(VALUE str, long *len)
2204 StringValue(str);
2205 if (len) *len = RSTRING_LEN(str);
2206 else if (RTEST(ruby_verbose) && RSTRING_LEN(str) != strlen(RSTRING_PTR(str))) {
2207 rb_warn("string contains \\0 character");
2209 return RSTRING_PTR(str);
2212 VALUE
2213 rb_String(VALUE val)
2215 return rb_convert_type(val, T_STRING, "String", "to_s");
2220 * call-seq:
2221 * String(arg) => string
2223 * Converts <i>arg</i> to a <code>String</code> by calling its
2224 * <code>to_s</code> method.
2226 * String(self) #=> "main"
2227 * String(self.class) #=> "Object"
2228 * String(123456) #=> "123456"
2231 static VALUE
2232 rb_f_string(VALUE obj, VALUE arg)
2234 return rb_String(arg);
2237 VALUE
2238 rb_Array(VALUE val)
2240 VALUE tmp = rb_check_array_type(val);
2242 if (NIL_P(tmp)) {
2243 tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
2244 if (NIL_P(tmp)) {
2245 return rb_ary_new3(1, val);
2248 return tmp;
2252 * call-seq:
2253 * Array(arg) => array
2255 * Returns <i>arg</i> as an <code>Array</code>. First tries to call
2256 * <i>arg</i><code>.to_ary</code>, then <i>arg</i><code>.to_a</code>.
2258 * Array(1..5) #=> [1, 2, 3, 4, 5]
2261 static VALUE
2262 rb_f_array(VALUE obj, VALUE arg)
2264 return rb_Array(arg);
2267 static VALUE
2268 boot_defclass(const char *name, VALUE super)
2270 extern st_table *rb_class_tbl;
2271 VALUE obj = rb_class_boot(super);
2272 ID id = rb_intern(name);
2274 rb_name_class(obj, id);
2275 st_add_direct(rb_class_tbl, id, obj);
2276 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
2277 return obj;
2281 * Document-class: Class
2283 * Classes in Ruby are first-class objects---each is an instance of
2284 * class <code>Class</code>.
2286 * When a new class is created (typically using <code>class Name ...
2287 * end</code>), an object of type <code>Class</code> is created and
2288 * assigned to a global constant (<code>Name</code> in this case). When
2289 * <code>Name.new</code> is called to create a new object, the
2290 * <code>new</code> method in <code>Class</code> is run by default.
2291 * This can be demonstrated by overriding <code>new</code> in
2292 * <code>Class</code>:
2294 * class Class
2295 * alias oldNew new
2296 * def new(*args)
2297 * print "Creating a new ", self.name, "\n"
2298 * oldNew(*args)
2299 * end
2300 * end
2303 * class Name
2304 * end
2307 * n = Name.new
2309 * <em>produces:</em>
2311 * Creating a new Name
2313 * Classes, modules, and objects are interrelated. In the diagram
2314 * that follows, the vertical arrows represent inheritance, and the
2315 * parentheses meta-classes. All metaclasses are instances
2316 * of the class `Class'.
2318 * +-----------------+
2319 * | |
2320 * BasicObject-->(BasicObject) |
2321 * ^ ^ |
2322 * | | |
2323 * Object---->(Object) |
2324 * ^ ^ ^ ^ |
2325 * | | | | |
2326 * | | +-----+ +---------+ |
2327 * | | | | |
2328 * | +-----------+ | |
2329 * | | | | |
2330 * +------+ | Module--->(Module) |
2331 * | | ^ ^ |
2332 * OtherClass-->(OtherClass) | | |
2333 * | | |
2334 * Class---->(Class) |
2335 * ^ |
2336 * | |
2337 * +----------------+
2342 * <code>BasicObject</code> is the parent class of all classes in Ruby.
2343 * It's an explicit blank class. <code>Object</code>, the root of Ruby's
2344 * class hierarchy is a direct subclass of <code>BasicObject</code>. Its
2345 * methods are therefore available to all objects unless explicitly
2346 * overridden.
2348 * <code>Object</code> mixes in the <code>Kernel</code> module, making
2349 * the built-in kernel functions globally accessible. Although the
2350 * instance methods of <code>Object</code> are defined by the
2351 * <code>Kernel</code> module, we have chosen to document them here for
2352 * clarity.
2354 * In the descriptions of Object's methods, the parameter <i>symbol</i> refers
2355 * to a symbol, which is either a quoted string or a
2356 * <code>Symbol</code> (such as <code>:name</code>).
2359 void
2360 Init_Object(void)
2362 VALUE metaclass;
2364 rb_cBasicObject = boot_defclass("BasicObject", 0);
2365 rb_cObject = boot_defclass("Object", rb_cBasicObject);
2366 rb_cModule = boot_defclass("Module", rb_cObject);
2367 rb_cClass = boot_defclass("Class", rb_cModule);
2369 metaclass = rb_make_metaclass(rb_cBasicObject, rb_cClass);
2370 metaclass = rb_make_metaclass(rb_cObject, metaclass);
2371 metaclass = rb_make_metaclass(rb_cModule, metaclass);
2372 metaclass = rb_make_metaclass(rb_cClass, metaclass);
2374 rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0);
2375 rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
2376 rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
2377 rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
2378 rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
2379 rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);
2381 rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1);
2382 rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1);
2383 rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
2385 rb_mKernel = rb_define_module("Kernel");
2386 rb_include_module(rb_cObject, rb_mKernel);
2387 rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1);
2388 rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1);
2389 rb_define_private_method(rb_cModule, "extended", rb_obj_dummy, 1);
2390 rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
2391 rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
2392 rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
2394 rb_define_method(rb_mKernel, "nil?", rb_false, 0);
2395 rb_define_method(rb_mKernel, "===", rb_equal, 1);
2396 rb_define_method(rb_mKernel, "=~", rb_obj_match, 1);
2397 rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
2398 rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
2400 rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
2401 rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
2402 rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
2403 rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
2405 rb_define_method(rb_mKernel, "taint", rb_obj_taint, 0);
2406 rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
2407 rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
2408 rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
2409 rb_define_method(rb_mKernel, "frozen?", rb_obj_frozen_p, 0);
2411 rb_define_method(rb_mKernel, "to_s", rb_any_to_s, 0);
2412 rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
2413 rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1);
2414 rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
2415 rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1);
2416 rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1);
2417 rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1);
2418 rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
2419 rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
2420 rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
2421 rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
2422 rb_define_private_method(rb_mKernel, "remove_instance_variable",
2423 rb_obj_remove_instance_variable, 1); /* in variable.c */
2425 rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
2426 rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
2427 rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
2428 rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
2430 rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */
2431 rb_define_global_function("format", rb_f_sprintf, -1); /* in sprintf.c */
2433 rb_define_global_function("Integer", rb_f_integer, 1);
2434 rb_define_global_function("Float", rb_f_float, 1);
2436 rb_define_global_function("String", rb_f_string, 1);
2437 rb_define_global_function("Array", rb_f_array, 1);
2439 rb_cNilClass = rb_define_class("NilClass", rb_cObject);
2440 rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
2441 rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
2442 rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
2443 rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
2444 rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
2445 rb_define_method(rb_cNilClass, "&", false_and, 1);
2446 rb_define_method(rb_cNilClass, "|", false_or, 1);
2447 rb_define_method(rb_cNilClass, "^", false_xor, 1);
2449 rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
2450 rb_undef_alloc_func(rb_cNilClass);
2451 rb_undef_method(CLASS_OF(rb_cNilClass), "new");
2452 rb_define_global_const("NIL", Qnil);
2454 rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
2455 rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
2456 rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
2457 rb_define_method(rb_cModule, "<=>", rb_mod_cmp, 1);
2458 rb_define_method(rb_cModule, "<", rb_mod_lt, 1);
2459 rb_define_method(rb_cModule, "<=", rb_class_inherited_p, 1);
2460 rb_define_method(rb_cModule, ">", rb_mod_gt, 1);
2461 rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
2462 rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
2463 rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
2464 rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
2465 rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
2466 rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */
2467 rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
2469 rb_define_private_method(rb_cModule, "attr", rb_mod_attr, -1);
2470 rb_define_private_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
2471 rb_define_private_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
2472 rb_define_private_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
2474 rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
2475 rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
2476 rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
2477 rb_define_method(rb_cModule, "public_instance_methods",
2478 rb_class_public_instance_methods, -1); /* in class.c */
2479 rb_define_method(rb_cModule, "protected_instance_methods",
2480 rb_class_protected_instance_methods, -1); /* in class.c */
2481 rb_define_method(rb_cModule, "private_instance_methods",
2482 rb_class_private_instance_methods, -1); /* in class.c */
2484 rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
2485 rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
2486 rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
2487 rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
2488 rb_define_private_method(rb_cModule, "remove_const",
2489 rb_mod_remove_const, 1); /* in variable.c */
2490 rb_define_method(rb_cModule, "const_missing",
2491 rb_mod_const_missing, 1); /* in variable.c */
2492 rb_define_method(rb_cModule, "class_variables",
2493 rb_mod_class_variables, 0); /* in variable.c */
2494 rb_define_method(rb_cModule, "remove_class_variable",
2495 rb_mod_remove_cvar, 1); /* in variable.c */
2496 rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
2497 rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
2498 rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
2500 rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
2501 rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
2502 rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
2503 rb_define_method(rb_cClass, "initialize_copy", rb_class_init_copy, 1); /* in class.c */
2504 rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
2505 rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
2506 rb_undef_method(rb_cClass, "extend_object");
2507 rb_undef_method(rb_cClass, "append_features");
2509 rb_cData = rb_define_class("Data", rb_cObject);
2510 rb_undef_alloc_func(rb_cData);
2512 rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
2513 rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
2514 rb_define_method(rb_cTrueClass, "&", true_and, 1);
2515 rb_define_method(rb_cTrueClass, "|", true_or, 1);
2516 rb_define_method(rb_cTrueClass, "^", true_xor, 1);
2517 rb_undef_alloc_func(rb_cTrueClass);
2518 rb_undef_method(CLASS_OF(rb_cTrueClass), "new");
2519 rb_define_global_const("TRUE", Qtrue);
2521 rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
2522 rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
2523 rb_define_method(rb_cFalseClass, "&", false_and, 1);
2524 rb_define_method(rb_cFalseClass, "|", false_or, 1);
2525 rb_define_method(rb_cFalseClass, "^", false_xor, 1);
2526 rb_undef_alloc_func(rb_cFalseClass);
2527 rb_undef_method(CLASS_OF(rb_cFalseClass), "new");
2528 rb_define_global_const("FALSE", Qfalse);
2530 id_eq = rb_intern("==");
2531 id_eql = rb_intern("eql?");
2532 id_match = rb_intern("=~");
2533 id_inspect = rb_intern("inspect");
2534 id_init_copy = rb_intern("initialize_copy");