* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / object.c
blob3c95ef9515eaadbb0ac3aa67ee7e81fcdc8ad3ff
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|FL_UNTRUSTED);
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_TEST(clone, FL_UNTRUSTED)) & ~(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 const char *cname = rb_obj_classname(obj);
302 VALUE str;
304 str = rb_sprintf("#<%s:%p>", cname, (void*)obj);
305 OBJ_INFECT(str, obj);
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 const char *c = rb_obj_classname(obj);
392 str = rb_sprintf("-<%s:%p", c, (void*)obj);
393 return rb_exec_recursive(inspect_obj, obj, str);
396 return rb_funcall(obj, rb_intern("to_s"), 0, 0);
401 * call-seq:
402 * obj.instance_of?(class) => true or false
404 * Returns <code>true</code> if <i>obj</i> is an instance of the given
405 * class. See also <code>Object#kind_of?</code>.
408 VALUE
409 rb_obj_is_instance_of(VALUE obj, VALUE c)
411 switch (TYPE(c)) {
412 case T_MODULE:
413 case T_CLASS:
414 case T_ICLASS:
415 break;
416 default:
417 rb_raise(rb_eTypeError, "class or module required");
420 if (rb_obj_class(obj) == c) return Qtrue;
421 return Qfalse;
426 * call-seq:
427 * obj.is_a?(class) => true or false
428 * obj.kind_of?(class) => true or false
430 * Returns <code>true</code> if <i>class</i> is the class of
431 * <i>obj</i>, or if <i>class</i> is one of the superclasses of
432 * <i>obj</i> or modules included in <i>obj</i>.
434 * module M; end
435 * class A
436 * include M
437 * end
438 * class B < A; end
439 * class C < B; end
440 * b = B.new
441 * b.instance_of? A #=> false
442 * b.instance_of? B #=> true
443 * b.instance_of? C #=> false
444 * b.instance_of? M #=> false
445 * b.kind_of? A #=> true
446 * b.kind_of? B #=> true
447 * b.kind_of? C #=> false
448 * b.kind_of? M #=> true
451 VALUE
452 rb_obj_is_kind_of(VALUE obj, VALUE c)
454 VALUE cl = CLASS_OF(obj);
456 switch (TYPE(c)) {
457 case T_MODULE:
458 case T_CLASS:
459 case T_ICLASS:
460 break;
462 default:
463 rb_raise(rb_eTypeError, "class or module required");
466 while (cl) {
467 if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
468 return Qtrue;
469 cl = RCLASS_SUPER(cl);
471 return Qfalse;
476 * call-seq:
477 * obj.tap{|x|...} => obj
479 * Yields <code>x</code> to the block, and then returns <code>x</code>.
480 * The primary purpose of this method is to "tap into" a method chain,
481 * in order to perform operations on intermediate results within the chain.
483 * (1..10) .tap {|x| puts "original: #{x.inspect}"}
484 * .to_a .tap {|x| puts "array: #{x.inspect}"}
485 * .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"}
486 * .map { |x| x*x } .tap {|x| puts "squares: #{x.inspect}"}
490 VALUE
491 rb_obj_tap(VALUE obj)
493 rb_yield(obj);
494 return obj;
499 * Document-method: inherited
501 * call-seq:
502 * inherited(subclass)
504 * Callback invoked whenever a subclass of the current class is created.
506 * Example:
508 * class Foo
509 * def self.inherited(subclass)
510 * puts "New subclass: #{subclass}"
511 * end
512 * end
514 * class Bar < Foo
515 * end
517 * class Baz < Bar
518 * end
520 * produces:
522 * New subclass: Bar
523 * New subclass: Baz
527 * Document-method: singleton_method_added
529 * call-seq:
530 * singleton_method_added(symbol)
532 * Invoked as a callback whenever a singleton method is added to the
533 * receiver.
535 * module Chatty
536 * def Chatty.singleton_method_added(id)
537 * puts "Adding #{id.id2name}"
538 * end
539 * def self.one() end
540 * def two() end
541 * def Chatty.three() end
542 * end
544 * <em>produces:</em>
546 * Adding singleton_method_added
547 * Adding one
548 * Adding three
553 * Document-method: singleton_method_removed
555 * call-seq:
556 * singleton_method_removed(symbol)
558 * Invoked as a callback whenever a singleton method is removed from
559 * the receiver.
561 * module Chatty
562 * def Chatty.singleton_method_removed(id)
563 * puts "Removing #{id.id2name}"
564 * end
565 * def self.one() end
566 * def two() end
567 * def Chatty.three() end
568 * class <<self
569 * remove_method :three
570 * remove_method :one
571 * end
572 * end
574 * <em>produces:</em>
576 * Removing three
577 * Removing one
581 * Document-method: singleton_method_undefined
583 * call-seq:
584 * singleton_method_undefined(symbol)
586 * Invoked as a callback whenever a singleton method is undefined in
587 * the receiver.
589 * module Chatty
590 * def Chatty.singleton_method_undefined(id)
591 * puts "Undefining #{id.id2name}"
592 * end
593 * def Chatty.one() end
594 * class << self
595 * undef_method(:one)
596 * end
597 * end
599 * <em>produces:</em>
601 * Undefining one
606 * Document-method: included
608 * call-seq:
609 * included( othermod )
611 * Callback invoked whenever the receiver is included in another
612 * module or class. This should be used in preference to
613 * <tt>Module.append_features</tt> if your code wants to perform some
614 * action when a module is included in another.
616 * module A
617 * def A.included(mod)
618 * puts "#{self} included in #{mod}"
619 * end
620 * end
621 * module Enumerable
622 * include A
623 * end
628 * Not documented
631 static VALUE
632 rb_obj_dummy(void)
634 return Qnil;
638 * call-seq:
639 * obj.tainted? => true or false
641 * Returns <code>true</code> if the object is tainted.
644 VALUE
645 rb_obj_tainted(VALUE obj)
647 if (OBJ_TAINTED(obj))
648 return Qtrue;
649 return Qfalse;
653 * call-seq:
654 * obj.taint -> obj
656 * Marks <i>obj</i> as tainted---if the <code>$SAFE</code> level is
657 * set appropriately, many method calls which might alter the running
658 * programs environment will refuse to accept tainted strings.
661 VALUE
662 rb_obj_taint(VALUE obj)
664 rb_secure(4);
665 if (!OBJ_TAINTED(obj)) {
666 if (OBJ_FROZEN(obj)) {
667 rb_error_frozen("object");
669 OBJ_TAINT(obj);
671 return obj;
676 * call-seq:
677 * obj.untaint => obj
679 * Removes the taint from <i>obj</i>.
682 VALUE
683 rb_obj_untaint(VALUE obj)
685 rb_secure(3);
686 if (OBJ_TAINTED(obj)) {
687 if (OBJ_FROZEN(obj)) {
688 rb_error_frozen("object");
690 FL_UNSET(obj, FL_TAINT);
692 return obj;
696 * call-seq:
697 * obj.untrusted? => true or false
699 * Returns <code>true</code> if the object is untrusted.
702 VALUE
703 rb_obj_untrusted(VALUE obj)
705 if (OBJ_UNTRUSTED(obj))
706 return Qtrue;
707 return Qfalse;
711 * call-seq:
712 * obj.untrust -> obj
714 * Marks <i>obj</i> as untrusted.
717 VALUE
718 rb_obj_untrust(VALUE obj)
720 rb_secure(4);
721 if (!OBJ_UNTRUSTED(obj)) {
722 if (OBJ_FROZEN(obj)) {
723 rb_error_frozen("object");
725 OBJ_UNTRUST(obj);
727 return obj;
732 * call-seq:
733 * obj.trust => obj
735 * Removes the untrusted mark from <i>obj</i>.
738 VALUE
739 rb_obj_trust(VALUE obj)
741 rb_secure(3);
742 if (OBJ_UNTRUSTED(obj)) {
743 if (OBJ_FROZEN(obj)) {
744 rb_error_frozen("object");
746 FL_UNSET(obj, FL_UNTRUSTED);
748 return obj;
751 void
752 rb_obj_infect(VALUE obj1, VALUE obj2)
754 OBJ_INFECT(obj1, obj2);
757 static st_table *immediate_frozen_tbl = 0;
760 * call-seq:
761 * obj.freeze => obj
763 * Prevents further modifications to <i>obj</i>. A
764 * <code>TypeError</code> will be raised if modification is attempted.
765 * There is no way to unfreeze a frozen object. See also
766 * <code>Object#frozen?</code>.
768 * a = [ "a", "b", "c" ]
769 * a.freeze
770 * a << "z"
772 * <em>produces:</em>
774 * prog.rb:3:in `<<': can't modify frozen array (TypeError)
775 * from prog.rb:3
778 VALUE
779 rb_obj_freeze(VALUE obj)
781 if (!OBJ_FROZEN(obj)) {
782 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(obj)) {
783 rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
785 OBJ_FREEZE(obj);
786 if (SPECIAL_CONST_P(obj)) {
787 if (!immediate_frozen_tbl) {
788 immediate_frozen_tbl = st_init_numtable();
790 st_insert(immediate_frozen_tbl, obj, (st_data_t)Qtrue);
793 return obj;
797 * call-seq:
798 * obj.frozen? => true or false
800 * Returns the freeze status of <i>obj</i>.
802 * a = [ "a", "b", "c" ]
803 * a.freeze #=> ["a", "b", "c"]
804 * a.frozen? #=> true
807 VALUE
808 rb_obj_frozen_p(VALUE obj)
810 if (OBJ_FROZEN(obj)) return Qtrue;
811 if (SPECIAL_CONST_P(obj)) {
812 if (!immediate_frozen_tbl) return Qfalse;
813 if (st_lookup(immediate_frozen_tbl, obj, 0)) return Qtrue;
815 return Qfalse;
820 * Document-class: NilClass
822 * The class of the singleton object <code>nil</code>.
826 * call-seq:
827 * nil.to_i => 0
829 * Always returns zero.
831 * nil.to_i #=> 0
835 static VALUE
836 nil_to_i(VALUE obj)
838 return INT2FIX(0);
842 * call-seq:
843 * nil.to_f => 0.0
845 * Always returns zero.
847 * nil.to_f #=> 0.0
850 static VALUE
851 nil_to_f(VALUE obj)
853 return DOUBLE2NUM(0.0);
857 * call-seq:
858 * nil.to_s => ""
860 * Always returns the empty string.
863 static VALUE
864 nil_to_s(VALUE obj)
866 return rb_usascii_str_new(0, 0);
870 * Document-method: to_a
872 * call-seq:
873 * nil.to_a => []
875 * Always returns an empty array.
877 * nil.to_a #=> []
880 static VALUE
881 nil_to_a(VALUE obj)
883 return rb_ary_new2(0);
887 * call-seq:
888 * nil.inspect => "nil"
890 * Always returns the string "nil".
893 static VALUE
894 nil_inspect(VALUE obj)
896 return rb_usascii_str_new2("nil");
899 /***********************************************************************
900 * Document-class: TrueClass
902 * The global value <code>true</code> is the only instance of class
903 * <code>TrueClass</code> and represents a logically true value in
904 * boolean expressions. The class provides operators allowing
905 * <code>true</code> to be used in logical expressions.
910 * call-seq:
911 * true.to_s => "true"
913 * The string representation of <code>true</code> is "true".
916 static VALUE
917 true_to_s(VALUE obj)
919 return rb_usascii_str_new2("true");
924 * call-seq:
925 * true & obj => true or false
927 * And---Returns <code>false</code> if <i>obj</i> is
928 * <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
931 static VALUE
932 true_and(VALUE obj, VALUE obj2)
934 return RTEST(obj2)?Qtrue:Qfalse;
938 * call-seq:
939 * true | obj => true
941 * Or---Returns <code>true</code>. As <i>anObject</i> is an argument to
942 * a method call, it is always evaluated; there is no short-circuit
943 * evaluation in this case.
945 * true | puts("or")
946 * true || puts("logical or")
948 * <em>produces:</em>
950 * or
953 static VALUE
954 true_or(VALUE obj, VALUE obj2)
956 return Qtrue;
961 * call-seq:
962 * true ^ obj => !obj
964 * Exclusive Or---Returns <code>true</code> if <i>obj</i> is
965 * <code>nil</code> or <code>false</code>, <code>false</code>
966 * otherwise.
969 static VALUE
970 true_xor(VALUE obj, VALUE obj2)
972 return RTEST(obj2)?Qfalse:Qtrue;
977 * Document-class: FalseClass
979 * The global value <code>false</code> is the only instance of class
980 * <code>FalseClass</code> and represents a logically false value in
981 * boolean expressions. The class provides operators allowing
982 * <code>false</code> to participate correctly in logical expressions.
987 * call-seq:
988 * false.to_s => "false"
990 * 'nuf said...
993 static VALUE
994 false_to_s(VALUE obj)
996 return rb_usascii_str_new2("false");
1000 * call-seq:
1001 * false & obj => false
1002 * nil & obj => false
1004 * And---Returns <code>false</code>. <i>obj</i> is always
1005 * evaluated as it is the argument to a method call---there is no
1006 * short-circuit evaluation in this case.
1009 static VALUE
1010 false_and(VALUE obj, VALUE obj2)
1012 return Qfalse;
1017 * call-seq:
1018 * false | obj => true or false
1019 * nil | obj => true or false
1021 * Or---Returns <code>false</code> if <i>obj</i> is
1022 * <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
1025 static VALUE
1026 false_or(VALUE obj, VALUE obj2)
1028 return RTEST(obj2)?Qtrue:Qfalse;
1034 * call-seq:
1035 * false ^ obj => true or false
1036 * nil ^ obj => true or false
1038 * Exclusive Or---If <i>obj</i> is <code>nil</code> or
1039 * <code>false</code>, returns <code>false</code>; otherwise, returns
1040 * <code>true</code>.
1044 static VALUE
1045 false_xor(VALUE obj, VALUE obj2)
1047 return RTEST(obj2)?Qtrue:Qfalse;
1051 * call_seq:
1052 * nil.nil? => true
1054 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1057 static VALUE
1058 rb_true(VALUE obj)
1060 return Qtrue;
1064 * call_seq:
1065 * nil.nil? => true
1066 * <anything_else>.nil? => false
1068 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1072 static VALUE
1073 rb_false(VALUE obj)
1075 return Qfalse;
1080 * call-seq:
1081 * obj =~ other => nil
1083 * Pattern Match---Overridden by descendents (notably
1084 * <code>Regexp</code> and <code>String</code>) to provide meaningful
1085 * pattern-match semantics.
1088 static VALUE
1089 rb_obj_match(VALUE obj1, VALUE obj2)
1091 return Qnil;
1095 * call-seq:
1096 * obj !~ other => nil
1098 * Returns true if two objects does not match, using <i>=~</i> method.
1101 static VALUE
1102 rb_obj_not_match(VALUE obj1, VALUE obj2)
1104 VALUE result = rb_funcall(obj1, id_match, 1, obj2);
1105 return RTEST(result) ? Qfalse : Qtrue;
1109 /***********************************************************************
1111 * Document-class: Module
1113 * A <code>Module</code> is a collection of methods and constants. The
1114 * methods in a module may be instance methods or module methods.
1115 * Instance methods appear as methods in a class when the module is
1116 * included, module methods do not. Conversely, module methods may be
1117 * called without creating an encapsulating object, while instance
1118 * methods may not. (See <code>Module#module_function</code>)
1120 * In the descriptions that follow, the parameter <i>syml</i> refers
1121 * to a symbol, which is either a quoted string or a
1122 * <code>Symbol</code> (such as <code>:name</code>).
1124 * module Mod
1125 * include Math
1126 * CONST = 1
1127 * def meth
1128 * # ...
1129 * end
1130 * end
1131 * Mod.class #=> Module
1132 * Mod.constants #=> [:CONST, :PI, :E]
1133 * Mod.instance_methods #=> [:meth]
1138 * call-seq:
1139 * mod.to_s => string
1141 * Return a string representing this module or class. For basic
1142 * classes and modules, this is the name. For singletons, we
1143 * show information on the thing we're attached to as well.
1146 static VALUE
1147 rb_mod_to_s(VALUE klass)
1149 if (FL_TEST(klass, FL_SINGLETON)) {
1150 VALUE s = rb_usascii_str_new2("#<");
1151 VALUE v = rb_iv_get(klass, "__attached__");
1153 rb_str_cat2(s, "Class:");
1154 switch (TYPE(v)) {
1155 case T_CLASS: case T_MODULE:
1156 rb_str_append(s, rb_inspect(v));
1157 break;
1158 default:
1159 rb_str_append(s, rb_any_to_s(v));
1160 break;
1162 rb_str_cat2(s, ">");
1164 return s;
1166 return rb_str_dup(rb_class_name(klass));
1170 * call-seq:
1171 * mod.freeze
1173 * Prevents further modifications to <i>mod</i>.
1176 static VALUE
1177 rb_mod_freeze(VALUE mod)
1179 rb_class_name(mod);
1180 return rb_obj_freeze(mod);
1184 * call-seq:
1185 * mod === obj => true or false
1187 * Case Equality---Returns <code>true</code> if <i>anObject</i> is an
1188 * instance of <i>mod</i> or one of <i>mod</i>'s descendents. Of
1189 * limited use for modules, but can be used in <code>case</code>
1190 * statements to classify objects by class.
1193 static VALUE
1194 rb_mod_eqq(VALUE mod, VALUE arg)
1196 return rb_obj_is_kind_of(arg, mod);
1200 * call-seq:
1201 * mod <= other => true, false, or nil
1203 * Returns true if <i>mod</i> is a subclass of <i>other</i> or
1204 * is the same as <i>other</i>. Returns
1205 * <code>nil</code> if there's no relationship between the two.
1206 * (Think of the relationship in terms of the class definition:
1207 * "class A<B" implies "A<B").
1211 VALUE
1212 rb_class_inherited_p(VALUE mod, VALUE arg)
1214 VALUE start = mod;
1216 if (mod == arg) return Qtrue;
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");
1224 while (mod) {
1225 if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg))
1226 return Qtrue;
1227 mod = RCLASS_SUPER(mod);
1229 /* not mod < arg; check if mod > arg */
1230 while (arg) {
1231 if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start))
1232 return Qfalse;
1233 arg = RCLASS_SUPER(arg);
1235 return Qnil;
1239 * call-seq:
1240 * mod < other => true, false, or nil
1242 * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
1243 * <code>nil</code> if there's no relationship between the two.
1244 * (Think of the relationship in terms of the class definition:
1245 * "class A<B" implies "A<B").
1249 static VALUE
1250 rb_mod_lt(VALUE mod, VALUE arg)
1252 if (mod == arg) return Qfalse;
1253 return rb_class_inherited_p(mod, arg);
1258 * call-seq:
1259 * mod >= other => true, false, or nil
1261 * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
1262 * two modules are the same. Returns
1263 * <code>nil</code> if there's no relationship between the two.
1264 * (Think of the relationship in terms of the class definition:
1265 * "class A<B" implies "B>A").
1269 static VALUE
1270 rb_mod_ge(VALUE mod, VALUE arg)
1272 switch (TYPE(arg)) {
1273 case T_MODULE:
1274 case T_CLASS:
1275 break;
1276 default:
1277 rb_raise(rb_eTypeError, "compared with non class/module");
1280 return rb_class_inherited_p(arg, mod);
1284 * call-seq:
1285 * mod > other => true, false, or nil
1287 * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
1288 * <code>nil</code> if there's no relationship between the two.
1289 * (Think of the relationship in terms of the class definition:
1290 * "class A<B" implies "B>A").
1294 static VALUE
1295 rb_mod_gt(VALUE mod, VALUE arg)
1297 if (mod == arg) return Qfalse;
1298 return rb_mod_ge(mod, arg);
1302 * call-seq:
1303 * mod <=> other_mod => -1, 0, +1, or nil
1305 * Comparison---Returns -1 if <i>mod</i> includes <i>other_mod</i>, 0 if
1306 * <i>mod</i> is the same as <i>other_mod</i>, and +1 if <i>mod</i> is
1307 * included by <i>other_mod</i> or if <i>mod</i> has no relationship with
1308 * <i>other_mod</i>. Returns <code>nil</code> if <i>other_mod</i> is
1309 * not a module.
1312 static VALUE
1313 rb_mod_cmp(VALUE mod, VALUE arg)
1315 VALUE cmp;
1317 if (mod == arg) return INT2FIX(0);
1318 switch (TYPE(arg)) {
1319 case T_MODULE:
1320 case T_CLASS:
1321 break;
1322 default:
1323 return Qnil;
1326 cmp = rb_class_inherited_p(mod, arg);
1327 if (NIL_P(cmp)) return Qnil;
1328 if (cmp) {
1329 return INT2FIX(-1);
1331 return INT2FIX(1);
1334 static VALUE
1335 rb_module_s_alloc(VALUE klass)
1337 VALUE mod = rb_module_new();
1339 RBASIC(mod)->klass = klass;
1340 return mod;
1343 static VALUE
1344 rb_class_s_alloc(VALUE klass)
1346 return rb_class_boot(0);
1350 * call-seq:
1351 * Module.new => mod
1352 * Module.new {|mod| block } => mod
1354 * Creates a new anonymous module. If a block is given, it is passed
1355 * the module object, and the block is evaluated in the context of this
1356 * module using <code>module_eval</code>.
1358 * Fred = Module.new do
1359 * def meth1
1360 * "hello"
1361 * end
1362 * def meth2
1363 * "bye"
1364 * end
1365 * end
1366 * a = "my string"
1367 * a.extend(Fred) #=> "my string"
1368 * a.meth1 #=> "hello"
1369 * a.meth2 #=> "bye"
1372 static VALUE
1373 rb_mod_initialize(VALUE module)
1375 extern VALUE rb_mod_module_exec(int argc, VALUE *argv, VALUE mod);
1377 if (rb_block_given_p()) {
1378 rb_mod_module_exec(1, &module, module);
1380 return Qnil;
1384 * call-seq:
1385 * Class.new(super_class=Object) => a_class
1387 * Creates a new anonymous (unnamed) class with the given superclass
1388 * (or <code>Object</code> if no parameter is given). You can give a
1389 * class a name by assigning the class object to a constant.
1393 static VALUE
1394 rb_class_initialize(int argc, VALUE *argv, VALUE klass)
1396 VALUE super;
1398 if (RCLASS_SUPER(klass) != 0) {
1399 rb_raise(rb_eTypeError, "already initialized class");
1401 if (argc == 0) {
1402 super = rb_cObject;
1404 else {
1405 rb_scan_args(argc, argv, "01", &super);
1406 rb_check_inheritable(super);
1408 RCLASS_SUPER(klass) = super;
1409 rb_make_metaclass(klass, RBASIC(super)->klass);
1410 rb_class_inherited(super, klass);
1411 rb_mod_initialize(klass);
1413 return klass;
1417 * call-seq:
1418 * class.allocate() => obj
1420 * Allocates space for a new object of <i>class</i>'s class and does not
1421 * call initialize on the new instance. The returned object must be an
1422 * instance of <i>class</i>.
1424 * klass = Class.new do
1425 * def initialize(*args)
1426 * @initialized = true
1427 * end
1429 * def initialized?
1430 * @initialized || false
1431 * end
1432 * end
1434 * klass.allocate.initialized? #=> false
1438 VALUE
1439 rb_obj_alloc(VALUE klass)
1441 VALUE obj;
1443 if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
1444 rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
1446 if (FL_TEST(klass, FL_SINGLETON)) {
1447 rb_raise(rb_eTypeError, "can't create instance of singleton class");
1449 obj = rb_funcall(klass, ID_ALLOCATOR, 0, 0);
1450 if (rb_obj_class(obj) != rb_class_real(klass)) {
1451 rb_raise(rb_eTypeError, "wrong instance allocation");
1453 return obj;
1456 static VALUE
1457 rb_class_allocate_instance(VALUE klass)
1459 NEWOBJ(obj, struct RObject);
1460 OBJSETUP(obj, klass, T_OBJECT);
1461 return (VALUE)obj;
1465 * call-seq:
1466 * class.new(args, ...) => obj
1468 * Calls <code>allocate</code> to create a new object of
1469 * <i>class</i>'s class, then invokes that object's
1470 * <code>initialize</code> method, passing it <i>args</i>.
1471 * This is the method that ends up getting called whenever
1472 * an object is constructed using .new.
1476 VALUE
1477 rb_class_new_instance(int argc, VALUE *argv, VALUE klass)
1479 VALUE obj;
1481 obj = rb_obj_alloc(klass);
1482 rb_obj_call_init(obj, argc, argv);
1484 return obj;
1488 * call-seq:
1489 * class.superclass -> a_super_class or nil
1491 * Returns the superclass of <i>class</i>, or <code>nil</code>.
1493 * File.superclass #=> IO
1494 * IO.superclass #=> Object
1495 * Object.superclass #=> BasicObject
1496 * class Foo; end
1497 * class Bar < Foo; end
1498 * Bar.superclass #=> Foo
1500 * returns nil when the given class hasn't a parent class:
1502 * BasicObject.superclass #=> nil
1506 static VALUE
1507 rb_class_superclass(VALUE klass)
1509 VALUE super = RCLASS_SUPER(klass);
1511 if (!super) {
1512 if (klass == rb_cBasicObject) return Qnil;
1513 rb_raise(rb_eTypeError, "uninitialized class");
1515 while (TYPE(super) == T_ICLASS) {
1516 super = RCLASS_SUPER(super);
1518 if (!super) {
1519 return Qnil;
1521 return super;
1525 * call-seq:
1526 * attr_reader(symbol, ...) => nil
1527 * attr(symbol, ...) => nil
1529 * Creates instance variables and corresponding methods that return the
1530 * value of each instance variable. Equivalent to calling
1531 * ``<code>attr</code><i>:name</i>'' on each name in turn.
1534 static VALUE
1535 rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
1537 int i;
1539 for (i=0; i<argc; i++) {
1540 rb_attr(klass, rb_to_id(argv[i]), Qtrue, Qfalse, Qtrue);
1542 return Qnil;
1545 VALUE
1546 rb_mod_attr(int argc, VALUE *argv, VALUE klass)
1548 if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
1549 rb_warning("optional boolean argument is obsoleted");
1550 rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), Qtrue);
1551 return Qnil;
1553 return rb_mod_attr_reader(argc, argv, klass);
1557 * call-seq:
1558 * attr_writer(symbol, ...) => nil
1560 * Creates an accessor method to allow assignment to the attribute
1561 * <i>aSymbol</i><code>.id2name</code>.
1564 static VALUE
1565 rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
1567 int i;
1569 for (i=0; i<argc; i++) {
1570 rb_attr(klass, rb_to_id(argv[i]), Qfalse, Qtrue, Qtrue);
1572 return Qnil;
1576 * call-seq:
1577 * attr_accessor(symbol, ...) => nil
1579 * Defines a named attribute for this module, where the name is
1580 * <i>symbol.</i><code>id2name</code>, creating an instance variable
1581 * (<code>@name</code>) and a corresponding access method to read it.
1582 * Also creates a method called <code>name=</code> to set the attribute.
1584 * module Mod
1585 * attr_accessor(:one, :two)
1586 * end
1587 * Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
1590 static VALUE
1591 rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
1593 int i;
1595 for (i=0; i<argc; i++) {
1596 rb_attr(klass, rb_to_id(argv[i]), Qtrue, Qtrue, Qtrue);
1598 return Qnil;
1602 * call-seq:
1603 * mod.const_get(sym, inherit=true) => obj
1605 * Returns the value of the named constant in <i>mod</i>.
1607 * Math.const_get(:PI) #=> 3.14159265358979
1609 * If the constant is not defined or is defined by the ancestors and
1610 * +inherit+ is false, +NameError+ will be raised.
1613 static VALUE
1614 rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
1616 VALUE name, recur;
1617 ID id;
1619 if (argc == 1) {
1620 name = argv[0];
1621 recur = Qtrue;
1623 else {
1624 rb_scan_args(argc, argv, "11", &name, &recur);
1626 id = rb_to_id(name);
1627 if (!rb_is_const_id(id)) {
1628 rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1630 return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
1634 * call-seq:
1635 * mod.const_set(sym, obj) => obj
1637 * Sets the named constant to the given object, returning that object.
1638 * Creates a new constant if no constant with the given name previously
1639 * existed.
1641 * Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
1642 * Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
1645 static VALUE
1646 rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
1648 ID id = rb_to_id(name);
1650 if (!rb_is_const_id(id)) {
1651 rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1653 rb_const_set(mod, id, value);
1654 return value;
1658 * call-seq:
1659 * mod.const_defined?(sym, inherit=true) => true or false
1661 * Returns <code>true</code> if a constant with the given name is
1662 * defined by <i>mod</i>, or its ancestors if +inherit+ is not false.
1664 * Math.const_defined? "PI" #=> true
1665 * IO.const_defined? "SYNC" #=> true
1666 * IO.const_defined? "SYNC", false #=> false
1669 static VALUE
1670 rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
1672 VALUE name, recur;
1673 ID id;
1675 if (argc == 1) {
1676 name = argv[0];
1677 recur = Qtrue;
1679 else {
1680 rb_scan_args(argc, argv, "11", &name, &recur);
1682 id = rb_to_id(name);
1683 if (!rb_is_const_id(id)) {
1684 rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1686 return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
1690 * call-seq:
1691 * obj.methods => array
1693 * Returns a list of the names of methods publicly accessible in
1694 * <i>obj</i>. This will include all the methods accessible in
1695 * <i>obj</i>'s ancestors.
1697 * class Klass
1698 * def kMethod()
1699 * end
1700 * end
1701 * k = Klass.new
1702 * k.methods[0..9] #=> ["kMethod", "freeze", "nil?", "is_a?",
1703 * # "class", "instance_variable_set",
1704 * # "methods", "extend", "__send__", "instance_eval"]
1705 * k.methods.length #=> 42
1708 static VALUE
1709 rb_obj_methods(int argc, VALUE *argv, VALUE obj)
1711 retry:
1712 if (argc == 0) {
1713 VALUE args[1];
1715 args[0] = Qtrue;
1716 return rb_class_instance_methods(1, args, CLASS_OF(obj));
1718 else {
1719 VALUE recur;
1721 rb_scan_args(argc, argv, "1", &recur);
1722 if (RTEST(recur)) {
1723 argc = 0;
1724 goto retry;
1726 return rb_obj_singleton_methods(argc, argv, obj);
1731 * call-seq:
1732 * obj.protected_methods(all=true) => array
1734 * Returns the list of protected methods accessible to <i>obj</i>. If
1735 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1736 * in the receiver will be listed.
1739 static VALUE
1740 rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
1742 if (argc == 0) { /* hack to stop warning */
1743 VALUE args[1];
1745 args[0] = Qtrue;
1746 return rb_class_protected_instance_methods(1, args, CLASS_OF(obj));
1748 return rb_class_protected_instance_methods(argc, argv, CLASS_OF(obj));
1752 * call-seq:
1753 * obj.private_methods(all=true) => array
1755 * Returns the list of private methods accessible to <i>obj</i>. If
1756 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1757 * in the receiver will be listed.
1760 static VALUE
1761 rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
1763 if (argc == 0) { /* hack to stop warning */
1764 VALUE args[1];
1766 args[0] = Qtrue;
1767 return rb_class_private_instance_methods(1, args, CLASS_OF(obj));
1769 return rb_class_private_instance_methods(argc, argv, CLASS_OF(obj));
1773 * call-seq:
1774 * obj.public_methods(all=true) => array
1776 * Returns the list of public methods accessible to <i>obj</i>. If
1777 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1778 * in the receiver will be listed.
1781 static VALUE
1782 rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
1784 if (argc == 0) { /* hack to stop warning */
1785 VALUE args[1];
1787 args[0] = Qtrue;
1788 return rb_class_public_instance_methods(1, args, CLASS_OF(obj));
1790 return rb_class_public_instance_methods(argc, argv, CLASS_OF(obj));
1794 * call-seq:
1795 * obj.instance_variable_get(symbol) => obj
1797 * Returns the value of the given instance variable, or nil if the
1798 * instance variable is not set. The <code>@</code> part of the
1799 * variable name should be included for regular instance
1800 * variables. Throws a <code>NameError</code> exception if the
1801 * supplied symbol is not valid as an instance variable name.
1803 * class Fred
1804 * def initialize(p1, p2)
1805 * @a, @b = p1, p2
1806 * end
1807 * end
1808 * fred = Fred.new('cat', 99)
1809 * fred.instance_variable_get(:@a) #=> "cat"
1810 * fred.instance_variable_get("@b") #=> 99
1813 static VALUE
1814 rb_obj_ivar_get(VALUE obj, VALUE iv)
1816 ID id = rb_to_id(iv);
1818 if (!rb_is_instance_id(id)) {
1819 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1821 return rb_ivar_get(obj, id);
1825 * call-seq:
1826 * obj.instance_variable_set(symbol, obj) => obj
1828 * Sets the instance variable names by <i>symbol</i> to
1829 * <i>object</i>, thereby frustrating the efforts of the class's
1830 * author to attempt to provide proper encapsulation. The variable
1831 * did not have to exist prior to this call.
1833 * class Fred
1834 * def initialize(p1, p2)
1835 * @a, @b = p1, p2
1836 * end
1837 * end
1838 * fred = Fred.new('cat', 99)
1839 * fred.instance_variable_set(:@a, 'dog') #=> "dog"
1840 * fred.instance_variable_set(:@c, 'cat') #=> "cat"
1841 * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
1844 static VALUE
1845 rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
1847 ID id = rb_to_id(iv);
1849 if (!rb_is_instance_id(id)) {
1850 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1852 return rb_ivar_set(obj, id, val);
1856 * call-seq:
1857 * obj.instance_variable_defined?(symbol) => true or false
1859 * Returns <code>true</code> if the given instance variable is
1860 * defined in <i>obj</i>.
1862 * class Fred
1863 * def initialize(p1, p2)
1864 * @a, @b = p1, p2
1865 * end
1866 * end
1867 * fred = Fred.new('cat', 99)
1868 * fred.instance_variable_defined?(:@a) #=> true
1869 * fred.instance_variable_defined?("@b") #=> true
1870 * fred.instance_variable_defined?("@c") #=> false
1873 static VALUE
1874 rb_obj_ivar_defined(VALUE obj, VALUE iv)
1876 ID id = rb_to_id(iv);
1878 if (!rb_is_instance_id(id)) {
1879 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1881 return rb_ivar_defined(obj, id);
1885 * call-seq:
1886 * mod.class_variable_get(symbol) => obj
1888 * Returns the value of the given class variable (or throws a
1889 * <code>NameError</code> exception). The <code>@@</code> part of the
1890 * variable name should be included for regular class variables
1892 * class Fred
1893 * @@foo = 99
1894 * end
1895 * Fred.class_variable_get(:@@foo) #=> 99
1898 static VALUE
1899 rb_mod_cvar_get(VALUE obj, VALUE iv)
1901 ID id = rb_to_id(iv);
1903 if (!rb_is_class_id(id)) {
1904 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
1906 return rb_cvar_get(obj, id);
1910 * call-seq:
1911 * obj.class_variable_set(symbol, obj) => obj
1913 * Sets the class variable names by <i>symbol</i> to
1914 * <i>object</i>.
1916 * class Fred
1917 * @@foo = 99
1918 * def foo
1919 * @@foo
1920 * end
1921 * end
1922 * Fred.class_variable_set(:@@foo, 101) #=> 101
1923 * Fred.new.foo #=> 101
1926 static VALUE
1927 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
1929 ID id = rb_to_id(iv);
1931 if (!rb_is_class_id(id)) {
1932 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
1934 rb_cvar_set(obj, id, val);
1935 return val;
1939 * call-seq:
1940 * obj.class_variable_defined?(symbol) => true or false
1942 * Returns <code>true</code> if the given class variable is defined
1943 * in <i>obj</i>.
1945 * class Fred
1946 * @@foo = 99
1947 * end
1948 * Fred.class_variable_defined?(:@@foo) #=> true
1949 * Fred.class_variable_defined?(:@@bar) #=> false
1952 static VALUE
1953 rb_mod_cvar_defined(VALUE obj, VALUE iv)
1955 ID id = rb_to_id(iv);
1957 if (!rb_is_class_id(id)) {
1958 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
1960 return rb_cvar_defined(obj, id);
1963 static VALUE
1964 convert_type(VALUE val, const char *tname, const char *method, int raise)
1966 ID m;
1968 m = rb_intern(method);
1969 if (!rb_respond_to(val, m)) {
1970 if (raise) {
1971 rb_raise(rb_eTypeError, "can't convert %s into %s",
1972 NIL_P(val) ? "nil" :
1973 val == Qtrue ? "true" :
1974 val == Qfalse ? "false" :
1975 rb_obj_classname(val),
1976 tname);
1978 else {
1979 return Qnil;
1982 return rb_funcall(val, m, 0);
1985 VALUE
1986 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
1988 VALUE v;
1990 if (TYPE(val) == type) return val;
1991 v = convert_type(val, tname, method, Qtrue);
1992 if (TYPE(v) != type) {
1993 const char *cname = rb_obj_classname(val);
1994 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
1995 cname, tname, cname, method, rb_obj_classname(v));
1997 return v;
2000 VALUE
2001 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
2003 VALUE v;
2005 /* always convert T_DATA */
2006 if (TYPE(val) == type && type != T_DATA) return val;
2007 v = convert_type(val, tname, method, Qfalse);
2008 if (NIL_P(v)) return Qnil;
2009 if (TYPE(v) != type) {
2010 const char *cname = rb_obj_classname(val);
2011 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
2012 cname, tname, cname, method, rb_obj_classname(v));
2014 return v;
2018 static VALUE
2019 rb_to_integer(VALUE val, const char *method)
2021 VALUE v;
2023 if (FIXNUM_P(val)) return val;
2024 v = convert_type(val, "Integer", method, Qtrue);
2025 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
2026 const char *cname = rb_obj_classname(val);
2027 rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)",
2028 cname, cname, method, rb_obj_classname(v));
2030 return v;
2033 VALUE
2034 rb_check_to_integer(VALUE val, const char *method)
2036 VALUE v;
2038 if (FIXNUM_P(val)) return val;
2039 v = convert_type(val, "Integer", method, Qfalse);
2040 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
2041 return Qnil;
2043 return v;
2046 VALUE
2047 rb_to_int(VALUE val)
2049 return rb_to_integer(val, "to_int");
2052 VALUE
2053 rb_Integer(VALUE val)
2055 VALUE tmp;
2057 switch (TYPE(val)) {
2058 case T_FLOAT:
2059 if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX
2060 && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) {
2061 break;
2063 return rb_dbl2big(RFLOAT_VALUE(val));
2065 case T_FIXNUM:
2066 case T_BIGNUM:
2067 return val;
2069 case T_STRING:
2070 return rb_str_to_inum(val, 0, Qtrue);
2072 case T_NIL:
2073 rb_raise(rb_eTypeError, "can't convert nil into Integer");
2074 break;
2076 default:
2077 break;
2079 tmp = convert_type(val, "Integer", "to_int", Qfalse);
2080 if (NIL_P(tmp)) {
2081 return rb_to_integer(val, "to_i");
2083 return tmp;
2087 * call-seq:
2088 * Integer(arg) => integer
2090 * Converts <i>arg</i> to a <code>Fixnum</code> or <code>Bignum</code>.
2091 * Numeric types are converted directly (with floating point numbers
2092 * being truncated). If <i>arg</i> is a <code>String</code>, leading
2093 * radix indicators (<code>0</code>, <code>0b</code>, and
2094 * <code>0x</code>) are honored. Others are converted using
2095 * <code>to_int</code> and <code>to_i</code>. This behavior is
2096 * different from that of <code>String#to_i</code>.
2098 * Integer(123.999) #=> 123
2099 * Integer("0x1a") #=> 26
2100 * Integer(Time.new) #=> 1204973019
2103 static VALUE
2104 rb_f_integer(VALUE obj, VALUE arg)
2106 return rb_Integer(arg);
2109 double
2110 rb_cstr_to_dbl(const char *p, int badcheck)
2112 const char *q;
2113 char *end;
2114 double d;
2115 const char *ellipsis = "";
2116 int w;
2117 #define OutOfRange() (((w = end - p) > 20) ? (w = 20, ellipsis = "...") : (ellipsis = ""))
2119 if (!p) return 0.0;
2120 q = p;
2121 while (ISSPACE(*p)) p++;
2122 d = strtod(p, &end);
2123 if (errno == ERANGE) {
2124 OutOfRange();
2125 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
2126 errno = 0;
2128 if (p == end) {
2129 if (badcheck) {
2130 bad:
2131 rb_invalid_str(q, "Float()");
2133 return d;
2135 if (*end) {
2136 char buf[DBL_DIG * 4 + 10];
2137 char *n = buf;
2138 char *e = buf + sizeof(buf) - 1;
2139 char prev = 0;
2141 while (p < end && n < e) prev = *n++ = *p++;
2142 while (*p) {
2143 if (*p == '_') {
2144 /* remove underscores between digits */
2145 if (badcheck) {
2146 if (n == buf || !ISDIGIT(prev)) goto bad;
2147 ++p;
2148 if (!ISDIGIT(*p)) goto bad;
2150 else {
2151 while (*++p == '_');
2152 continue;
2155 prev = *p++;
2156 if (n < e) *n++ = prev;
2158 *n = '\0';
2159 p = buf;
2160 d = strtod(p, &end);
2161 if (errno == ERANGE) {
2162 OutOfRange();
2163 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
2164 errno = 0;
2166 if (badcheck) {
2167 if (!end || p == end) goto bad;
2168 while (*end && ISSPACE(*end)) end++;
2169 if (*end) goto bad;
2172 if (errno == ERANGE) {
2173 errno = 0;
2174 OutOfRange();
2175 rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
2177 return d;
2180 double
2181 rb_str_to_dbl(VALUE str, int badcheck)
2183 char *s;
2184 long len;
2186 StringValue(str);
2187 s = RSTRING_PTR(str);
2188 len = RSTRING_LEN(str);
2189 if (s) {
2190 if (s[len]) { /* no sentinel somehow */
2191 char *p = ALLOCA_N(char, len+1);
2193 MEMCPY(p, s, char, len);
2194 p[len] = '\0';
2195 s = p;
2197 if (badcheck && len != strlen(s)) {
2198 rb_raise(rb_eArgError, "string for Float contains null byte");
2201 return rb_cstr_to_dbl(s, badcheck);
2204 VALUE
2205 rb_Float(VALUE val)
2207 switch (TYPE(val)) {
2208 case T_FIXNUM:
2209 return DOUBLE2NUM((double)FIX2LONG(val));
2211 case T_FLOAT:
2212 return val;
2214 case T_BIGNUM:
2215 return DOUBLE2NUM(rb_big2dbl(val));
2217 case T_STRING:
2218 return DOUBLE2NUM(rb_str_to_dbl(val, Qtrue));
2220 case T_NIL:
2221 rb_raise(rb_eTypeError, "can't convert nil into Float");
2222 break;
2224 default:
2225 return rb_convert_type(val, T_FLOAT, "Float", "to_f");
2230 * call-seq:
2231 * Float(arg) => float
2233 * Returns <i>arg</i> converted to a float. Numeric types are converted
2234 * directly, the rest are converted using <i>arg</i>.to_f. As of Ruby
2235 * 1.8, converting <code>nil</code> generates a <code>TypeError</code>.
2237 * Float(1) #=> 1.0
2238 * Float("123.456") #=> 123.456
2241 static VALUE
2242 rb_f_float(VALUE obj, VALUE arg)
2244 return rb_Float(arg);
2247 double
2248 rb_num2dbl(VALUE val)
2250 switch (TYPE(val)) {
2251 case T_FLOAT:
2252 return RFLOAT_VALUE(val);
2254 case T_STRING:
2255 rb_raise(rb_eTypeError, "no implicit conversion to float from string");
2256 break;
2258 case T_NIL:
2259 rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
2260 break;
2262 default:
2263 break;
2266 return RFLOAT_VALUE(rb_Float(val));
2269 char*
2270 rb_str2cstr(VALUE str, long *len)
2272 StringValue(str);
2273 if (len) *len = RSTRING_LEN(str);
2274 else if (RTEST(ruby_verbose) && RSTRING_LEN(str) != strlen(RSTRING_PTR(str))) {
2275 rb_warn("string contains \\0 character");
2277 return RSTRING_PTR(str);
2280 VALUE
2281 rb_String(VALUE val)
2283 return rb_convert_type(val, T_STRING, "String", "to_s");
2288 * call-seq:
2289 * String(arg) => string
2291 * Converts <i>arg</i> to a <code>String</code> by calling its
2292 * <code>to_s</code> method.
2294 * String(self) #=> "main"
2295 * String(self.class) #=> "Object"
2296 * String(123456) #=> "123456"
2299 static VALUE
2300 rb_f_string(VALUE obj, VALUE arg)
2302 return rb_String(arg);
2305 VALUE
2306 rb_Array(VALUE val)
2308 VALUE tmp = rb_check_array_type(val);
2310 if (NIL_P(tmp)) {
2311 tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
2312 if (NIL_P(tmp)) {
2313 return rb_ary_new3(1, val);
2316 return tmp;
2320 * call-seq:
2321 * Array(arg) => array
2323 * Returns <i>arg</i> as an <code>Array</code>. First tries to call
2324 * <i>arg</i><code>.to_ary</code>, then <i>arg</i><code>.to_a</code>.
2326 * Array(1..5) #=> [1, 2, 3, 4, 5]
2329 static VALUE
2330 rb_f_array(VALUE obj, VALUE arg)
2332 return rb_Array(arg);
2335 static VALUE
2336 boot_defclass(const char *name, VALUE super)
2338 extern st_table *rb_class_tbl;
2339 VALUE obj = rb_class_boot(super);
2340 ID id = rb_intern(name);
2342 rb_name_class(obj, id);
2343 st_add_direct(rb_class_tbl, id, obj);
2344 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
2345 return obj;
2349 * Document-class: Class
2351 * Classes in Ruby are first-class objects---each is an instance of
2352 * class <code>Class</code>.
2354 * When a new class is created (typically using <code>class Name ...
2355 * end</code>), an object of type <code>Class</code> is created and
2356 * assigned to a global constant (<code>Name</code> in this case). When
2357 * <code>Name.new</code> is called to create a new object, the
2358 * <code>new</code> method in <code>Class</code> is run by default.
2359 * This can be demonstrated by overriding <code>new</code> in
2360 * <code>Class</code>:
2362 * class Class
2363 * alias oldNew new
2364 * def new(*args)
2365 * print "Creating a new ", self.name, "\n"
2366 * oldNew(*args)
2367 * end
2368 * end
2371 * class Name
2372 * end
2375 * n = Name.new
2377 * <em>produces:</em>
2379 * Creating a new Name
2381 * Classes, modules, and objects are interrelated. In the diagram
2382 * that follows, the vertical arrows represent inheritance, and the
2383 * parentheses meta-classes. All metaclasses are instances
2384 * of the class `Class'.
2386 * +-----------------+
2387 * | |
2388 * BasicObject-->(BasicObject) |
2389 * ^ ^ |
2390 * | | |
2391 * Object---->(Object) |
2392 * ^ ^ ^ ^ |
2393 * | | | | |
2394 * | | +-----+ +---------+ |
2395 * | | | | |
2396 * | +-----------+ | |
2397 * | | | | |
2398 * +------+ | Module--->(Module) |
2399 * | | ^ ^ |
2400 * OtherClass-->(OtherClass) | | |
2401 * | | |
2402 * Class---->(Class) |
2403 * ^ |
2404 * | |
2405 * +----------------+
2410 * <code>BasicObject</code> is the parent class of all classes in Ruby.
2411 * It's an explicit blank class. <code>Object</code>, the root of Ruby's
2412 * class hierarchy is a direct subclass of <code>BasicObject</code>. Its
2413 * methods are therefore available to all objects unless explicitly
2414 * overridden.
2416 * <code>Object</code> mixes in the <code>Kernel</code> module, making
2417 * the built-in kernel functions globally accessible. Although the
2418 * instance methods of <code>Object</code> are defined by the
2419 * <code>Kernel</code> module, we have chosen to document them here for
2420 * clarity.
2422 * In the descriptions of Object's methods, the parameter <i>symbol</i> refers
2423 * to a symbol, which is either a quoted string or a
2424 * <code>Symbol</code> (such as <code>:name</code>).
2427 void
2428 Init_Object(void)
2430 #undef rb_intern
2431 #define rb_intern(str) rb_intern_const(str)
2433 VALUE metaclass;
2435 rb_cBasicObject = boot_defclass("BasicObject", 0);
2436 rb_cObject = boot_defclass("Object", rb_cBasicObject);
2437 rb_cModule = boot_defclass("Module", rb_cObject);
2438 rb_cClass = boot_defclass("Class", rb_cModule);
2440 metaclass = rb_make_metaclass(rb_cBasicObject, rb_cClass);
2441 metaclass = rb_make_metaclass(rb_cObject, metaclass);
2442 metaclass = rb_make_metaclass(rb_cModule, metaclass);
2443 metaclass = rb_make_metaclass(rb_cClass, metaclass);
2445 rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0);
2446 rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
2447 rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
2448 rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
2449 rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
2450 rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);
2452 rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1);
2453 rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1);
2454 rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
2456 rb_mKernel = rb_define_module("Kernel");
2457 rb_include_module(rb_cObject, rb_mKernel);
2458 rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1);
2459 rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1);
2460 rb_define_private_method(rb_cModule, "extended", rb_obj_dummy, 1);
2461 rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
2462 rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
2463 rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
2465 rb_define_method(rb_mKernel, "nil?", rb_false, 0);
2466 rb_define_method(rb_mKernel, "===", rb_equal, 1);
2467 rb_define_method(rb_mKernel, "=~", rb_obj_match, 1);
2468 rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
2469 rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
2471 rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
2472 rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
2473 rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
2474 rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
2476 rb_define_method(rb_mKernel, "taint", rb_obj_taint, 0);
2477 rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
2478 rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
2479 rb_define_method(rb_mKernel, "untrust", rb_obj_untrust, 0);
2480 rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0);
2481 rb_define_method(rb_mKernel, "trust", rb_obj_trust, 0);
2482 rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
2483 rb_define_method(rb_mKernel, "frozen?", rb_obj_frozen_p, 0);
2485 rb_define_method(rb_mKernel, "to_s", rb_any_to_s, 0);
2486 rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
2487 rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1);
2488 rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
2489 rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1);
2490 rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1);
2491 rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1);
2492 rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
2493 rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
2494 rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
2495 rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
2496 rb_define_private_method(rb_mKernel, "remove_instance_variable",
2497 rb_obj_remove_instance_variable, 1); /* in variable.c */
2499 rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
2500 rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
2501 rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
2502 rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
2504 rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */
2505 rb_define_global_function("format", rb_f_sprintf, -1); /* in sprintf.c */
2507 rb_define_global_function("Integer", rb_f_integer, 1);
2508 rb_define_global_function("Float", rb_f_float, 1);
2510 rb_define_global_function("String", rb_f_string, 1);
2511 rb_define_global_function("Array", rb_f_array, 1);
2513 rb_cNilClass = rb_define_class("NilClass", rb_cObject);
2514 rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
2515 rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
2516 rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
2517 rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
2518 rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
2519 rb_define_method(rb_cNilClass, "&", false_and, 1);
2520 rb_define_method(rb_cNilClass, "|", false_or, 1);
2521 rb_define_method(rb_cNilClass, "^", false_xor, 1);
2523 rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
2524 rb_undef_alloc_func(rb_cNilClass);
2525 rb_undef_method(CLASS_OF(rb_cNilClass), "new");
2526 rb_define_global_const("NIL", Qnil);
2528 rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
2529 rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
2530 rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
2531 rb_define_method(rb_cModule, "<=>", rb_mod_cmp, 1);
2532 rb_define_method(rb_cModule, "<", rb_mod_lt, 1);
2533 rb_define_method(rb_cModule, "<=", rb_class_inherited_p, 1);
2534 rb_define_method(rb_cModule, ">", rb_mod_gt, 1);
2535 rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
2536 rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
2537 rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
2538 rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
2539 rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
2540 rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */
2541 rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
2543 rb_define_private_method(rb_cModule, "attr", rb_mod_attr, -1);
2544 rb_define_private_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
2545 rb_define_private_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
2546 rb_define_private_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
2548 rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
2549 rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
2550 rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
2551 rb_define_method(rb_cModule, "public_instance_methods",
2552 rb_class_public_instance_methods, -1); /* in class.c */
2553 rb_define_method(rb_cModule, "protected_instance_methods",
2554 rb_class_protected_instance_methods, -1); /* in class.c */
2555 rb_define_method(rb_cModule, "private_instance_methods",
2556 rb_class_private_instance_methods, -1); /* in class.c */
2558 rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
2559 rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
2560 rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
2561 rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
2562 rb_define_private_method(rb_cModule, "remove_const",
2563 rb_mod_remove_const, 1); /* in variable.c */
2564 rb_define_method(rb_cModule, "const_missing",
2565 rb_mod_const_missing, 1); /* in variable.c */
2566 rb_define_method(rb_cModule, "class_variables",
2567 rb_mod_class_variables, 0); /* in variable.c */
2568 rb_define_method(rb_cModule, "remove_class_variable",
2569 rb_mod_remove_cvar, 1); /* in variable.c */
2570 rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
2571 rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
2572 rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
2574 rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
2575 rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
2576 rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
2577 rb_define_method(rb_cClass, "initialize_copy", rb_class_init_copy, 1); /* in class.c */
2578 rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
2579 rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
2580 rb_undef_method(rb_cClass, "extend_object");
2581 rb_undef_method(rb_cClass, "append_features");
2583 rb_cData = rb_define_class("Data", rb_cObject);
2584 rb_undef_alloc_func(rb_cData);
2586 rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
2587 rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
2588 rb_define_method(rb_cTrueClass, "&", true_and, 1);
2589 rb_define_method(rb_cTrueClass, "|", true_or, 1);
2590 rb_define_method(rb_cTrueClass, "^", true_xor, 1);
2591 rb_undef_alloc_func(rb_cTrueClass);
2592 rb_undef_method(CLASS_OF(rb_cTrueClass), "new");
2593 rb_define_global_const("TRUE", Qtrue);
2595 rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
2596 rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
2597 rb_define_method(rb_cFalseClass, "&", false_and, 1);
2598 rb_define_method(rb_cFalseClass, "|", false_or, 1);
2599 rb_define_method(rb_cFalseClass, "^", false_xor, 1);
2600 rb_undef_alloc_func(rb_cFalseClass);
2601 rb_undef_method(CLASS_OF(rb_cFalseClass), "new");
2602 rb_define_global_const("FALSE", Qfalse);
2604 id_eq = rb_intern("==");
2605 id_eql = rb_intern("eql?");
2606 id_match = rb_intern("=~");
2607 id_inspect = rb_intern("inspect");
2608 id_init_copy = rb_intern("initialize_copy");