Ruby 2.0.0p247(2013-06-27revision41674)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

object.c
Go to the documentation of this file.
1 /**********************************************************************
2
3 object.c -
4
5 $Author: nagachika $
6 created at: Thu Jul 15 12:01:24 JST 1993
7
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
11
12 **********************************************************************/
13
14 #include "ruby/ruby.h"
15 #include "ruby/st.h"
16 #include "ruby/util.h"
17 #include "ruby/encoding.h"
18 #include <stdio.h>
19 #include <errno.h>
20 #include <ctype.h>
21 #include <math.h>
22 #include <float.h>
23 #include "constant.h"
24 #include "internal.h"
25 #include "probes.h"
26
27 VALUE rb_cBasicObject;
28 VALUE rb_mKernel;
29 VALUE rb_cObject;
30 VALUE rb_cModule;
31 VALUE rb_cClass;
32 VALUE rb_cData;
33
34 VALUE rb_cNilClass;
35 VALUE rb_cTrueClass;
36 VALUE rb_cFalseClass;
37
38 static ID id_eq, id_eql, id_match, id_inspect;
39 static ID id_init_copy, id_init_clone, id_init_dup;
40 static ID id_const_missing;
41
42 #define CLASS_OR_MODULE_P(obj) \
43 (!SPECIAL_CONST_P(obj) && \
44 (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
45
46 /*
47 * call-seq:
48 * obj === other -> true or false
49 *
50 * Case Equality -- For class Object, effectively the same as calling
51 * <code>#==</code>, but typically overridden by descendants to provide
52 * meaningful semantics in +case+ statements.
53 */
54
55 VALUE
56 rb_equal(VALUE obj1, VALUE obj2)
57 {
58 VALUE result;
59
60 if (obj1 == obj2) return Qtrue;
61 result = rb_funcall(obj1, id_eq, 1, obj2);
62 if (RTEST(result)) return Qtrue;
63 return Qfalse;
64 }
65
66 int
67 rb_eql(VALUE obj1, VALUE obj2)
68 {
69 return RTEST(rb_funcall(obj1, id_eql, 1, obj2));
70 }
71
72 /*
73 * call-seq:
74 * obj == other -> true or false
75 * obj.equal?(other) -> true or false
76 * obj.eql?(other) -> true or false
77 *
78 * Equality --- At the <code>Object</code> level, <code>==</code> returns
79 * <code>true</code> only if +obj+ and +other+ are the same object.
80 * Typically, this method is overridden in descendant classes to provide
81 * class-specific meaning.
82 *
83 * Unlike <code>==</code>, the <code>equal?</code> method should never be
84 * overridden by subclasses as it is used to determine object identity
85 * (that is, <code>a.equal?(b)</code> if and only if <code>a</code> is the
86 * same object as <code>b</code>):
87 *
88 * obj = "a"
89 * other = obj.dup
90 *
91 * a == other #=> true
92 * a.equal? other #=> false
93 * a.equal? a #=> true
94 *
95 * The <code>eql?</code> method returns <code>true</code> if +obj+ and
96 * +other+ refer to the same hash key. This is used by Hash to test members
97 * for equality. For objects of class <code>Object</code>, <code>eql?</code>
98 * is synonymous with <code>==</code>. Subclasses normally continue this
99 * tradition by aliasing <code>eql?</code> to their overridden <code>==</code>
100 * method, but there are exceptions. <code>Numeric</code> types, for
101 * example, perform type conversion across <code>==</code>, but not across
102 * <code>eql?</code>, so:
103 *
104 * 1 == 1.0 #=> true
105 * 1.eql? 1.0 #=> false
106 */
107
108 VALUE
109 rb_obj_equal(VALUE obj1, VALUE obj2)
110 {
111 if (obj1 == obj2) return Qtrue;
112 return Qfalse;
113 }
114
115 /*
116 * Generates a Fixnum hash value for this object. This function must have the
117 * property that <code>a.eql?(b)</code> implies <code>a.hash == b.hash</code>.
118 *
119 * The hash value is used along with #eql? by the Hash class to determine if
120 * two objects reference the same hash key. Any hash value that exceeds the
121 * capacity of a Fixnum will be truncated before being used.
122 *
123 * The hash value for an object may not be identical across invocations or
124 * implementations of ruby. If you need a stable identifier across ruby
125 * invocations and implementations you will need to generate one with a custom
126 * method.
127 */
128 VALUE
129 rb_obj_hash(VALUE obj)
130 {
131 VALUE oid = rb_obj_id(obj);
132 #if SIZEOF_LONG == SIZEOF_VOIDP
133 st_index_t index = NUM2LONG(oid);
134 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
135 st_index_t index = NUM2LL(oid);
136 #else
137 # error not supported
138 #endif
139 st_index_t h = rb_hash_end(rb_hash_start(index));
140 return LONG2FIX(h);
141 }
142
143 /*
144 * call-seq:
145 * !obj -> true or false
146 *
147 * Boolean negate.
148 */
149
150 VALUE
151 rb_obj_not(VALUE obj)
152 {
153 return RTEST(obj) ? Qfalse : Qtrue;
154 }
155
156 /*
157 * call-seq:
158 * obj != other -> true or false
159 *
160 * Returns true if two objects are not-equal, otherwise false.
161 */
162
163 VALUE
164 rb_obj_not_equal(VALUE obj1, VALUE obj2)
165 {
166 VALUE result = rb_funcall(obj1, id_eq, 1, obj2);
167 return RTEST(result) ? Qfalse : Qtrue;
168 }
169
170 VALUE
171 rb_class_real(VALUE cl)
172 {
173 if (cl == 0)
174 return 0;
175 while ((RBASIC(cl)->flags & FL_SINGLETON) || BUILTIN_TYPE(cl) == T_ICLASS) {
176 cl = RCLASS_SUPER(cl);
177 }
178 return cl;
179 }
180
181 /*
182 * call-seq:
183 * obj.class -> class
184 *
185 * Returns the class of <i>obj</i>. This method must always be
186 * called with an explicit receiver, as <code>class</code> is also a
187 * reserved word in Ruby.
188 *
189 * 1.class #=> Fixnum
190 * self.class #=> Object
191 */
192
193 VALUE
194 rb_obj_class(VALUE obj)
195 {
196 return rb_class_real(CLASS_OF(obj));
197 }
198
199 /*
200 * call-seq:
201 * obj.singleton_class -> class
202 *
203 * Returns the singleton class of <i>obj</i>. This method creates
204 * a new singleton class if <i>obj</i> does not have it.
205 *
206 * If <i>obj</i> is <code>nil</code>, <code>true</code>, or
207 * <code>false</code>, it returns NilClass, TrueClass, or FalseClass,
208 * respectively.
209 * If <i>obj</i> is a Fixnum or a Symbol, it raises a TypeError.
210 *
211 * Object.new.singleton_class #=> #<Class:#<Object:0xb7ce1e24>>
212 * String.singleton_class #=> #<Class:String>
213 * nil.singleton_class #=> NilClass
214 */
215
216 static VALUE
217 rb_obj_singleton_class(VALUE obj)
218 {
219 return rb_singleton_class(obj);
220 }
221
222 static void
223 init_copy(VALUE dest, VALUE obj)
224 {
225 if (OBJ_FROZEN(dest)) {
226 rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
227 }
228 RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
229 RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR|FL_TAINT|FL_UNTRUSTED);
230 rb_copy_generic_ivar(dest, obj);
231 rb_gc_copy_finalizer(dest, obj);
232 switch (TYPE(obj)) {
233 case T_OBJECT:
234 if (!(RBASIC(dest)->flags & ROBJECT_EMBED) && ROBJECT_IVPTR(dest)) {
235 xfree(ROBJECT_IVPTR(dest));
236 ROBJECT(dest)->as.heap.ivptr = 0;
237 ROBJECT(dest)->as.heap.numiv = 0;
238 ROBJECT(dest)->as.heap.iv_index_tbl = 0;
239 }
240 if (RBASIC(obj)->flags & ROBJECT_EMBED) {
241 MEMCPY(ROBJECT(dest)->as.ary, ROBJECT(obj)->as.ary, VALUE, ROBJECT_EMBED_LEN_MAX);
242 RBASIC(dest)->flags |= ROBJECT_EMBED;
243 }
244 else {
245 long len = ROBJECT(obj)->as.heap.numiv;
246 VALUE *ptr = ALLOC_N(VALUE, len);
247 MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
248 ROBJECT(dest)->as.heap.ivptr = ptr;
249 ROBJECT(dest)->as.heap.numiv = len;
250 ROBJECT(dest)->as.heap.iv_index_tbl = ROBJECT(obj)->as.heap.iv_index_tbl;
251 RBASIC(dest)->flags &= ~ROBJECT_EMBED;
252 }
253 break;
254 case T_CLASS:
255 case T_MODULE:
256 if (RCLASS_IV_TBL(dest)) {
257 st_free_table(RCLASS_IV_TBL(dest));
258 RCLASS_IV_TBL(dest) = 0;
259 }
260 if (RCLASS_CONST_TBL(dest)) {
261 rb_free_const_table(RCLASS_CONST_TBL(dest));
262 RCLASS_CONST_TBL(dest) = 0;
263 }
264 if (RCLASS_IV_TBL(obj)) {
265 RCLASS_IV_TBL(dest) = st_copy(RCLASS_IV_TBL(obj));
266 }
267 break;
268 }
269 }
270
271 /*
272 * call-seq:
273 * obj.clone -> an_object
274 *
275 * Produces a shallow copy of <i>obj</i>---the instance variables of
276 * <i>obj</i> are copied, but not the objects they reference. Copies
277 * the frozen and tainted state of <i>obj</i>. See also the discussion
278 * under <code>Object#dup</code>.
279 *
280 * class Klass
281 * attr_accessor :str
282 * end
283 * s1 = Klass.new #=> #<Klass:0x401b3a38>
284 * s1.str = "Hello" #=> "Hello"
285 * s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello">
286 * s2.str[1,4] = "i" #=> "i"
287 * s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
288 * s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
289 *
290 * This method may have class-specific behavior. If so, that
291 * behavior will be documented under the #+initialize_copy+ method of
292 * the class.
293 */
294
295 VALUE
296 rb_obj_clone(VALUE obj)
297 {
298 VALUE clone;
299 VALUE singleton;
300
301 if (rb_special_const_p(obj)) {
302 rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
303 }
304 clone = rb_obj_alloc(rb_obj_class(obj));
305 singleton = rb_singleton_class_clone_and_attach(obj, clone);
306 RBASIC(clone)->klass = singleton;
307 if (FL_TEST(singleton, FL_SINGLETON)) {
308 rb_singleton_class_attached(singleton, clone);
309 }
310 RBASIC(clone)->flags &= (FL_TAINT|FL_UNTRUSTED);
311 RBASIC(clone)->flags |= RBASIC(obj)->flags & ~(FL_FREEZE|FL_FINALIZE);
312 init_copy(clone, obj);
313 rb_funcall(clone, id_init_clone, 1, obj);
314 RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
315
316 return clone;
317 }
318
319 /*
320 * call-seq:
321 * obj.dup -> an_object
322 *
323 * Produces a shallow copy of <i>obj</i>---the instance variables of
324 * <i>obj</i> are copied, but not the objects they reference.
325 * <code>dup</code> copies the tainted state of <i>obj</i>. See also
326 * the discussion under <code>Object#clone</code>. In general,
327 * <code>clone</code> and <code>dup</code> may have different semantics
328 * in descendant classes. While <code>clone</code> is used to duplicate
329 * an object, including its internal state, <code>dup</code> typically
330 * uses the class of the descendant object to create the new instance.
331 *
332 * This method may have class-specific behavior. If so, that
333 * behavior will be documented under the #+initialize_copy+ method of
334 * the class.
335 */
336
337 VALUE
338 rb_obj_dup(VALUE obj)
339 {
340 VALUE dup;
341
342 if (rb_special_const_p(obj)) {
343 rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj));
344 }
345 dup = rb_obj_alloc(rb_obj_class(obj));
346 init_copy(dup, obj);
347 rb_funcall(dup, id_init_dup, 1, obj);
348
349 return dup;
350 }
351
352 /* :nodoc: */
353 VALUE
354 rb_obj_init_copy(VALUE obj, VALUE orig)
355 {
356 if (obj == orig) return obj;
357 rb_check_frozen(obj);
358 rb_check_trusted(obj);
359 if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
360 rb_raise(rb_eTypeError, "initialize_copy should take same class object");
361 }
362 return obj;
363 }
364
365 /* :nodoc: */
366 VALUE
367 rb_obj_init_dup_clone(VALUE obj, VALUE orig)
368 {
369 rb_funcall(obj, id_init_copy, 1, orig);
370 return obj;
371 }
372
373 /*
374 * call-seq:
375 * obj.to_s -> string
376 *
377 * Returns a string representing <i>obj</i>. The default
378 * <code>to_s</code> prints the object's class and an encoding of the
379 * object id. As a special case, the top-level object that is the
380 * initial execution context of Ruby programs returns ``main.''
381 */
382
383 VALUE
384 rb_any_to_s(VALUE obj)
385 {
386 VALUE str;
387 VALUE cname = rb_class_name(CLASS_OF(obj));
388
389 str = rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)obj);
390 OBJ_INFECT(str, obj);
391
392 return str;
393 }
394
395 /*
396 * If the default external encoding is ASCII compatible, the encoding of
397 * inspected result must be compatible with it.
398 * If the default external encoding is ASCII incomapatible,
399 * the result must be ASCII only.
400 */
401 VALUE
402 rb_inspect(VALUE obj)
403 {
404 VALUE str = rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
405 rb_encoding *ext = rb_default_external_encoding();
406 if (!rb_enc_asciicompat(ext)) {
407 if (!rb_enc_str_asciionly_p(str))
408 rb_raise(rb_eEncCompatError, "inspected result must be ASCII only if default external encoding is ASCII incompatible");
409 return str;
410 }
411 if (rb_enc_get(str) != ext && !rb_enc_str_asciionly_p(str))
412 rb_raise(rb_eEncCompatError, "inspected result must be ASCII only or use the same encoding with default external");
413 return str;
414 }
415
416 static int
417 inspect_i(st_data_t k, st_data_t v, st_data_t a)
418 {
419 ID id = (ID)k;
420 VALUE value = (VALUE)v;
421 VALUE str = (VALUE)a;
422 VALUE str2;
423 const char *ivname;
424
425 /* need not to show internal data */
426 if (CLASS_OF(value) == 0) return ST_CONTINUE;
427 if (!rb_is_instance_id(id)) return ST_CONTINUE;
428 if (RSTRING_PTR(str)[0] == '-') { /* first element */
429 RSTRING_PTR(str)[0] = '#';
430 rb_str_cat2(str, " ");
431 }
432 else {
433 rb_str_cat2(str, ", ");
434 }
435 ivname = rb_id2name(id);
436 rb_str_cat2(str, ivname);
437 rb_str_cat2(str, "=");
438 str2 = rb_inspect(value);
439 rb_str_append(str, str2);
440 OBJ_INFECT(str, str2);
441
442 return ST_CONTINUE;
443 }
444
445 static VALUE
446 inspect_obj(VALUE obj, VALUE str, int recur)
447 {
448 if (recur) {
449 rb_str_cat2(str, " ...");
450 }
451 else {
452 rb_ivar_foreach(obj, inspect_i, str);
453 }
454 rb_str_cat2(str, ">");
455 RSTRING_PTR(str)[0] = '#';
456 OBJ_INFECT(str, obj);
457
458 return str;
459 }
460
461 /*
462 * call-seq:
463 * obj.inspect -> string
464 *
465 * Returns a string containing a human-readable representation of <i>obj</i>.
466 * By default, show the class name and the list of the instance variables and
467 * their values (by calling #inspect on each of them).
468 * User defined classes should override this method to make better
469 * representation of <i>obj</i>. When overriding this method, it should
470 * return a string whose encoding is compatible with the default external
471 * encoding.
472 *
473 * [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
474 * Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
475 *
476 * class Foo
477 * end
478 * Foo.new.inspect #=> "#<Foo:0x0300c868>"
479 *
480 * class Bar
481 * def initialize
482 * @bar = 1
483 * end
484 * end
485 * Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
486 *
487 * class Baz
488 * def to_s
489 * "baz"
490 * end
491 * end
492 * Baz.new.inspect #=> "#<Baz:0x0300c868>"
493 */
494
495 static VALUE
496 rb_obj_inspect(VALUE obj)
497 {
498 if (rb_ivar_count(obj) > 0) {
499 VALUE str;
500 VALUE c = rb_class_name(CLASS_OF(obj));
501
502 str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void*)obj);
503 return rb_exec_recursive(inspect_obj, obj, str);
504 }
505 else {
506 return rb_any_to_s(obj);
507 }
508 }
509
510 static VALUE
511 class_or_module_required(VALUE c)
512 {
513 if (SPECIAL_CONST_P(c)) goto not_class;
514 switch (BUILTIN_TYPE(c)) {
515 case T_MODULE:
516 case T_CLASS:
517 case T_ICLASS:
518 break;
519
520 default:
521 not_class:
522 rb_raise(rb_eTypeError, "class or module required");
523 }
524 return c;
525 }
526
527 /*
528 * call-seq:
529 * obj.instance_of?(class) -> true or false
530 *
531 * Returns <code>true</code> if <i>obj</i> is an instance of the given
532 * class. See also <code>Object#kind_of?</code>.
533 *
534 * class A; end
535 * class B < A; end
536 * class C < B; end
537 *
538 * b = B.new
539 * b.instance_of? A #=> false
540 * b.instance_of? B #=> true
541 * b.instance_of? C #=> false
542 */
543
544 VALUE
545 rb_obj_is_instance_of(VALUE obj, VALUE c)
546 {
547 c = class_or_module_required(c);
548 if (rb_obj_class(obj) == c) return Qtrue;
549 return Qfalse;
550 }
551
552
553 /*
554 * call-seq:
555 * obj.is_a?(class) -> true or false
556 * obj.kind_of?(class) -> true or false
557 *
558 * Returns <code>true</code> if <i>class</i> is the class of
559 * <i>obj</i>, or if <i>class</i> is one of the superclasses of
560 * <i>obj</i> or modules included in <i>obj</i>.
561 *
562 * module M; end
563 * class A
564 * include M
565 * end
566 * class B < A; end
567 * class C < B; end
568 *
569 * b = B.new
570 * b.is_a? A #=> true
571 * b.is_a? B #=> true
572 * b.is_a? C #=> false
573 * b.is_a? M #=> true
574 *
575 * b.kind_of? A #=> true
576 * b.kind_of? B #=> true
577 * b.kind_of? C #=> false
578 * b.kind_of? M #=> true
579 */
580
581 VALUE
582 rb_obj_is_kind_of(VALUE obj, VALUE c)
583 {
584 VALUE cl = CLASS_OF(obj);
585
586 c = class_or_module_required(c);
587 c = RCLASS_ORIGIN(c);
588 while (cl) {
589 if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
590 return Qtrue;
591 cl = RCLASS_SUPER(cl);
592 }
593 return Qfalse;
594 }
595
596
597 /*
598 * call-seq:
599 * obj.tap{|x|...} -> obj
600 *
601 * Yields <code>x</code> to the block, and then returns <code>x</code>.
602 * The primary purpose of this method is to "tap into" a method chain,
603 * in order to perform operations on intermediate results within the chain.
604 *
605 * (1..10) .tap {|x| puts "original: #{x.inspect}"}
606 * .to_a .tap {|x| puts "array: #{x.inspect}"}
607 * .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"}
608 * .map { |x| x*x } .tap {|x| puts "squares: #{x.inspect}"}
609 *
610 */
611
612 VALUE
613 rb_obj_tap(VALUE obj)
614 {
615 rb_yield(obj);
616 return obj;
617 }
618
619
620 /*
621 * Document-method: inherited
622 *
623 * call-seq:
624 * inherited(subclass)
625 *
626 * Callback invoked whenever a subclass of the current class is created.
627 *
628 * Example:
629 *
630 * class Foo
631 * def self.inherited(subclass)
632 * puts "New subclass: #{subclass}"
633 * end
634 * end
635 *
636 * class Bar < Foo
637 * end
638 *
639 * class Baz < Bar
640 * end
641 *
642 * produces:
643 *
644 * New subclass: Bar
645 * New subclass: Baz
646 */
647
648 /* Document-method: method_added
649 *
650 * call-seq:
651 * method_added(method_name)
652 *
653 * Invoked as a callback whenever an instance method is added to the
654 * receiver.
655 *
656 * module Chatty
657 * def self.method_added(method_name)
658 * puts "Adding #{method_name.inspect}"
659 * end
660 * def self.some_class_method() end
661 * def some_instance_method() end
662 * end
663 *
664 * produces:
665 *
666 * Adding :some_instance_method
667 *
668 */
669
670 /* Document-method: method_removed
671 *
672 * call-seq:
673 * method_removed(method_name)
674 *
675 * Invoked as a callback whenever an instance method is removed from the
676 * receiver.
677 *
678 * module Chatty
679 * def self.method_removed(method_name)
680 * puts "Removing #{method_name.inspect}"
681 * end
682 * def self.some_class_method() end
683 * def some_instance_method() end
684 * class << self
685 * remove_method :some_class_method
686 * end
687 * remove_method :some_instance_method
688 * end
689 *
690 * produces:
691 *
692 * Removing :some_instance_method
693 *
694 */
695
696 /*
697 * Document-method: singleton_method_added
698 *
699 * call-seq:
700 * singleton_method_added(symbol)
701 *
702 * Invoked as a callback whenever a singleton method is added to the
703 * receiver.
704 *
705 * module Chatty
706 * def Chatty.singleton_method_added(id)
707 * puts "Adding #{id.id2name}"
708 * end
709 * def self.one() end
710 * def two() end
711 * def Chatty.three() end
712 * end
713 *
714 * <em>produces:</em>
715 *
716 * Adding singleton_method_added
717 * Adding one
718 * Adding three
719 *
720 */
721
722 /*
723 * Document-method: singleton_method_removed
724 *
725 * call-seq:
726 * singleton_method_removed(symbol)
727 *
728 * Invoked as a callback whenever a singleton method is removed from
729 * the receiver.
730 *
731 * module Chatty
732 * def Chatty.singleton_method_removed(id)
733 * puts "Removing #{id.id2name}"
734 * end
735 * def self.one() end
736 * def two() end
737 * def Chatty.three() end
738 * class << self
739 * remove_method :three
740 * remove_method :one
741 * end
742 * end
743 *
744 * <em>produces:</em>
745 *
746 * Removing three
747 * Removing one
748 */
749
750 /*
751 * Document-method: singleton_method_undefined
752 *
753 * call-seq:
754 * singleton_method_undefined(symbol)
755 *
756 * Invoked as a callback whenever a singleton method is undefined in
757 * the receiver.
758 *
759 * module Chatty
760 * def Chatty.singleton_method_undefined(id)
761 * puts "Undefining #{id.id2name}"
762 * end
763 * def Chatty.one() end
764 * class << self
765 * undef_method(:one)
766 * end
767 * end
768 *
769 * <em>produces:</em>
770 *
771 * Undefining one
772 */
773
774
775 /*
776 * Document-method: included
777 *
778 * call-seq:
779 * included( othermod )
780 *
781 * Callback invoked whenever the receiver is included in another
782 * module or class. This should be used in preference to
783 * <tt>Module.append_features</tt> if your code wants to perform some
784 * action when a module is included in another.
785 *
786 * module A
787 * def A.included(mod)
788 * puts "#{self} included in #{mod}"
789 * end
790 * end
791 * module Enumerable
792 * include A
793 * end
794 * # => prints "A included in Enumerable"
795 */
796
797 /*
798 * Document-method: prepended
799 *
800 * call-seq:
801 * prepended( othermod )
802 *
803 * The equivalent of <tt>included</tt>, but for prepended modules.
804 *
805 * module A
806 * def self.prepended(mod)
807 * puts "#{self} prepended to #{mod}"
808 * end
809 * end
810 * module Enumerable
811 * prepend A
812 * end
813 * # => prints "A prepended to Enumerable"
814 */
815
816 /*
817 * Document-method: initialize
818 *
819 * call-seq:
820 * BasicObject.new
821 *
822 * Returns a new BasicObject.
823 */
824
825 /*
826 * Not documented
827 */
828
829 static VALUE
830 rb_obj_dummy(void)
831 {
832 return Qnil;
833 }
834
835 /*
836 * call-seq:
837 * obj.tainted? -> true or false
838 *
839 * Returns <code>true</code> if the object is tainted.
840 */
841
842 VALUE
843 rb_obj_tainted(VALUE obj)
844 {
845 if (OBJ_TAINTED(obj))
846 return Qtrue;
847 return Qfalse;
848 }
849
850 /*
851 * call-seq:
852 * obj.taint -> obj
853 *
854 * Marks <i>obj</i> as tainted---if the <code>$SAFE</code> level is
855 * set appropriately, many method calls which might alter the running
856 * programs environment will refuse to accept tainted strings.
857 */
858
859 VALUE
860 rb_obj_taint(VALUE obj)
861 {
862 rb_secure(4);
863 if (!OBJ_TAINTED(obj)) {
864 rb_check_frozen(obj);
865 OBJ_TAINT(obj);
866 }
867 return obj;
868 }
869
870
871 /*
872 * call-seq:
873 * obj.untaint -> obj
874 *
875 * Removes the taint from <i>obj</i>.
876 */
877
878 VALUE
879 rb_obj_untaint(VALUE obj)
880 {
881 rb_secure(3);
882 if (OBJ_TAINTED(obj)) {
883 rb_check_frozen(obj);
884 FL_UNSET(obj, FL_TAINT);
885 }
886 return obj;
887 }
888
889 /*
890 * call-seq:
891 * obj.untrusted? -> true or false
892 *
893 * Returns <code>true</code> if the object is untrusted.
894 */
895
896 VALUE
897 rb_obj_untrusted(VALUE obj)
898 {
899 if (OBJ_UNTRUSTED(obj))
900 return Qtrue;
901 return Qfalse;
902 }
903
904 /*
905 * call-seq:
906 * obj.untrust -> obj
907 *
908 * Marks <i>obj</i> as untrusted.
909 */
910
911 VALUE
912 rb_obj_untrust(VALUE obj)
913 {
914 rb_secure(4);
915 if (!OBJ_UNTRUSTED(obj)) {
916 rb_check_frozen(obj);
917 OBJ_UNTRUST(obj);
918 }
919 return obj;
920 }
921
922
923 /*
924 * call-seq:
925 * obj.trust -> obj
926 *
927 * Removes the untrusted mark from <i>obj</i>.
928 */
929
930 VALUE
931 rb_obj_trust(VALUE obj)
932 {
933 rb_secure(3);
934 if (OBJ_UNTRUSTED(obj)) {
935 rb_check_frozen(obj);
936 FL_UNSET(obj, FL_UNTRUSTED);
937 }
938 return obj;
939 }
940
941 void
942 rb_obj_infect(VALUE obj1, VALUE obj2)
943 {
944 OBJ_INFECT(obj1, obj2);
945 }
946
947 static st_table *immediate_frozen_tbl = 0;
948
949 /*
950 * call-seq:
951 * obj.freeze -> obj
952 *
953 * Prevents further modifications to <i>obj</i>. A
954 * <code>RuntimeError</code> will be raised if modification is attempted.
955 * There is no way to unfreeze a frozen object. See also
956 * <code>Object#frozen?</code>.
957 *
958 * This method returns self.
959 *
960 * a = [ "a", "b", "c" ]
961 * a.freeze
962 * a << "z"
963 *
964 * <em>produces:</em>
965 *
966 * prog.rb:3:in `<<': can't modify frozen array (RuntimeError)
967 * from prog.rb:3
968 */
969
970 VALUE
971 rb_obj_freeze(VALUE obj)
972 {
973 if (!OBJ_FROZEN(obj)) {
974 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(obj)) {
975 rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
976 }
977 OBJ_FREEZE(obj);
978 if (SPECIAL_CONST_P(obj)) {
979 if (!immediate_frozen_tbl) {
980 immediate_frozen_tbl = st_init_numtable();
981 }
982 st_insert(immediate_frozen_tbl, obj, (st_data_t)Qtrue);
983 }
984 }
985 return obj;
986 }
987
988 /*
989 * call-seq:
990 * obj.frozen? -> true or false
991 *
992 * Returns the freeze status of <i>obj</i>.
993 *
994 * a = [ "a", "b", "c" ]
995 * a.freeze #=> ["a", "b", "c"]
996 * a.frozen? #=> true
997 */
998
999 VALUE
1000 rb_obj_frozen_p(VALUE obj)
1001 {
1002 if (OBJ_FROZEN(obj)) return Qtrue;
1003 if (SPECIAL_CONST_P(obj)) {
1004 if (!immediate_frozen_tbl) return Qfalse;
1005 if (st_lookup(immediate_frozen_tbl, obj, 0)) return Qtrue;
1006 }
1007 return Qfalse;
1008 }
1009
1010
1011 /*
1012 * Document-class: NilClass
1013 *
1014 * The class of the singleton object <code>nil</code>.
1015 */
1016
1017 /*
1018 * call-seq:
1019 * nil.to_i -> 0
1020 *
1021 * Always returns zero.
1022 *
1023 * nil.to_i #=> 0
1024 */
1025
1026
1027 static VALUE
1028 nil_to_i(VALUE obj)
1029 {
1030 return INT2FIX(0);
1031 }
1032
1033 /*
1034 * call-seq:
1035 * nil.to_f -> 0.0
1036 *
1037 * Always returns zero.
1038 *
1039 * nil.to_f #=> 0.0
1040 */
1041
1042 static VALUE
1043 nil_to_f(VALUE obj)
1044 {
1045 return DBL2NUM(0.0);
1046 }
1047
1048 /*
1049 * call-seq:
1050 * nil.to_s -> ""
1051 *
1052 * Always returns the empty string.
1053 */
1054
1055 static VALUE
1056 nil_to_s(VALUE obj)
1057 {
1058 return rb_usascii_str_new(0, 0);
1059 }
1060
1061 /*
1062 * Document-method: to_a
1063 *
1064 * call-seq:
1065 * nil.to_a -> []
1066 *
1067 * Always returns an empty array.
1068 *
1069 * nil.to_a #=> []
1070 */
1071
1072 static VALUE
1073 nil_to_a(VALUE obj)
1074 {
1075 return rb_ary_new2(0);
1076 }
1077
1078 /*
1079 * Document-method: to_h
1080 *
1081 * call-seq:
1082 * nil.to_h -> {}
1083 *
1084 * Always returns an empty hash.
1085 *
1086 * nil.to_h #=> {}
1087 */
1088
1089 static VALUE
1090 nil_to_h(VALUE obj)
1091 {
1092 return rb_hash_new();
1093 }
1094
1095 /*
1096 * call-seq:
1097 * nil.inspect -> "nil"
1098 *
1099 * Always returns the string "nil".
1100 */
1101
1102 static VALUE
1103 nil_inspect(VALUE obj)
1104 {
1105 return rb_usascii_str_new2("nil");
1106 }
1107
1108 /***********************************************************************
1109 * Document-class: TrueClass
1110 *
1111 * The global value <code>true</code> is the only instance of class
1112 * <code>TrueClass</code> and represents a logically true value in
1113 * boolean expressions. The class provides operators allowing
1114 * <code>true</code> to be used in logical expressions.
1115 */
1116
1117
1118 /*
1119 * call-seq:
1120 * true.to_s -> "true"
1121 *
1122 * The string representation of <code>true</code> is "true".
1123 */
1124
1125 static VALUE
1126 true_to_s(VALUE obj)
1127 {
1128 return rb_usascii_str_new2("true");
1129 }
1130
1131
1132 /*
1133 * call-seq:
1134 * true & obj -> true or false
1135 *
1136 * And---Returns <code>false</code> if <i>obj</i> is
1137 * <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
1138 */
1139
1140 static VALUE
1141 true_and(VALUE obj, VALUE obj2)
1142 {
1143 return RTEST(obj2)?Qtrue:Qfalse;
1144 }
1145
1146 /*
1147 * call-seq:
1148 * true | obj -> true
1149 *
1150 * Or---Returns <code>true</code>. As <i>anObject</i> is an argument to
1151 * a method call, it is always evaluated; there is no short-circuit
1152 * evaluation in this case.
1153 *
1154 * true | puts("or")
1155 * true || puts("logical or")
1156 *
1157 * <em>produces:</em>
1158 *
1159 * or
1160 */
1161
1162 static VALUE
1163 true_or(VALUE obj, VALUE obj2)
1164 {
1165 return Qtrue;
1166 }
1167
1168
1169 /*
1170 * call-seq:
1171 * true ^ obj -> !obj
1172 *
1173 * Exclusive Or---Returns <code>true</code> if <i>obj</i> is
1174 * <code>nil</code> or <code>false</code>, <code>false</code>
1175 * otherwise.
1176 */
1177
1178 static VALUE
1179 true_xor(VALUE obj, VALUE obj2)
1180 {
1181 return RTEST(obj2)?Qfalse:Qtrue;
1182 }
1183
1184
1185 /*
1186 * Document-class: FalseClass
1187 *
1188 * The global value <code>false</code> is the only instance of class
1189 * <code>FalseClass</code> and represents a logically false value in
1190 * boolean expressions. The class provides operators allowing
1191 * <code>false</code> to participate correctly in logical expressions.
1192 *
1193 */
1194
1195 /*
1196 * call-seq:
1197 * false.to_s -> "false"
1198 *
1199 * 'nuf said...
1200 */
1201
1202 static VALUE
1203 false_to_s(VALUE obj)
1204 {
1205 return rb_usascii_str_new2("false");
1206 }
1207
1208 /*
1209 * call-seq:
1210 * false & obj -> false
1211 * nil & obj -> false
1212 *
1213 * And---Returns <code>false</code>. <i>obj</i> is always
1214 * evaluated as it is the argument to a method call---there is no
1215 * short-circuit evaluation in this case.
1216 */
1217
1218 static VALUE
1219 false_and(VALUE obj, VALUE obj2)
1220 {
1221 return Qfalse;
1222 }
1223
1224
1225 /*
1226 * call-seq:
1227 * false | obj -> true or false
1228 * nil | obj -> true or false
1229 *
1230 * Or---Returns <code>false</code> if <i>obj</i> is
1231 * <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
1232 */
1233
1234 static VALUE
1235 false_or(VALUE obj, VALUE obj2)
1236 {
1237 return RTEST(obj2)?Qtrue:Qfalse;
1238 }
1239
1240
1241
1242 /*
1243 * call-seq:
1244 * false ^ obj -> true or false
1245 * nil ^ obj -> true or false
1246 *
1247 * Exclusive Or---If <i>obj</i> is <code>nil</code> or
1248 * <code>false</code>, returns <code>false</code>; otherwise, returns
1249 * <code>true</code>.
1250 *
1251 */
1252
1253 static VALUE
1254 false_xor(VALUE obj, VALUE obj2)
1255 {
1256 return RTEST(obj2)?Qtrue:Qfalse;
1257 }
1258
1259 /*
1260 * call_seq:
1261 * nil.nil? -> true
1262 *
1263 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1264 */
1265
1266 static VALUE
1267 rb_true(VALUE obj)
1268 {
1269 return Qtrue;
1270 }
1271
1272 /*
1273 * call_seq:
1274 * nil.nil? -> true
1275 * <anything_else>.nil? -> false
1276 *
1277 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1278 */
1279
1280
1281 static VALUE
1282 rb_false(VALUE obj)
1283 {
1284 return Qfalse;
1285 }
1286
1287
1288 /*
1289 * call-seq:
1290 * obj =~ other -> nil
1291 *
1292 * Pattern Match---Overridden by descendants (notably
1293 * <code>Regexp</code> and <code>String</code>) to provide meaningful
1294 * pattern-match semantics.
1295 */
1296
1297 static VALUE
1298 rb_obj_match(VALUE obj1, VALUE obj2)
1299 {
1300 return Qnil;
1301 }
1302
1303 /*
1304 * call-seq:
1305 * obj !~ other -> true or false
1306 *
1307 * Returns true if two objects do not match (using the <i>=~</i>
1308 * method), otherwise false.
1309 */
1310
1311 static VALUE
1312 rb_obj_not_match(VALUE obj1, VALUE obj2)
1313 {
1314 VALUE result = rb_funcall(obj1, id_match, 1, obj2);
1315 return RTEST(result) ? Qfalse : Qtrue;
1316 }
1317
1318
1319 /*
1320 * call-seq:
1321 * obj <=> other -> 0 or nil
1322 *
1323 * Returns 0 if obj === other, otherwise nil.
1324 *
1325 * The <=> is used by various methods to compare objects, for example
1326 * Enumerable#sort, Enumerable#max etc.
1327 *
1328 * Your implementation of <=> should return one of the following values: -1, 0,
1329 * 1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
1330 * 1 means self is bigger than other. Nil means the two values could not be
1331 * compared.
1332 *
1333 * When you defined <=>, you can include Comparable to gain the methods <=, <,
1334 * ==, >=, > and between?.
1335 */
1336 static VALUE
1337 rb_obj_cmp(VALUE obj1, VALUE obj2)
1338 {
1339 if (obj1 == obj2 || rb_equal(obj1, obj2))
1340 return INT2FIX(0);
1341 return Qnil;
1342 }
1343
1344 /***********************************************************************
1345 *
1346 * Document-class: Module
1347 *
1348 * A <code>Module</code> is a collection of methods and constants. The
1349 * methods in a module may be instance methods or module methods.
1350 * Instance methods appear as methods in a class when the module is
1351 * included, module methods do not. Conversely, module methods may be
1352 * called without creating an encapsulating object, while instance
1353 * methods may not. (See <code>Module#module_function</code>)
1354 *
1355 * In the descriptions that follow, the parameter <i>sym</i> refers
1356 * to a symbol, which is either a quoted string or a
1357 * <code>Symbol</code> (such as <code>:name</code>).
1358 *
1359 * module Mod
1360 * include Math
1361 * CONST = 1
1362 * def meth
1363 * # ...
1364 * end
1365 * end
1366 * Mod.class #=> Module
1367 * Mod.constants #=> [:CONST, :PI, :E]
1368 * Mod.instance_methods #=> [:meth]
1369 *
1370 */
1371
1372 /*
1373 * call-seq:
1374 * mod.to_s -> string
1375 *
1376 * Return a string representing this module or class. For basic
1377 * classes and modules, this is the name. For singletons, we
1378 * show information on the thing we're attached to as well.
1379 */
1380
1381 static VALUE
1382 rb_mod_to_s(VALUE klass)
1383 {
1384 ID id_defined_at;
1385 VALUE refined_class, defined_at;
1386
1387 if (FL_TEST(klass, FL_SINGLETON)) {
1388 VALUE s = rb_usascii_str_new2("#<Class:");
1389 VALUE v = rb_iv_get(klass, "__attached__");
1390
1391 if (CLASS_OR_MODULE_P(v)) {
1392 rb_str_append(s, rb_inspect(v));
1393 }
1394 else {
1395 rb_str_append(s, rb_any_to_s(v));
1396 }
1397 rb_str_cat2(s, ">");
1398
1399 return s;
1400 }
1401 refined_class = rb_refinement_module_get_refined_class(klass);
1402 if (!NIL_P(refined_class)) {
1403 VALUE s = rb_usascii_str_new2("#<refinement:");
1404
1405 rb_str_concat(s, rb_inspect(refined_class));
1406 rb_str_cat2(s, "@");
1407 CONST_ID(id_defined_at, "__defined_at__");
1408 defined_at = rb_attr_get(klass, id_defined_at);
1409 rb_str_concat(s, rb_inspect(defined_at));
1410 rb_str_cat2(s, ">");
1411 return s;
1412 }
1413 return rb_str_dup(rb_class_name(klass));
1414 }
1415
1416 /*
1417 * call-seq:
1418 * mod.freeze -> mod
1419 *
1420 * Prevents further modifications to <i>mod</i>.
1421 *
1422 * This method returns self.
1423 */
1424
1425 static VALUE
1426 rb_mod_freeze(VALUE mod)
1427 {
1428 rb_class_name(mod);
1429 return rb_obj_freeze(mod);
1430 }
1431
1432 /*
1433 * call-seq:
1434 * mod === obj -> true or false
1435 *
1436 * Case Equality---Returns <code>true</code> if <i>anObject</i> is an
1437 * instance of <i>mod</i> or one of <i>mod</i>'s descendants. Of
1438 * limited use for modules, but can be used in <code>case</code>
1439 * statements to classify objects by class.
1440 */
1441
1442 static VALUE
1443 rb_mod_eqq(VALUE mod, VALUE arg)
1444 {
1445 return rb_obj_is_kind_of(arg, mod);
1446 }
1447
1448 /*
1449 * call-seq:
1450 * mod <= other -> true, false, or nil
1451 *
1452 * Returns true if <i>mod</i> is a subclass of <i>other</i> or
1453 * is the same as <i>other</i>. Returns
1454 * <code>nil</code> if there's no relationship between the two.
1455 * (Think of the relationship in terms of the class definition:
1456 * "class A<B" implies "A<B").
1457 *
1458 */
1459
1460 VALUE
1461 rb_class_inherited_p(VALUE mod, VALUE arg)
1462 {
1463 VALUE start = mod;
1464
1465 if (mod == arg) return Qtrue;
1466 if (!CLASS_OR_MODULE_P(arg)) {
1467 rb_raise(rb_eTypeError, "compared with non class/module");
1468 }
1469 arg = RCLASS_ORIGIN(arg);
1470 while (mod) {
1471 if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg))
1472 return Qtrue;
1473 mod = RCLASS_SUPER(mod);
1474 }
1475 /* not mod < arg; check if mod > arg */
1476 while (arg) {
1477 if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start))
1478 return Qfalse;
1479 arg = RCLASS_SUPER(arg);
1480 }
1481 return Qnil;
1482 }
1483
1484 /*
1485 * call-seq:
1486 * mod < other -> true, false, or nil
1487 *
1488 * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
1489 * <code>nil</code> if there's no relationship between the two.
1490 * (Think of the relationship in terms of the class definition:
1491 * "class A<B" implies "A<B").
1492 *
1493 */
1494
1495 static VALUE
1496 rb_mod_lt(VALUE mod, VALUE arg)
1497 {
1498 if (mod == arg) return Qfalse;
1499 return rb_class_inherited_p(mod, arg);
1500 }
1501
1502
1503 /*
1504 * call-seq:
1505 * mod >= other -> true, false, or nil
1506 *
1507 * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
1508 * two modules are the same. Returns
1509 * <code>nil</code> if there's no relationship between the two.
1510 * (Think of the relationship in terms of the class definition:
1511 * "class A<B" implies "B>A").
1512 *
1513 */
1514
1515 static VALUE
1516 rb_mod_ge(VALUE mod, VALUE arg)
1517 {
1518 if (!CLASS_OR_MODULE_P(arg)) {
1519 rb_raise(rb_eTypeError, "compared with non class/module");
1520 }
1521
1522 return rb_class_inherited_p(arg, mod);
1523 }
1524
1525 /*
1526 * call-seq:
1527 * mod > other -> true, false, or nil
1528 *
1529 * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
1530 * <code>nil</code> if there's no relationship between the two.
1531 * (Think of the relationship in terms of the class definition:
1532 * "class A<B" implies "B>A").
1533 *
1534 */
1535
1536 static VALUE
1537 rb_mod_gt(VALUE mod, VALUE arg)
1538 {
1539 if (mod == arg) return Qfalse;
1540 return rb_mod_ge(mod, arg);
1541 }
1542
1543 /*
1544 * call-seq:
1545 * module <=> other_module -> -1, 0, +1, or nil
1546 *
1547 * Comparison---Returns -1, 0, +1 or nil depending on whether +module+
1548 * includes +other_module+, they are the same, or if +module+ is included by
1549 * +other_module+. This is the basis for the tests in Comparable.
1550 *
1551 * Returns +nil+ if +module+ has no relationship with +other_module+, if
1552 * +other_module+ is not a module, or if the two values are incomparable.
1553 */
1554
1555 static VALUE
1556 rb_mod_cmp(VALUE mod, VALUE arg)
1557 {
1558 VALUE cmp;
1559
1560 if (mod == arg) return INT2FIX(0);
1561 if (!CLASS_OR_MODULE_P(arg)) {
1562 return Qnil;
1563 }
1564
1565 cmp = rb_class_inherited_p(mod, arg);
1566 if (NIL_P(cmp)) return Qnil;
1567 if (cmp) {
1568 return INT2FIX(-1);
1569 }
1570 return INT2FIX(1);
1571 }
1572
1573 static VALUE
1574 rb_module_s_alloc(VALUE klass)
1575 {
1576 VALUE mod = rb_module_new();
1577
1578 RBASIC(mod)->klass = klass;
1579 return mod;
1580 }
1581
1582 static VALUE
1583 rb_class_s_alloc(VALUE klass)
1584 {
1585 return rb_class_boot(0);
1586 }
1587
1588 /*
1589 * call-seq:
1590 * Module.new -> mod
1591 * Module.new {|mod| block } -> mod
1592 *
1593 * Creates a new anonymous module. If a block is given, it is passed
1594 * the module object, and the block is evaluated in the context of this
1595 * module using <code>module_eval</code>.
1596 *
1597 * fred = Module.new do
1598 * def meth1
1599 * "hello"
1600 * end
1601 * def meth2
1602 * "bye"
1603 * end
1604 * end
1605 * a = "my string"
1606 * a.extend(fred) #=> "my string"
1607 * a.meth1 #=> "hello"
1608 * a.meth2 #=> "bye"
1609 *
1610 * Assign the module to a constant (name starting uppercase) if you
1611 * want to treat it like a regular module.
1612 */
1613
1614 static VALUE
1615 rb_mod_initialize(VALUE module)
1616 {
1617 if (rb_block_given_p()) {
1618 rb_mod_module_exec(1, &module, module);
1619 }
1620 return Qnil;
1621 }
1622
1623 /*
1624 * call-seq:
1625 * Class.new(super_class=Object) -> a_class
1626 * Class.new(super_class=Object) { |mod| ... } -> a_class
1627 *
1628 * Creates a new anonymous (unnamed) class with the given superclass
1629 * (or <code>Object</code> if no parameter is given). You can give a
1630 * class a name by assigning the class object to a constant.
1631 *
1632 * If a block is given, it is passed the class object, and the block
1633 * is evaluated in the context of this class using
1634 * <code>class_eval</code>.
1635 *
1636 * fred = Class.new do
1637 * def meth1
1638 * "hello"
1639 * end
1640 * def meth2
1641 * "bye"
1642 * end
1643 * end
1644 *
1645 * a = fred.new #=> #<#<Class:0x100381890>:0x100376b98>
1646 * a.meth1 #=> "hello"
1647 * a.meth2 #=> "bye"
1648 *
1649 * Assign the class to a constant (name starting uppercase) if you
1650 * want to treat it like a regular class.
1651 */
1652
1653 static VALUE
1654 rb_class_initialize(int argc, VALUE *argv, VALUE klass)
1655 {
1656 VALUE super;
1657
1658 if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
1659 rb_raise(rb_eTypeError, "already initialized class");
1660 }
1661 if (argc == 0) {
1662 super = rb_cObject;
1663 }
1664 else {
1665 rb_scan_args(argc, argv, "01", &super);
1666 rb_check_inheritable(super);
1667 if (super != rb_cBasicObject && !RCLASS_SUPER(super)) {
1668 rb_raise(rb_eTypeError, "can't inherit uninitialized class");
1669 }
1670 }
1671 RCLASS_SUPER(klass) = super;
1672 rb_make_metaclass(klass, RBASIC(super)->klass);
1673 rb_class_inherited(super, klass);
1674 rb_mod_initialize(klass);
1675
1676 return klass;
1677 }
1678
1679 /*
1680 * call-seq:
1681 * class.allocate() -> obj
1682 *
1683 * Allocates space for a new object of <i>class</i>'s class and does not
1684 * call initialize on the new instance. The returned object must be an
1685 * instance of <i>class</i>.
1686 *
1687 * klass = Class.new do
1688 * def initialize(*args)
1689 * @initialized = true
1690 * end
1691 *
1692 * def initialized?
1693 * @initialized || false
1694 * end
1695 * end
1696 *
1697 * klass.allocate.initialized? #=> false
1698 *
1699 */
1700
1701 VALUE
1702 rb_obj_alloc(VALUE klass)
1703 {
1704 VALUE obj;
1705 rb_alloc_func_t allocator;
1706
1707 if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
1708 rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
1709 }
1710 if (FL_TEST(klass, FL_SINGLETON)) {
1711 rb_raise(rb_eTypeError, "can't create instance of singleton class");
1712 }
1713 allocator = rb_get_alloc_func(klass);
1714 if (!allocator) {
1715 rb_raise(rb_eTypeError, "allocator undefined for %"PRIsVALUE,
1716 klass);
1717 }
1718
1719 #if !defined(DTRACE_PROBES_DISABLED) || !DTRACE_PROBES_DISABLED
1720 if (RUBY_DTRACE_OBJECT_CREATE_ENABLED()) {
1721 const char * file = rb_sourcefile();
1722 RUBY_DTRACE_OBJECT_CREATE(rb_class2name(klass),
1723 file ? file : "",
1724 rb_sourceline());
1725 }
1726 #endif
1727
1728 obj = (*allocator)(klass);
1729
1730 if (rb_obj_class(obj) != rb_class_real(klass)) {
1731 rb_raise(rb_eTypeError, "wrong instance allocation");
1732 }
1733 return obj;
1734 }
1735
1736 static VALUE
1737 rb_class_allocate_instance(VALUE klass)
1738 {
1739 NEWOBJ_OF(obj, struct RObject, klass, T_OBJECT);
1740 return (VALUE)obj;
1741 }
1742
1743 /*
1744 * call-seq:
1745 * class.new(args, ...) -> obj
1746 *
1747 * Calls <code>allocate</code> to create a new object of
1748 * <i>class</i>'s class, then invokes that object's
1749 * <code>initialize</code> method, passing it <i>args</i>.
1750 * This is the method that ends up getting called whenever
1751 * an object is constructed using .new.
1752 *
1753 */
1754
1755 VALUE
1756 rb_class_new_instance(int argc, VALUE *argv, VALUE klass)
1757 {
1758 VALUE obj;
1759
1760 obj = rb_obj_alloc(klass);
1761 rb_obj_call_init(obj, argc, argv);
1762
1763 return obj;
1764 }
1765
1766 /*
1767 * call-seq:
1768 * class.superclass -> a_super_class or nil
1769 *
1770 * Returns the superclass of <i>class</i>, or <code>nil</code>.
1771 *
1772 * File.superclass #=> IO
1773 * IO.superclass #=> Object
1774 * Object.superclass #=> BasicObject
1775 * class Foo; end
1776 * class Bar < Foo; end
1777 * Bar.superclass #=> Foo
1778 *
1779 * returns nil when the given class hasn't a parent class:
1780 *
1781 * BasicObject.superclass #=> nil
1782 *
1783 */
1784
1785 VALUE
1786 rb_class_superclass(VALUE klass)
1787 {
1788 VALUE super = RCLASS_SUPER(klass);
1789
1790 if (!super) {
1791 if (klass == rb_cBasicObject) return Qnil;
1792 rb_raise(rb_eTypeError, "uninitialized class");
1793 }
1794 while (RB_TYPE_P(super, T_ICLASS)) {
1795 super = RCLASS_SUPER(super);
1796 }
1797 if (!super) {
1798 return Qnil;
1799 }
1800 return super;
1801 }
1802
1803 VALUE
1804 rb_class_get_superclass(VALUE klass)
1805 {
1806 return RCLASS_SUPER(klass);
1807 }
1808
1809 /*
1810 * call-seq:
1811 * attr_reader(symbol, ...) -> nil
1812 * attr(symbol, ...) -> nil
1813 *
1814 * Creates instance variables and corresponding methods that return the
1815 * value of each instance variable. Equivalent to calling
1816 * ``<code>attr</code><i>:name</i>'' on each name in turn.
1817 */
1818
1819 static VALUE
1820 rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
1821 {
1822 int i;
1823
1824 for (i=0; i<argc; i++) {
1825 rb_attr(klass, rb_to_id(argv[i]), TRUE, FALSE, TRUE);
1826 }
1827 return Qnil;
1828 }
1829
1830 VALUE
1831 rb_mod_attr(int argc, VALUE *argv, VALUE klass)
1832 {
1833 if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
1834 rb_warning("optional boolean argument is obsoleted");
1835 rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), TRUE);
1836 return Qnil;
1837 }
1838 return rb_mod_attr_reader(argc, argv, klass);
1839 }
1840
1841 /*
1842 * call-seq:
1843 * attr_writer(symbol, ...) -> nil
1844 *
1845 * Creates an accessor method to allow assignment to the attribute
1846 * <i>symbol</i><code>.id2name</code>.
1847 */
1848
1849 static VALUE
1850 rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
1851 {
1852 int i;
1853
1854 for (i=0; i<argc; i++) {
1855 rb_attr(klass, rb_to_id(argv[i]), FALSE, TRUE, TRUE);
1856 }
1857 return Qnil;
1858 }
1859
1860 /*
1861 * call-seq:
1862 * attr_accessor(symbol, ...) -> nil
1863 *
1864 * Defines a named attribute for this module, where the name is
1865 * <i>symbol.</i><code>id2name</code>, creating an instance variable
1866 * (<code>@name</code>) and a corresponding access method to read it.
1867 * Also creates a method called <code>name=</code> to set the attribute.
1868 *
1869 * module Mod
1870 * attr_accessor(:one, :two)
1871 * end
1872 * Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
1873 */
1874
1875 static VALUE
1876 rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
1877 {
1878 int i;
1879
1880 for (i=0; i<argc; i++) {
1881 rb_attr(klass, rb_to_id(argv[i]), TRUE, TRUE, TRUE);
1882 }
1883 return Qnil;
1884 }
1885
1886 /*
1887 * call-seq:
1888 * mod.const_get(sym, inherit=true) -> obj
1889 * mod.const_get(str, inherit=true) -> obj
1890 *
1891 * Checks for a constant with the given name in <i>mod</i>
1892 * If +inherit+ is set, the lookup will also search
1893 * the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
1894 *
1895 * The value of the constant is returned if a definition is found,
1896 * otherwise a +NameError+ is raised.
1897 *
1898 * Math.const_get(:PI) #=> 3.14159265358979
1899 *
1900 * This method will recursively look up constant names if a namespaced
1901 * class name is provided. For example:
1902 *
1903 * module Foo; class Bar; end end
1904 * Object.const_get 'Foo::Bar'
1905 *
1906 * The +inherit+ flag is respected on each lookup. For example:
1907 *
1908 * module Foo
1909 * class Bar
1910 * VAL = 10
1911 * end
1912 *
1913 * class Baz < Bar; end
1914 * end
1915 *
1916 * Object.const_get 'Foo::Baz::VAL' # => 10
1917 * Object.const_get 'Foo::Baz::VAL', false # => NameError
1918 */
1919
1920 static VALUE
1921 rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
1922 {
1923 VALUE name, recur;
1924 rb_encoding *enc;
1925 const char *pbeg, *p, *path, *pend;
1926 ID id;
1927 int nestable = 1;
1928
1929 if (argc == 1) {
1930 name = argv[0];
1931 recur = Qtrue;
1932 }
1933 else {
1934 rb_scan_args(argc, argv, "11", &name, &recur);
1935 }
1936
1937 if (SYMBOL_P(name)) {
1938 name = rb_sym_to_s(name);
1939 nestable = 0;
1940 }
1941
1942 name = rb_check_string_type(name);
1943 Check_Type(name, T_STRING);
1944
1945 enc = rb_enc_get(name);
1946 path = RSTRING_PTR(name);
1947
1948 if (!rb_enc_asciicompat(enc)) {
1949 rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
1950 }
1951
1952 pbeg = p = path;
1953 pend = path + RSTRING_LEN(name);
1954
1955 if (p >= pend || !*p) {
1956 wrong_name:
1957 rb_raise(rb_eNameError, "wrong constant name %"PRIsVALUE,
1958 QUOTE(name));
1959 }
1960
1961 if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
1962 if (!nestable) goto wrong_name;
1963 mod = rb_cObject;
1964 p += 2;
1965 pbeg = p;
1966 }
1967
1968 while (p < pend) {
1969 VALUE part;
1970 long len, beglen;
1971
1972 while (p < pend && *p != ':') p++;
1973
1974 if (pbeg == p) goto wrong_name;
1975
1976 id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
1977 beglen = pbeg-path;
1978
1979 if (p < pend && p[0] == ':') {
1980 if (!nestable) goto wrong_name;
1981 if (p + 2 >= pend || p[1] != ':') goto wrong_name;
1982 p += 2;
1983 pbeg = p;
1984 }
1985
1986 if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
1987 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
1988 QUOTE(name));
1989 }
1990
1991 if (!id) {
1992 if (!ISUPPER(*pbeg) || !rb_enc_symname2_p(pbeg, len, enc)) {
1993 part = rb_str_subseq(name, beglen, len);
1994 rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
1995 QUOTE(part));
1996 }
1997 else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
1998 id = rb_intern3(pbeg, len, enc);
1999 }
2000 else {
2001 part = rb_str_subseq(name, beglen, len);
2002 rb_name_error_str(part, "uninitialized constant %"PRIsVALUE"%"PRIsVALUE,
2003 rb_str_subseq(name, 0, beglen),
2004 QUOTE(part));
2005 }
2006 }
2007 if (!rb_is_const_id(id)) {
2008 rb_name_error(id, "wrong constant name %"PRIsVALUE,
2009 QUOTE_ID(id));
2010 }
2011 mod = RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
2012 }
2013
2014 return mod;
2015 }
2016
2017 /*
2018 * call-seq:
2019 * mod.const_set(sym, obj) -> obj
2020 *
2021 * Sets the named constant to the given object, returning that object.
2022 * Creates a new constant if no constant with the given name previously
2023 * existed.
2024 *
2025 * Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
2026 * Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
2027 */
2028
2029 static VALUE
2030 rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
2031 {
2032 ID id = rb_to_id(name);
2033
2034 if (!rb_is_const_id(id)) {
2035 rb_name_error(id, "wrong constant name %"PRIsVALUE,
2036 QUOTE_ID(id));
2037 }
2038 rb_const_set(mod, id, value);
2039 return value;
2040 }
2041
2042 /*
2043 * call-seq:
2044 * mod.const_defined?(sym, inherit=true) -> true or false
2045 *
2046 * Checks for a constant with the given name in <i>mod</i>
2047 * If +inherit+ is set, the lookup will also search
2048 * the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
2049 *
2050 * Returns whether or not a definition is found:
2051 *
2052 * Math.const_defined? "PI" #=> true
2053 * IO.const_defined? :SYNC #=> true
2054 * IO.const_defined? :SYNC, false #=> false
2055 */
2056
2057 static VALUE
2058 rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
2059 {
2060 VALUE name, recur;
2061 ID id;
2062
2063 if (argc == 1) {
2064 name = argv[0];
2065 recur = Qtrue;
2066 }
2067 else {
2068 rb_scan_args(argc, argv, "11", &name, &recur);
2069 }
2070 if (!(id = rb_check_id(&name))) {
2071 if (rb_is_const_name(name)) {
2072 return Qfalse;
2073 }
2074 else {
2075 rb_name_error_str(name, "wrong constant name %"PRIsVALUE,
2076 QUOTE(name));
2077 }
2078 }
2079 if (!rb_is_const_id(id)) {
2080 rb_name_error(id, "wrong constant name %"PRIsVALUE,
2081 QUOTE_ID(id));
2082 }
2083 return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
2084 }
2085
2086 /*
2087 * call-seq:
2088 * obj.instance_variable_get(symbol) -> obj
2089 *
2090 * Returns the value of the given instance variable, or nil if the
2091 * instance variable is not set. The <code>@</code> part of the
2092 * variable name should be included for regular instance
2093 * variables. Throws a <code>NameError</code> exception if the
2094 * supplied symbol is not valid as an instance variable name.
2095 *
2096 * class Fred
2097 * def initialize(p1, p2)
2098 * @a, @b = p1, p2
2099 * end
2100 * end
2101 * fred = Fred.new('cat', 99)
2102 * fred.instance_variable_get(:@a) #=> "cat"
2103 * fred.instance_variable_get("@b") #=> 99
2104 */
2105
2106 static VALUE
2107 rb_obj_ivar_get(VALUE obj, VALUE iv)
2108 {
2109 ID id = rb_check_id(&iv);
2110
2111 if (!id) {
2112 if (rb_is_instance_name(iv)) {
2113 return Qnil;
2114 }
2115 else {
2116 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
2117 QUOTE(iv));
2118 }
2119 }
2120 if (!rb_is_instance_id(id)) {
2121 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
2122 QUOTE_ID(id));
2123 }
2124 return rb_ivar_get(obj, id);
2125 }
2126
2127 /*
2128 * call-seq:
2129 * obj.instance_variable_set(symbol, obj) -> obj
2130 *
2131 * Sets the instance variable names by <i>symbol</i> to
2132 * <i>object</i>, thereby frustrating the efforts of the class's
2133 * author to attempt to provide proper encapsulation. The variable
2134 * did not have to exist prior to this call.
2135 *
2136 * class Fred
2137 * def initialize(p1, p2)
2138 * @a, @b = p1, p2
2139 * end
2140 * end
2141 * fred = Fred.new('cat', 99)
2142 * fred.instance_variable_set(:@a, 'dog') #=> "dog"
2143 * fred.instance_variable_set(:@c, 'cat') #=> "cat"
2144 * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
2145 */
2146
2147 static VALUE
2148 rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
2149 {
2150 ID id = rb_to_id(iv);
2151
2152 if (!rb_is_instance_id(id)) {
2153 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
2154 QUOTE_ID(id));
2155 }
2156 return rb_ivar_set(obj, id, val);
2157 }
2158
2159 /*
2160 * call-seq:
2161 * obj.instance_variable_defined?(symbol) -> true or false
2162 *
2163 * Returns <code>true</code> if the given instance variable is
2164 * defined in <i>obj</i>.
2165 *
2166 * class Fred
2167 * def initialize(p1, p2)
2168 * @a, @b = p1, p2
2169 * end
2170 * end
2171 * fred = Fred.new('cat', 99)
2172 * fred.instance_variable_defined?(:@a) #=> true
2173 * fred.instance_variable_defined?("@b") #=> true
2174 * fred.instance_variable_defined?("@c") #=> false
2175 */
2176
2177 static VALUE
2178 rb_obj_ivar_defined(VALUE obj, VALUE iv)
2179 {
2180 ID id = rb_check_id(&iv);
2181
2182 if (!id) {
2183 if (rb_is_instance_name(iv)) {
2184 return Qfalse;
2185 }
2186 else {
2187 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
2188 QUOTE(iv));
2189 }
2190 }
2191 if (!rb_is_instance_id(id)) {
2192 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
2193 QUOTE_ID(id));
2194 }
2195 return rb_ivar_defined(obj, id);
2196 }
2197
2198 /*
2199 * call-seq:
2200 * mod.class_variable_get(symbol) -> obj
2201 *
2202 * Returns the value of the given class variable (or throws a
2203 * <code>NameError</code> exception). The <code>@@</code> part of the
2204 * variable name should be included for regular class variables
2205 *
2206 * class Fred
2207 * @@foo = 99
2208 * end
2209 * Fred.class_variable_get(:@@foo) #=> 99
2210 */
2211
2212 static VALUE
2213 rb_mod_cvar_get(VALUE obj, VALUE iv)
2214 {
2215 ID id = rb_check_id(&iv);
2216
2217 if (!id) {
2218 if (rb_is_class_name(iv)) {
2219 rb_name_error_str(iv, "uninitialized class variable %"PRIsVALUE" in %"PRIsVALUE"",
2220 iv, rb_class_name(obj));
2221 }
2222 else {
2223 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name",
2224 QUOTE(iv));
2225 }
2226 }
2227 if (!rb_is_class_id(id)) {
2228 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
2229 QUOTE_ID(id));
2230 }
2231 return rb_cvar_get(obj, id);
2232 }
2233
2234 /*
2235 * call-seq:
2236 * obj.class_variable_set(symbol, obj) -> obj
2237 *
2238 * Sets the class variable names by <i>symbol</i> to
2239 * <i>object</i>.
2240 *
2241 * class Fred
2242 * @@foo = 99
2243 * def foo
2244 * @@foo
2245 * end
2246 * end
2247 * Fred.class_variable_set(:@@foo, 101) #=> 101
2248 * Fred.new.foo #=> 101
2249 */
2250
2251 static VALUE
2252 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
2253 {
2254 ID id = rb_to_id(iv);
2255
2256 if (!rb_is_class_id(id)) {
2257 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
2258 QUOTE_ID(id));
2259 }
2260 rb_cvar_set(obj, id, val);
2261 return val;
2262 }
2263
2264 /*
2265 * call-seq:
2266 * obj.class_variable_defined?(symbol) -> true or false
2267 *
2268 * Returns <code>true</code> if the given class variable is defined
2269 * in <i>obj</i>.
2270 *
2271 * class Fred
2272 * @@foo = 99
2273 * end
2274 * Fred.class_variable_defined?(:@@foo) #=> true
2275 * Fred.class_variable_defined?(:@@bar) #=> false
2276 */
2277
2278 static VALUE
2279 rb_mod_cvar_defined(VALUE obj, VALUE iv)
2280 {
2281 ID id = rb_check_id(&iv);
2282
2283 if (!id) {
2284 if (rb_is_class_name(iv)) {
2285 return Qfalse;
2286 }
2287 else {
2288 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name",
2289 QUOTE(iv));
2290 }
2291 }
2292 if (!rb_is_class_id(id)) {
2293 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
2294 QUOTE_ID(id));
2295 }
2296 return rb_cvar_defined(obj, id);
2297 }
2298
2299 static struct conv_method_tbl {
2300 const char *method;
2301 ID id;
2302 } conv_method_names[] = {
2303 {"to_int", 0},
2304 {"to_ary", 0},
2305 {"to_str", 0},
2306 {"to_sym", 0},
2307 {"to_hash", 0},
2308 {"to_proc", 0},
2309 {"to_io", 0},
2310 {"to_a", 0},
2311 {"to_s", 0},
2312 {NULL, 0}
2313 };
2314 #define IMPLICIT_CONVERSIONS 7
2315
2316 static VALUE
2317 convert_type(VALUE val, const char *tname, const char *method, int raise)
2318 {
2319 ID m = 0;
2320 int i;
2321 VALUE r;
2322
2323 for (i=0; conv_method_names[i].method; i++) {
2324 if (conv_method_names[i].method[0] == method[0] &&
2325 strcmp(conv_method_names[i].method, method) == 0) {
2326 m = conv_method_names[i].id;
2327 break;
2328 }
2329 }
2330 if (!m) m = rb_intern(method);
2331 r = rb_check_funcall(val, m, 0, 0);
2332 if (r == Qundef) {
2333 if (raise) {
2334 rb_raise(rb_eTypeError, i < IMPLICIT_CONVERSIONS
2335 ? "no implicit conversion of %s into %s"
2336 : "can't convert %s into %s",
2337 NIL_P(val) ? "nil" :
2338 val == Qtrue ? "true" :
2339 val == Qfalse ? "false" :
2340 rb_obj_classname(val),
2341 tname);
2342 }
2343 return Qnil;
2344 }
2345 return r;
2346 }
2347
2348 VALUE
2349 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
2350 {
2351 VALUE v;
2352
2353 if (TYPE(val) == type) return val;
2354 v = convert_type(val, tname, method, TRUE);
2355 if (TYPE(v) != type) {
2356 const char *cname = rb_obj_classname(val);
2357 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
2358 cname, tname, cname, method, rb_obj_classname(v));
2359 }
2360 return v;
2361 }
2362
2363 VALUE
2364 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
2365 {
2366 VALUE v;
2367
2368 /* always convert T_DATA */
2369 if (TYPE(val) == type && type != T_DATA) return val;
2370 v = convert_type(val, tname, method, FALSE);
2371 if (NIL_P(v)) return Qnil;
2372 if (TYPE(v) != type) {
2373 const char *cname = rb_obj_classname(val);
2374 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
2375 cname, tname, cname, method, rb_obj_classname(v));
2376 }
2377 return v;
2378 }
2379
2380
2381 static VALUE
2382 rb_to_integer(VALUE val, const char *method)
2383 {
2384 VALUE v;
2385
2386 if (FIXNUM_P(val)) return val;
2387 if (RB_TYPE_P(val, T_BIGNUM)) return val;
2388 v = convert_type(val, "Integer", method, TRUE);
2389 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
2390 const char *cname = rb_obj_classname(val);
2391 rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)",
2392 cname, cname, method, rb_obj_classname(v));
2393 }
2394 return v;
2395 }
2396
2397 VALUE
2398 rb_check_to_integer(VALUE val, const char *method)
2399 {
2400 VALUE v;
2401
2402 if (FIXNUM_P(val)) return val;
2403 if (RB_TYPE_P(val, T_BIGNUM)) return val;
2404 v = convert_type(val, "Integer", method, FALSE);
2405 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
2406 return Qnil;
2407 }
2408 return v;
2409 }
2410
2411 VALUE
2412 rb_to_int(VALUE val)
2413 {
2414 return rb_to_integer(val, "to_int");
2415 }
2416
2417 VALUE
2418 rb_check_to_int(VALUE val)
2419 {
2420 return rb_check_to_integer(val, "to_int");
2421 }
2422
2423 static VALUE
2424 rb_convert_to_integer(VALUE val, int base)
2425 {
2426 VALUE tmp;
2427
2428 switch (TYPE(val)) {
2429 case T_FLOAT:
2430 if (base != 0) goto arg_error;
2431 if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX
2432 && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) {
2433 break;
2434 }
2435 return rb_dbl2big(RFLOAT_VALUE(val));
2436
2437 case T_FIXNUM:
2438 case T_BIGNUM:
2439 if (base != 0) goto arg_error;
2440 return val;
2441
2442 case T_STRING:
2443 string_conv:
2444 return rb_str_to_inum(val, base, TRUE);
2445
2446 case T_NIL:
2447 if (base != 0) goto arg_error;
2448 rb_raise(rb_eTypeError, "can't convert nil into Integer");
2449 break;
2450
2451 default:
2452 break;
2453 }
2454 if (base != 0) {
2455 tmp = rb_check_string_type(val);
2456 if (!NIL_P(tmp)) goto string_conv;
2457 arg_error:
2458 rb_raise(rb_eArgError, "base specified for non string value");
2459 }
2460 tmp = convert_type(val, "Integer", "to_int", FALSE);
2461 if (NIL_P(tmp)) {
2462 return rb_to_integer(val, "to_i");
2463 }
2464 return tmp;
2465
2466 }
2467
2468 VALUE
2469 rb_Integer(VALUE val)
2470 {
2471 return rb_convert_to_integer(val, 0);
2472 }
2473
2474 /*
2475 * call-seq:
2476 * Integer(arg,base=0) -> integer
2477 *
2478 * Converts <i>arg</i> to a <code>Fixnum</code> or <code>Bignum</code>.
2479 * Numeric types are converted directly (with floating point numbers
2480 * being truncated). <i>base</i> (0, or between 2 and 36) is a base for
2481 * integer string representation. If <i>arg</i> is a <code>String</code>,
2482 * when <i>base</i> is omitted or equals to zero, radix indicators
2483 * (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
2484 * In any case, strings should be strictly conformed to numeric
2485 * representation. This behavior is different from that of
2486 * <code>String#to_i</code>. Non string values will be converted using
2487 * <code>to_int</code>, and <code>to_i</code>.
2488 *
2489 * Integer(123.999) #=> 123
2490 * Integer("0x1a") #=> 26
2491 * Integer(Time.new) #=> 1204973019
2492 * Integer("0930", 10) #=> 930
2493 * Integer("111", 2) #=> 7
2494 */
2495
2496 static VALUE
2497 rb_f_integer(int argc, VALUE *argv, VALUE obj)
2498 {
2499 VALUE arg = Qnil;
2500 int base = 0;
2501
2502 switch (argc) {
2503 case 2:
2504 base = NUM2INT(argv[1]);
2505 case 1:
2506 arg = argv[0];
2507 break;
2508 default:
2509 /* should cause ArgumentError */
2510 rb_scan_args(argc, argv, "11", NULL, NULL);
2511 }
2512 return rb_convert_to_integer(arg, base);
2513 }
2514
2515 double
2516 rb_cstr_to_dbl(const char *p, int badcheck)
2517 {
2518 const char *q;
2519 char *end;
2520 double d;
2521 const char *ellipsis = "";
2522 int w;
2523 enum {max_width = 20};
2524 #define OutOfRange() ((end - p > max_width) ? \
2525 (w = max_width, ellipsis = "...") : \
2526 (w = (int)(end - p), ellipsis = ""))
2527
2528 if (!p) return 0.0;
2529 q = p;
2530 while (ISSPACE(*p)) p++;
2531
2532 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2533 return 0.0;
2534 }
2535
2536 d = strtod(p, &end);
2537 if (errno == ERANGE) {
2538 OutOfRange();
2539 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
2540 errno = 0;
2541 }
2542 if (p == end) {
2543 if (badcheck) {
2544 bad:
2545 rb_invalid_str(q, "Float()");
2546 }
2547 return d;
2548 }
2549 if (*end) {
2550 char buf[DBL_DIG * 4 + 10];
2551 char *n = buf;
2552 char *e = buf + sizeof(buf) - 1;
2553 char prev = 0;
2554
2555 while (p < end && n < e) prev = *n++ = *p++;
2556 while (*p) {
2557 if (*p == '_') {
2558 /* remove underscores between digits */
2559 if (badcheck) {
2560 if (n == buf || !ISDIGIT(prev)) goto bad;
2561 ++p;
2562 if (!ISDIGIT(*p)) goto bad;
2563 }
2564 else {
2565 while (*++p == '_');
2566 continue;
2567 }
2568 }
2569 prev = *p++;
2570 if (n < e) *n++ = prev;
2571 }
2572 *n = '\0';
2573 p = buf;
2574
2575 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2576 return 0.0;
2577 }
2578
2579 d = strtod(p, &end);
2580 if (errno == ERANGE) {
2581 OutOfRange();
2582 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
2583 errno = 0;
2584 }
2585 if (badcheck) {
2586 if (!end || p == end) goto bad;
2587 while (*end && ISSPACE(*end)) end++;
2588 if (*end) goto bad;
2589 }
2590 }
2591 if (errno == ERANGE) {
2592 errno = 0;
2593 OutOfRange();
2594 rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
2595 }
2596 return d;
2597 }
2598
2599 double
2600 rb_str_to_dbl(VALUE str, int badcheck)
2601 {
2602 char *s;
2603 long len;
2604 double ret;
2605 VALUE v = 0;
2606
2607 StringValue(str);
2608 s = RSTRING_PTR(str);
2609 len = RSTRING_LEN(str);
2610 if (s) {
2611 if (badcheck && memchr(s, '\0', len)) {
2612 rb_raise(rb_eArgError, "string for Float contains null byte");
2613 }
2614 if (s[len]) { /* no sentinel somehow */
2615 char *p = ALLOCV(v, len);
2616 MEMCPY(p, s, char, len);
2617 p[len] = '\0';
2618 s = p;
2619 }
2620 }
2621 ret = rb_cstr_to_dbl(s, badcheck);
2622 if (v)
2623 ALLOCV_END(v);
2624 return ret;
2625 }
2626
2627 VALUE
2628 rb_Float(VALUE val)
2629 {
2630 switch (TYPE(val)) {
2631 case T_FIXNUM:
2632 return DBL2NUM((double)FIX2LONG(val));
2633
2634 case T_FLOAT:
2635 return val;
2636
2637 case T_BIGNUM:
2638 return DBL2NUM(rb_big2dbl(val));
2639
2640 case T_STRING:
2641 return DBL2NUM(rb_str_to_dbl(val, TRUE));
2642
2643 case T_NIL:
2644 rb_raise(rb_eTypeError, "can't convert nil into Float");
2645 break;
2646
2647 default:
2648 return rb_convert_type(val, T_FLOAT, "Float", "to_f");
2649 }
2650
2651 UNREACHABLE;
2652 }
2653
2654 /*
2655 * call-seq:
2656 * Float(arg) -> float
2657 *
2658 * Returns <i>arg</i> converted to a float. Numeric types are converted
2659 * directly, the rest are converted using <i>arg</i>.to_f. As of Ruby
2660 * 1.8, converting <code>nil</code> generates a <code>TypeError</code>.
2661 *
2662 * Float(1) #=> 1.0
2663 * Float("123.456") #=> 123.456
2664 */
2665
2666 static VALUE
2667 rb_f_float(VALUE obj, VALUE arg)
2668 {
2669 return rb_Float(arg);
2670 }
2671
2672 VALUE
2673 rb_to_float(VALUE val)
2674 {
2675 if (RB_TYPE_P(val, T_FLOAT)) return val;
2676 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
2677 rb_raise(rb_eTypeError, "can't convert %s into Float",
2678 NIL_P(val) ? "nil" :
2679 val == Qtrue ? "true" :
2680 val == Qfalse ? "false" :
2681 rb_obj_classname(val));
2682 }
2683 return rb_convert_type(val, T_FLOAT, "Float", "to_f");
2684 }
2685
2686 VALUE
2687 rb_check_to_float(VALUE val)
2688 {
2689 if (RB_TYPE_P(val, T_FLOAT)) return val;
2690 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
2691 return Qnil;
2692 }
2693 return rb_check_convert_type(val, T_FLOAT, "Float", "to_f");
2694 }
2695
2696 double
2697 rb_num2dbl(VALUE val)
2698 {
2699 switch (TYPE(val)) {
2700 case T_FLOAT:
2701 return RFLOAT_VALUE(val);
2702
2703 case T_STRING:
2704 rb_raise(rb_eTypeError, "no implicit conversion to float from string");
2705 break;
2706
2707 case T_NIL:
2708 rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
2709 break;
2710
2711 default:
2712 break;
2713 }
2714
2715 return RFLOAT_VALUE(rb_Float(val));
2716 }
2717
2718 VALUE
2719 rb_String(VALUE val)
2720 {
2721 VALUE tmp = rb_check_string_type(val);
2722 if (NIL_P(tmp))
2723 tmp = rb_convert_type(val, T_STRING, "String", "to_s");
2724 return tmp;
2725 }
2726
2727
2728 /*
2729 * call-seq:
2730 * String(arg) -> string
2731 *
2732 * Converts <i>arg</i> to a <code>String</code> by calling its
2733 * <code>to_s</code> method.
2734 *
2735 * String(self) #=> "main"
2736 * String(self.class) #=> "Object"
2737 * String(123456) #=> "123456"
2738 */
2739
2740 static VALUE
2741 rb_f_string(VALUE obj, VALUE arg)
2742 {
2743 return rb_String(arg);
2744 }
2745
2746 VALUE
2747 rb_Array(VALUE val)
2748 {
2749 VALUE tmp = rb_check_array_type(val);
2750
2751 if (NIL_P(tmp)) {
2752 tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
2753 if (NIL_P(tmp)) {
2754 return rb_ary_new3(1, val);
2755 }
2756 }
2757 return tmp;
2758 }
2759
2760 /*
2761 * call-seq:
2762 * Array(arg) -> array
2763 *
2764 * Returns +arg+ as an Array.
2765 *
2766 * First tries to call Array#to_ary on +arg+, then Array#to_a.
2767 *
2768 * Array(1..5) #=> [1, 2, 3, 4, 5]
2769 */
2770
2771 static VALUE
2772 rb_f_array(VALUE obj, VALUE arg)
2773 {
2774 return rb_Array(arg);
2775 }
2776
2777 VALUE
2778 rb_Hash(VALUE val)
2779 {
2780 VALUE tmp;
2781
2782 if (NIL_P(val)) return rb_hash_new();
2783 tmp = rb_check_hash_type(val);
2784 if (NIL_P(tmp)) {
2785 if (RB_TYPE_P(val, T_ARRAY) && RARRAY_LEN(val) == 0)
2786 return rb_hash_new();
2787 rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
2788 }
2789 return tmp;
2790 }
2791
2792 /*
2793 * call-seq:
2794 * Hash(arg) -> hash
2795 *
2796 * Converts <i>arg</i> to a <code>Hash</code> by calling
2797 * <i>arg</i><code>.to_hash</code>. Returns an empty <code>Hash</code> when
2798 * <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>.
2799 *
2800 * Hash([]) #=> {}
2801 * Hash(nil) #=> nil
2802 * Hash(key: :value) #=> {:key => :value}
2803 * Hash([1, 2, 3]) #=> TypeError
2804 */
2805
2806 static VALUE
2807 rb_f_hash(VALUE obj, VALUE arg)
2808 {
2809 return rb_Hash(arg);
2810 }
2811
2812 /*
2813 * Document-class: Class
2814 *
2815 * Classes in Ruby are first-class objects---each is an instance of
2816 * class <code>Class</code>.
2817 *
2818 * Typically, you create a new class by using:
2819 *
2820 * class Name
2821 * # some class describing the class behavior
2822 * end
2823 *
2824 * When a new class is created, an object of type Class is initialized and
2825 * assigned to a global constant (<code>Name</code> in this case).
2826 *
2827 * When <code>Name.new</code> is called to create a new object, the
2828 * <code>new</code> method in <code>Class</code> is run by default.
2829 * This can be demonstrated by overriding <code>new</code> in
2830 * <code>Class</code>:
2831 *
2832 * class Class
2833 * alias oldNew new
2834 * def new(*args)
2835 * print "Creating a new ", self.name, "\n"
2836 * oldNew(*args)
2837 * end
2838 * end
2839 *
2840 *
2841 * class Name
2842 * end
2843 *
2844 *
2845 * n = Name.new
2846 *
2847 * <em>produces:</em>
2848 *
2849 * Creating a new Name
2850 *
2851 * Classes, modules, and objects are interrelated. In the diagram
2852 * that follows, the vertical arrows represent inheritance, and the
2853 * parentheses meta-classes. All metaclasses are instances
2854 * of the class `Class'.
2855 * +---------+ +-...
2856 * | | |
2857 * BasicObject-----|-->(BasicObject)-------|-...
2858 * ^ | ^ |
2859 * | | | |
2860 * Object---------|----->(Object)---------|-...
2861 * ^ | ^ |
2862 * | | | |
2863 * +-------+ | +--------+ |
2864 * | | | | | |
2865 * | Module-|---------|--->(Module)-|-...
2866 * | ^ | | ^ |
2867 * | | | | | |
2868 * | Class-|---------|---->(Class)-|-...
2869 * | ^ | | ^ |
2870 * | +---+ | +----+
2871 * | |
2872 * obj--->OtherClass---------->(OtherClass)-----------...
2873 *
2874 */
2875
2876
2895 /* Document-class: BasicObject
2896 *
2897 * BasicObject is the parent class of all classes in Ruby. It's an explicit
2898 * blank class.
2899 *
2900 * BasicObject can be used for creating object hierarchies independent of
2901 * Ruby's object hierarchy, proxy objects like the Delegator class, or other
2902 * uses where namespace pollution from Ruby's methods and classes must be
2903 * avoided.
2904 *
2905 * To avoid polluting BasicObject for other users an appropriately named
2906 * subclass of BasicObject should be created instead of directly modifying
2907 * BasicObject:
2908 *
2909 * class MyObjectSystem < BasicObject
2910 * end
2911 *
2912 * BasicObject does not include Kernel (for methods like +puts+) and
2913 * BasicObject is outside of the namespace of the standard library so common
2914 * classes will not be found without a using a full class path.
2915 *
2916 * A variety of strategies can be used to provide useful portions of the
2917 * standard library to subclasses of BasicObject. A subclass could
2918 * <code>include Kernel</code> to obtain +puts+, +exit+, etc. A custom
2919 * Kernel-like module could be created and included or delegation can be used
2920 * via #method_missing:
2921 *
2922 * class MyObjectSystem < BasicObject
2923 * DELEGATE = [:puts, :p]
2924 *
2925 * def method_missing(name, *args, &block)
2926 * super unless DELEGATE.include? name
2927 * ::Kernel.send(name, *args, &block)
2928 * end
2929 *
2930 * def respond_to_missing?(name, include_private = false)
2931 * DELEGATE.include?(name) or super
2932 * end
2933 * end
2934 *
2935 * Access to classes and modules from the Ruby standard library can be
2936 * obtained in a BasicObject subclass by referencing the desired constant
2937 * from the root like <code>::File</code> or <code>::Enumerator</code>.
2938 * Like #method_missing, #const_missing can be used to delegate constant
2939 * lookup to +Object+:
2940 *
2941 * class MyObjectSystem < BasicObject
2942 * def self.const_missing(name)
2943 * ::Object.const_get(name)
2944 * end
2945 * end
2946 */
2947
2948 /* Document-class: Object
2949 *
2950 * Object is the default root of all Ruby objects. Object inherits from
2951 * BasicObject which allows creating alternate object hierarchies. Methods
2952 * on object are available to all classes unless explicitly overridden.
2953 *
2954 * Object mixes in the Kernel module, making the built-in kernel functions
2955 * globally accessible. Although the instance methods of Object are defined
2956 * by the Kernel module, we have chosen to document them here for clarity.
2957 *
2958 * When referencing constants in classes inheriting from Object you do not
2959 * need to use the full namespace. For example, referencing +File+ inside
2960 * +YourClass+ will find the top-level File class.
2961 *
2962 * In the descriptions of Object's methods, the parameter <i>symbol</i> refers
2963 * to a symbol, which is either a quoted string or a Symbol (such as
2964 * <code>:name</code>).
2965 */
2966
2967 void
2968 Init_Object(void)
2969 {
2970 int i;
2971
2972 Init_class_hierarchy();
2973
2974 #if 0
2975 // teach RDoc about these classes
2976 rb_cBasicObject = rb_define_class("BasicObject", Qnil);
2977 rb_cObject = rb_define_class("Object", rb_cBasicObject);
2978 rb_cModule = rb_define_class("Module", rb_cObject);
2979 rb_cClass = rb_define_class("Class", rb_cModule);
2980 #endif
2981
2982 #undef rb_intern
2983 #define rb_intern(str) rb_intern_const(str)
2984
2985 rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0);
2986 rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
2987 rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
2988 rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
2989 rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
2990 rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);
2991
2992 rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1);
2993 rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1);
2994 rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
2995
2996 /* Document-module: Kernel
2997 *
2998 * The Kernel module is included by class Object, so its methods are
2999 * available in every Ruby object.
3000 *
3001 * The Kernel instance methods are documented in class Object while the
3002 * module methods are documented here. These methods are called without a
3003 * receiver and thus can be called in functional form:
3004 *
3005 * sprintf "%.1f", 1.234 #=> "1.2"
3006 *
3007 */
3008 rb_mKernel = rb_define_module("Kernel");
3009 rb_include_module(rb_cObject, rb_mKernel);
3010 rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1);
3011 rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1);
3012 rb_define_private_method(rb_cModule, "extended", rb_obj_dummy, 1);
3013 rb_define_private_method(rb_cModule, "prepended", rb_obj_dummy, 1);
3014 rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
3015 rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
3016 rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
3017
3018 rb_define_method(rb_mKernel, "nil?", rb_false, 0);
3019 rb_define_method(rb_mKernel, "===", rb_equal, 1);
3020 rb_define_method(rb_mKernel, "=~", rb_obj_match, 1);
3021 rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
3022 rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
3023 rb_define_method(rb_mKernel, "hash", rb_obj_hash, 0);
3024 rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
3025
3026 rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
3027 rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
3028 rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
3029 rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
3030 rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
3031 rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
3032 rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1);
3033
3034 rb_define_method(rb_mKernel, "taint", rb_obj_taint, 0);
3035 rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
3036 rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
3037 rb_define_method(rb_mKernel, "untrust", rb_obj_untrust, 0);
3038 rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0);
3039 rb_define_method(rb_mKernel, "trust", rb_obj_trust, 0);
3040 rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
3041 rb_define_method(rb_mKernel, "frozen?", rb_obj_frozen_p, 0);
3042
3043 rb_define_method(rb_mKernel, "to_s", rb_any_to_s, 0);
3044 rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
3045 rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */
3046 rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
3047 rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
3048 rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */
3049 rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */
3050 rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
3051 rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
3052 rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
3053 rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
3054 rb_define_method(rb_mKernel, "remove_instance_variable",
3055 rb_obj_remove_instance_variable, 1); /* in variable.c */
3056
3057 rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
3058 rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
3059 rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
3060 rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
3061
3062 rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */
3063 rb_define_global_function("format", rb_f_sprintf, -1); /* in sprintf.c */
3064
3065 rb_define_global_function("Integer", rb_f_integer, -1);
3066 rb_define_global_function("Float", rb_f_float, 1);
3067
3068 rb_define_global_function("String", rb_f_string, 1);
3069 rb_define_global_function("Array", rb_f_array, 1);
3070 rb_define_global_function("Hash", rb_f_hash, 1);
3071
3072 rb_cNilClass = rb_define_class("NilClass", rb_cObject);
3073 rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
3074 rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
3075 rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
3076 rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
3077 rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0);
3078 rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
3079 rb_define_method(rb_cNilClass, "&", false_and, 1);
3080 rb_define_method(rb_cNilClass, "|", false_or, 1);
3081 rb_define_method(rb_cNilClass, "^", false_xor, 1);
3082
3083 rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
3084 rb_undef_alloc_func(rb_cNilClass);
3085 rb_undef_method(CLASS_OF(rb_cNilClass), "new");
3086 /*
3087 * An alias of +nil+
3088 */
3089 rb_define_global_const("NIL", Qnil);
3090
3091 rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
3092 rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
3093 rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
3094 rb_define_method(rb_cModule, "<=>", rb_mod_cmp, 1);
3095 rb_define_method(rb_cModule, "<", rb_mod_lt, 1);
3096 rb_define_method(rb_cModule, "<=", rb_class_inherited_p, 1);
3097 rb_define_method(rb_cModule, ">", rb_mod_gt, 1);
3098 rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
3099 rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
3100 rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
3101 rb_define_alias(rb_cModule, "inspect", "to_s");
3102 rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
3103 rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
3104 rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */
3105 rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
3106
3107 rb_define_private_method(rb_cModule, "attr", rb_mod_attr, -1);
3108 rb_define_private_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
3109 rb_define_private_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
3110 rb_define_private_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
3111
3112 rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
3113 rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
3114 rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
3115 rb_define_method(rb_cModule, "public_instance_methods",
3116 rb_class_public_instance_methods, -1); /* in class.c */
3117 rb_define_method(rb_cModule, "protected_instance_methods",
3118 rb_class_protected_instance_methods, -1); /* in class.c */
3119 rb_define_method(rb_cModule, "private_instance_methods",
3120 rb_class_private_instance_methods, -1); /* in class.c */
3121
3122 rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
3123 rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
3124 rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
3125 rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
3126 rb_define_private_method(rb_cModule, "remove_const",
3127 rb_mod_remove_const, 1); /* in variable.c */
3128 rb_define_method(rb_cModule, "const_missing",
3129 rb_mod_const_missing, 1); /* in variable.c */
3130 rb_define_method(rb_cModule, "class_variables",
3131 rb_mod_class_variables, -1); /* in variable.c */
3132 rb_define_method(rb_cModule, "remove_class_variable",
3133 rb_mod_remove_cvar, 1); /* in variable.c */
3134 rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
3135 rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
3136 rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
3137 rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); /* in variable.c */
3138 rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); /* in variable.c */
3139
3140 rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
3141 rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
3142 rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
3143 rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
3144 rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
3145 rb_undef_method(rb_cClass, "extend_object");
3146 rb_undef_method(rb_cClass, "append_features");
3147
3148 /*
3149 * Document-class: Data
3150 *
3151 * This is a recommended base class for C extensions using Data_Make_Struct
3152 * or Data_Wrap_Struct, see README.EXT for details.
3153 */
3154 rb_cData = rb_define_class("Data", rb_cObject);
3155 rb_undef_alloc_func(rb_cData);
3156
3157 rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
3158 rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
3159 rb_define_alias(rb_cTrueClass, "inspect", "to_s");
3160 rb_define_method(rb_cTrueClass, "&", true_and, 1);
3161 rb_define_method(rb_cTrueClass, "|", true_or, 1);
3162 rb_define_method(rb_cTrueClass, "^", true_xor, 1);
3163 rb_undef_alloc_func(rb_cTrueClass);
3164 rb_undef_method(CLASS_OF(rb_cTrueClass), "new");
3165 /*
3166 * An alias of +true+
3167 */
3168 rb_define_global_const("TRUE", Qtrue);
3169
3170 rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
3171 rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
3172 rb_define_alias(rb_cFalseClass, "inspect", "to_s");
3173 rb_define_method(rb_cFalseClass, "&", false_and, 1);
3174 rb_define_method(rb_cFalseClass, "|", false_or, 1);
3175 rb_define_method(rb_cFalseClass, "^", false_xor, 1);
3176 rb_undef_alloc_func(rb_cFalseClass);
3177 rb_undef_method(CLASS_OF(rb_cFalseClass), "new");
3178 /*
3179 * An alias of +false+
3180 */
3181 rb_define_global_const("FALSE", Qfalse);
3182
3183 id_eq = rb_intern("==");
3184 id_eql = rb_intern("eql?");
3185 id_match = rb_intern("=~");
3186 id_inspect = rb_intern("inspect");
3187 id_init_copy = rb_intern("initialize_copy");
3188 id_init_clone = rb_intern("initialize_clone");
3189 id_init_dup = rb_intern("initialize_dup");
3190 id_const_missing = rb_intern("const_missing");
3191
3192 for (i=0; conv_method_names[i].method; i++) {
3193 conv_method_names[i].id = rb_intern(conv_method_names[i].method);
3194 }
3195 }
3196