Ruby 2.0.0p353(2013-11-22revision43784)

- 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 * Document-method: extended
776 *
777 * call-seq:
778 * extended(othermod)
779 *
780 * The equivalent of <tt>included</tt>, but for extended modules.
781 *
782 * module A
783 * def self.extended(mod)
784 * puts "#{self} extended in #{mod}"
785 * end
786 * end
787 * module Enumerable
788 * extend A
789 * end
790 * # => prints "A extended in Enumerable"
791 */
792
793 /*
794 * Document-method: included
795 *
796 * call-seq:
797 * included(othermod)
798 *
799 * Callback invoked whenever the receiver is included in another
800 * module or class. This should be used in preference to
801 * <tt>Module.append_features</tt> if your code wants to perform some
802 * action when a module is included in another.
803 *
804 * module A
805 * def A.included(mod)
806 * puts "#{self} included in #{mod}"
807 * end
808 * end
809 * module Enumerable
810 * include A
811 * end
812 * # => prints "A included in Enumerable"
813 */
814
815 /*
816 * Document-method: prepended
817 *
818 * call-seq:
819 * prepended(othermod)
820 *
821 * The equivalent of <tt>included</tt>, but for prepended modules.
822 *
823 * module A
824 * def self.prepended(mod)
825 * puts "#{self} prepended to #{mod}"
826 * end
827 * end
828 * module Enumerable
829 * prepend A
830 * end
831 * # => prints "A prepended to Enumerable"
832 */
833
834 /*
835 * Document-method: initialize
836 *
837 * call-seq:
838 * BasicObject.new
839 *
840 * Returns a new BasicObject.
841 */
842
843 /*
844 * Not documented
845 */
846
847 static VALUE
848 rb_obj_dummy(void)
849 {
850 return Qnil;
851 }
852
853 /*
854 * call-seq:
855 * obj.tainted? -> true or false
856 *
857 * Returns <code>true</code> if the object is tainted.
858 */
859
860 VALUE
861 rb_obj_tainted(VALUE obj)
862 {
863 if (OBJ_TAINTED(obj))
864 return Qtrue;
865 return Qfalse;
866 }
867
868 /*
869 * call-seq:
870 * obj.taint -> obj
871 *
872 * Marks <i>obj</i> as tainted---if the <code>$SAFE</code> level is
873 * set appropriately, many method calls which might alter the running
874 * programs environment will refuse to accept tainted strings.
875 */
876
877 VALUE
878 rb_obj_taint(VALUE obj)
879 {
880 rb_secure(4);
881 if (!OBJ_TAINTED(obj)) {
882 rb_check_frozen(obj);
883 OBJ_TAINT(obj);
884 }
885 return obj;
886 }
887
888
889 /*
890 * call-seq:
891 * obj.untaint -> obj
892 *
893 * Removes the taint from <i>obj</i>.
894 */
895
896 VALUE
897 rb_obj_untaint(VALUE obj)
898 {
899 rb_secure(3);
900 if (OBJ_TAINTED(obj)) {
901 rb_check_frozen(obj);
902 FL_UNSET(obj, FL_TAINT);
903 }
904 return obj;
905 }
906
907 /*
908 * call-seq:
909 * obj.untrusted? -> true or false
910 *
911 * Returns <code>true</code> if the object is untrusted.
912 */
913
914 VALUE
915 rb_obj_untrusted(VALUE obj)
916 {
917 if (OBJ_UNTRUSTED(obj))
918 return Qtrue;
919 return Qfalse;
920 }
921
922 /*
923 * call-seq:
924 * obj.untrust -> obj
925 *
926 * Marks <i>obj</i> as untrusted.
927 */
928
929 VALUE
930 rb_obj_untrust(VALUE obj)
931 {
932 rb_secure(4);
933 if (!OBJ_UNTRUSTED(obj)) {
934 rb_check_frozen(obj);
935 OBJ_UNTRUST(obj);
936 }
937 return obj;
938 }
939
940
941 /*
942 * call-seq:
943 * obj.trust -> obj
944 *
945 * Removes the untrusted mark from <i>obj</i>.
946 */
947
948 VALUE
949 rb_obj_trust(VALUE obj)
950 {
951 rb_secure(3);
952 if (OBJ_UNTRUSTED(obj)) {
953 rb_check_frozen(obj);
954 FL_UNSET(obj, FL_UNTRUSTED);
955 }
956 return obj;
957 }
958
959 void
960 rb_obj_infect(VALUE obj1, VALUE obj2)
961 {
962 OBJ_INFECT(obj1, obj2);
963 }
964
965 static st_table *immediate_frozen_tbl = 0;
966
967 /*
968 * call-seq:
969 * obj.freeze -> obj
970 *
971 * Prevents further modifications to <i>obj</i>. A
972 * <code>RuntimeError</code> will be raised if modification is attempted.
973 * There is no way to unfreeze a frozen object. See also
974 * <code>Object#frozen?</code>.
975 *
976 * This method returns self.
977 *
978 * a = [ "a", "b", "c" ]
979 * a.freeze
980 * a << "z"
981 *
982 * <em>produces:</em>
983 *
984 * prog.rb:3:in `<<': can't modify frozen array (RuntimeError)
985 * from prog.rb:3
986 */
987
988 VALUE
989 rb_obj_freeze(VALUE obj)
990 {
991 if (!OBJ_FROZEN(obj)) {
992 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(obj)) {
993 rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
994 }
995 OBJ_FREEZE(obj);
996 if (SPECIAL_CONST_P(obj)) {
997 if (!immediate_frozen_tbl) {
998 immediate_frozen_tbl = st_init_numtable();
999 }
1000 st_insert(immediate_frozen_tbl, obj, (st_data_t)Qtrue);
1001 }
1002 }
1003 return obj;
1004 }
1005
1006 /*
1007 * call-seq:
1008 * obj.frozen? -> true or false
1009 *
1010 * Returns the freeze status of <i>obj</i>.
1011 *
1012 * a = [ "a", "b", "c" ]
1013 * a.freeze #=> ["a", "b", "c"]
1014 * a.frozen? #=> true
1015 */
1016
1017 VALUE
1018 rb_obj_frozen_p(VALUE obj)
1019 {
1020 if (OBJ_FROZEN(obj)) return Qtrue;
1021 if (SPECIAL_CONST_P(obj)) {
1022 if (!immediate_frozen_tbl) return Qfalse;
1023 if (st_lookup(immediate_frozen_tbl, obj, 0)) return Qtrue;
1024 }
1025 return Qfalse;
1026 }
1027
1028
1029 /*
1030 * Document-class: NilClass
1031 *
1032 * The class of the singleton object <code>nil</code>.
1033 */
1034
1035 /*
1036 * call-seq:
1037 * nil.to_i -> 0
1038 *
1039 * Always returns zero.
1040 *
1041 * nil.to_i #=> 0
1042 */
1043
1044
1045 static VALUE
1046 nil_to_i(VALUE obj)
1047 {
1048 return INT2FIX(0);
1049 }
1050
1051 /*
1052 * call-seq:
1053 * nil.to_f -> 0.0
1054 *
1055 * Always returns zero.
1056 *
1057 * nil.to_f #=> 0.0
1058 */
1059
1060 static VALUE
1061 nil_to_f(VALUE obj)
1062 {
1063 return DBL2NUM(0.0);
1064 }
1065
1066 /*
1067 * call-seq:
1068 * nil.to_s -> ""
1069 *
1070 * Always returns the empty string.
1071 */
1072
1073 static VALUE
1074 nil_to_s(VALUE obj)
1075 {
1076 return rb_usascii_str_new(0, 0);
1077 }
1078
1079 /*
1080 * Document-method: to_a
1081 *
1082 * call-seq:
1083 * nil.to_a -> []
1084 *
1085 * Always returns an empty array.
1086 *
1087 * nil.to_a #=> []
1088 */
1089
1090 static VALUE
1091 nil_to_a(VALUE obj)
1092 {
1093 return rb_ary_new2(0);
1094 }
1095
1096 /*
1097 * Document-method: to_h
1098 *
1099 * call-seq:
1100 * nil.to_h -> {}
1101 *
1102 * Always returns an empty hash.
1103 *
1104 * nil.to_h #=> {}
1105 */
1106
1107 static VALUE
1108 nil_to_h(VALUE obj)
1109 {
1110 return rb_hash_new();
1111 }
1112
1113 /*
1114 * call-seq:
1115 * nil.inspect -> "nil"
1116 *
1117 * Always returns the string "nil".
1118 */
1119
1120 static VALUE
1121 nil_inspect(VALUE obj)
1122 {
1123 return rb_usascii_str_new2("nil");
1124 }
1125
1126 /***********************************************************************
1127 * Document-class: TrueClass
1128 *
1129 * The global value <code>true</code> is the only instance of class
1130 * <code>TrueClass</code> and represents a logically true value in
1131 * boolean expressions. The class provides operators allowing
1132 * <code>true</code> to be used in logical expressions.
1133 */
1134
1135
1136 /*
1137 * call-seq:
1138 * true.to_s -> "true"
1139 *
1140 * The string representation of <code>true</code> is "true".
1141 */
1142
1143 static VALUE
1144 true_to_s(VALUE obj)
1145 {
1146 return rb_usascii_str_new2("true");
1147 }
1148
1149
1150 /*
1151 * call-seq:
1152 * true & obj -> true or false
1153 *
1154 * And---Returns <code>false</code> if <i>obj</i> is
1155 * <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
1156 */
1157
1158 static VALUE
1159 true_and(VALUE obj, VALUE obj2)
1160 {
1161 return RTEST(obj2)?Qtrue:Qfalse;
1162 }
1163
1164 /*
1165 * call-seq:
1166 * true | obj -> true
1167 *
1168 * Or---Returns <code>true</code>. As <i>anObject</i> is an argument to
1169 * a method call, it is always evaluated; there is no short-circuit
1170 * evaluation in this case.
1171 *
1172 * true | puts("or")
1173 * true || puts("logical or")
1174 *
1175 * <em>produces:</em>
1176 *
1177 * or
1178 */
1179
1180 static VALUE
1181 true_or(VALUE obj, VALUE obj2)
1182 {
1183 return Qtrue;
1184 }
1185
1186
1187 /*
1188 * call-seq:
1189 * true ^ obj -> !obj
1190 *
1191 * Exclusive Or---Returns <code>true</code> if <i>obj</i> is
1192 * <code>nil</code> or <code>false</code>, <code>false</code>
1193 * otherwise.
1194 */
1195
1196 static VALUE
1197 true_xor(VALUE obj, VALUE obj2)
1198 {
1199 return RTEST(obj2)?Qfalse:Qtrue;
1200 }
1201
1202
1203 /*
1204 * Document-class: FalseClass
1205 *
1206 * The global value <code>false</code> is the only instance of class
1207 * <code>FalseClass</code> and represents a logically false value in
1208 * boolean expressions. The class provides operators allowing
1209 * <code>false</code> to participate correctly in logical expressions.
1210 *
1211 */
1212
1213 /*
1214 * call-seq:
1215 * false.to_s -> "false"
1216 *
1217 * 'nuf said...
1218 */
1219
1220 static VALUE
1221 false_to_s(VALUE obj)
1222 {
1223 return rb_usascii_str_new2("false");
1224 }
1225
1226 /*
1227 * call-seq:
1228 * false & obj -> false
1229 * nil & obj -> false
1230 *
1231 * And---Returns <code>false</code>. <i>obj</i> is always
1232 * evaluated as it is the argument to a method call---there is no
1233 * short-circuit evaluation in this case.
1234 */
1235
1236 static VALUE
1237 false_and(VALUE obj, VALUE obj2)
1238 {
1239 return Qfalse;
1240 }
1241
1242
1243 /*
1244 * call-seq:
1245 * false | obj -> true or false
1246 * nil | obj -> true or false
1247 *
1248 * Or---Returns <code>false</code> if <i>obj</i> is
1249 * <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
1250 */
1251
1252 static VALUE
1253 false_or(VALUE obj, VALUE obj2)
1254 {
1255 return RTEST(obj2)?Qtrue:Qfalse;
1256 }
1257
1258
1259
1260 /*
1261 * call-seq:
1262 * false ^ obj -> true or false
1263 * nil ^ obj -> true or false
1264 *
1265 * Exclusive Or---If <i>obj</i> is <code>nil</code> or
1266 * <code>false</code>, returns <code>false</code>; otherwise, returns
1267 * <code>true</code>.
1268 *
1269 */
1270
1271 static VALUE
1272 false_xor(VALUE obj, VALUE obj2)
1273 {
1274 return RTEST(obj2)?Qtrue:Qfalse;
1275 }
1276
1277 /*
1278 * call_seq:
1279 * nil.nil? -> true
1280 *
1281 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1282 */
1283
1284 static VALUE
1285 rb_true(VALUE obj)
1286 {
1287 return Qtrue;
1288 }
1289
1290 /*
1291 * call_seq:
1292 * nil.nil? -> true
1293 * <anything_else>.nil? -> false
1294 *
1295 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1296 */
1297
1298
1299 static VALUE
1300 rb_false(VALUE obj)
1301 {
1302 return Qfalse;
1303 }
1304
1305
1306 /*
1307 * call-seq:
1308 * obj =~ other -> nil
1309 *
1310 * Pattern Match---Overridden by descendants (notably
1311 * <code>Regexp</code> and <code>String</code>) to provide meaningful
1312 * pattern-match semantics.
1313 */
1314
1315 static VALUE
1316 rb_obj_match(VALUE obj1, VALUE obj2)
1317 {
1318 return Qnil;
1319 }
1320
1321 /*
1322 * call-seq:
1323 * obj !~ other -> true or false
1324 *
1325 * Returns true if two objects do not match (using the <i>=~</i>
1326 * method), otherwise false.
1327 */
1328
1329 static VALUE
1330 rb_obj_not_match(VALUE obj1, VALUE obj2)
1331 {
1332 VALUE result = rb_funcall(obj1, id_match, 1, obj2);
1333 return RTEST(result) ? Qfalse : Qtrue;
1334 }
1335
1336
1337 /*
1338 * call-seq:
1339 * obj <=> other -> 0 or nil
1340 *
1341 * Returns 0 if +obj+ and +other+ are the same object
1342 * or <code>obj == other</code>, otherwise nil.
1343 *
1344 * The <=> is used by various methods to compare objects, for example
1345 * Enumerable#sort, Enumerable#max etc.
1346 *
1347 * Your implementation of <=> should return one of the following values: -1, 0,
1348 * 1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
1349 * 1 means self is bigger than other. Nil means the two values could not be
1350 * compared.
1351 *
1352 * When you define <=>, you can include Comparable to gain the methods <=, <,
1353 * ==, >=, > and between?.
1354 */
1355 static VALUE
1356 rb_obj_cmp(VALUE obj1, VALUE obj2)
1357 {
1358 if (obj1 == obj2 || rb_equal(obj1, obj2))
1359 return INT2FIX(0);
1360 return Qnil;
1361 }
1362
1363 /***********************************************************************
1364 *
1365 * Document-class: Module
1366 *
1367 * A <code>Module</code> is a collection of methods and constants. The
1368 * methods in a module may be instance methods or module methods.
1369 * Instance methods appear as methods in a class when the module is
1370 * included, module methods do not. Conversely, module methods may be
1371 * called without creating an encapsulating object, while instance
1372 * methods may not. (See <code>Module#module_function</code>)
1373 *
1374 * In the descriptions that follow, the parameter <i>sym</i> refers
1375 * to a symbol, which is either a quoted string or a
1376 * <code>Symbol</code> (such as <code>:name</code>).
1377 *
1378 * module Mod
1379 * include Math
1380 * CONST = 1
1381 * def meth
1382 * # ...
1383 * end
1384 * end
1385 * Mod.class #=> Module
1386 * Mod.constants #=> [:CONST, :PI, :E]
1387 * Mod.instance_methods #=> [:meth]
1388 *
1389 */
1390
1391 /*
1392 * call-seq:
1393 * mod.to_s -> string
1394 *
1395 * Return a string representing this module or class. For basic
1396 * classes and modules, this is the name. For singletons, we
1397 * show information on the thing we're attached to as well.
1398 */
1399
1400 static VALUE
1401 rb_mod_to_s(VALUE klass)
1402 {
1403 ID id_defined_at;
1404 VALUE refined_class, defined_at;
1405
1406 if (FL_TEST(klass, FL_SINGLETON)) {
1407 VALUE s = rb_usascii_str_new2("#<Class:");
1408 VALUE v = rb_iv_get(klass, "__attached__");
1409
1410 if (CLASS_OR_MODULE_P(v)) {
1411 rb_str_append(s, rb_inspect(v));
1412 }
1413 else {
1414 rb_str_append(s, rb_any_to_s(v));
1415 }
1416 rb_str_cat2(s, ">");
1417
1418 return s;
1419 }
1420 refined_class = rb_refinement_module_get_refined_class(klass);
1421 if (!NIL_P(refined_class)) {
1422 VALUE s = rb_usascii_str_new2("#<refinement:");
1423
1424 rb_str_concat(s, rb_inspect(refined_class));
1425 rb_str_cat2(s, "@");
1426 CONST_ID(id_defined_at, "__defined_at__");
1427 defined_at = rb_attr_get(klass, id_defined_at);
1428 rb_str_concat(s, rb_inspect(defined_at));
1429 rb_str_cat2(s, ">");
1430 return s;
1431 }
1432 return rb_str_dup(rb_class_name(klass));
1433 }
1434
1435 /*
1436 * call-seq:
1437 * mod.freeze -> mod
1438 *
1439 * Prevents further modifications to <i>mod</i>.
1440 *
1441 * This method returns self.
1442 */
1443
1444 static VALUE
1445 rb_mod_freeze(VALUE mod)
1446 {
1447 rb_class_name(mod);
1448 return rb_obj_freeze(mod);
1449 }
1450
1451 /*
1452 * call-seq:
1453 * mod === obj -> true or false
1454 *
1455 * Case Equality---Returns <code>true</code> if <i>anObject</i> is an
1456 * instance of <i>mod</i> or one of <i>mod</i>'s descendants. Of
1457 * limited use for modules, but can be used in <code>case</code>
1458 * statements to classify objects by class.
1459 */
1460
1461 static VALUE
1462 rb_mod_eqq(VALUE mod, VALUE arg)
1463 {
1464 return rb_obj_is_kind_of(arg, mod);
1465 }
1466
1467 /*
1468 * call-seq:
1469 * mod <= other -> true, false, or nil
1470 *
1471 * Returns true if <i>mod</i> is a subclass of <i>other</i> or
1472 * is the same as <i>other</i>. Returns
1473 * <code>nil</code> if there's no relationship between the two.
1474 * (Think of the relationship in terms of the class definition:
1475 * "class A<B" implies "A<B").
1476 *
1477 */
1478
1479 VALUE
1480 rb_class_inherited_p(VALUE mod, VALUE arg)
1481 {
1482 VALUE start = mod;
1483
1484 if (mod == arg) return Qtrue;
1485 if (!CLASS_OR_MODULE_P(arg) && !RB_TYPE_P(arg, T_ICLASS)) {
1486 rb_raise(rb_eTypeError, "compared with non class/module");
1487 }
1488 arg = RCLASS_ORIGIN(arg);
1489 while (mod) {
1490 if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg))
1491 return Qtrue;
1492 mod = RCLASS_SUPER(mod);
1493 }
1494 /* not mod < arg; check if mod > arg */
1495 while (arg) {
1496 if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start))
1497 return Qfalse;
1498 arg = RCLASS_SUPER(arg);
1499 }
1500 return Qnil;
1501 }
1502
1503 /*
1504 * call-seq:
1505 * mod < other -> true, false, or nil
1506 *
1507 * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
1508 * <code>nil</code> if there's no relationship between the two.
1509 * (Think of the relationship in terms of the class definition:
1510 * "class A<B" implies "A<B").
1511 *
1512 */
1513
1514 static VALUE
1515 rb_mod_lt(VALUE mod, VALUE arg)
1516 {
1517 if (mod == arg) return Qfalse;
1518 return rb_class_inherited_p(mod, arg);
1519 }
1520
1521
1522 /*
1523 * call-seq:
1524 * mod >= other -> true, false, or nil
1525 *
1526 * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
1527 * two modules are the same. Returns
1528 * <code>nil</code> if there's no relationship between the two.
1529 * (Think of the relationship in terms of the class definition:
1530 * "class A<B" implies "B>A").
1531 *
1532 */
1533
1534 static VALUE
1535 rb_mod_ge(VALUE mod, VALUE arg)
1536 {
1537 if (!CLASS_OR_MODULE_P(arg)) {
1538 rb_raise(rb_eTypeError, "compared with non class/module");
1539 }
1540
1541 return rb_class_inherited_p(arg, mod);
1542 }
1543
1544 /*
1545 * call-seq:
1546 * mod > other -> true, false, or nil
1547 *
1548 * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
1549 * <code>nil</code> if there's no relationship between the two.
1550 * (Think of the relationship in terms of the class definition:
1551 * "class A<B" implies "B>A").
1552 *
1553 */
1554
1555 static VALUE
1556 rb_mod_gt(VALUE mod, VALUE arg)
1557 {
1558 if (mod == arg) return Qfalse;
1559 return rb_mod_ge(mod, arg);
1560 }
1561
1562 /*
1563 * call-seq:
1564 * module <=> other_module -> -1, 0, +1, or nil
1565 *
1566 * Comparison---Returns -1, 0, +1 or nil depending on whether +module+
1567 * includes +other_module+, they are the same, or if +module+ is included by
1568 * +other_module+. This is the basis for the tests in Comparable.
1569 *
1570 * Returns +nil+ if +module+ has no relationship with +other_module+, if
1571 * +other_module+ is not a module, or if the two values are incomparable.
1572 */
1573
1574 static VALUE
1575 rb_mod_cmp(VALUE mod, VALUE arg)
1576 {
1577 VALUE cmp;
1578
1579 if (mod == arg) return INT2FIX(0);
1580 if (!CLASS_OR_MODULE_P(arg)) {
1581 return Qnil;
1582 }
1583
1584 cmp = rb_class_inherited_p(mod, arg);
1585 if (NIL_P(cmp)) return Qnil;
1586 if (cmp) {
1587 return INT2FIX(-1);
1588 }
1589 return INT2FIX(1);
1590 }
1591
1592 static VALUE
1593 rb_module_s_alloc(VALUE klass)
1594 {
1595 VALUE mod = rb_module_new();
1596
1597 RBASIC(mod)->klass = klass;
1598 return mod;
1599 }
1600
1601 static VALUE
1602 rb_class_s_alloc(VALUE klass)
1603 {
1604 return rb_class_boot(0);
1605 }
1606
1607 /*
1608 * call-seq:
1609 * Module.new -> mod
1610 * Module.new {|mod| block } -> mod
1611 *
1612 * Creates a new anonymous module. If a block is given, it is passed
1613 * the module object, and the block is evaluated in the context of this
1614 * module using <code>module_eval</code>.
1615 *
1616 * fred = Module.new do
1617 * def meth1
1618 * "hello"
1619 * end
1620 * def meth2
1621 * "bye"
1622 * end
1623 * end
1624 * a = "my string"
1625 * a.extend(fred) #=> "my string"
1626 * a.meth1 #=> "hello"
1627 * a.meth2 #=> "bye"
1628 *
1629 * Assign the module to a constant (name starting uppercase) if you
1630 * want to treat it like a regular module.
1631 */
1632
1633 static VALUE
1634 rb_mod_initialize(VALUE module)
1635 {
1636 if (rb_block_given_p()) {
1637 rb_mod_module_exec(1, &module, module);
1638 }
1639 return Qnil;
1640 }
1641
1642 /*
1643 * call-seq:
1644 * Class.new(super_class=Object) -> a_class
1645 * Class.new(super_class=Object) { |mod| ... } -> a_class
1646 *
1647 * Creates a new anonymous (unnamed) class with the given superclass
1648 * (or <code>Object</code> if no parameter is given). You can give a
1649 * class a name by assigning the class object to a constant.
1650 *
1651 * If a block is given, it is passed the class object, and the block
1652 * is evaluated in the context of this class using
1653 * <code>class_eval</code>.
1654 *
1655 * fred = Class.new do
1656 * def meth1
1657 * "hello"
1658 * end
1659 * def meth2
1660 * "bye"
1661 * end
1662 * end
1663 *
1664 * a = fred.new #=> #<#<Class:0x100381890>:0x100376b98>
1665 * a.meth1 #=> "hello"
1666 * a.meth2 #=> "bye"
1667 *
1668 * Assign the class to a constant (name starting uppercase) if you
1669 * want to treat it like a regular class.
1670 */
1671
1672 static VALUE
1673 rb_class_initialize(int argc, VALUE *argv, VALUE klass)
1674 {
1675 VALUE super;
1676
1677 if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
1678 rb_raise(rb_eTypeError, "already initialized class");
1679 }
1680 if (argc == 0) {
1681 super = rb_cObject;
1682 }
1683 else {
1684 rb_scan_args(argc, argv, "01", &super);
1685 rb_check_inheritable(super);
1686 if (super != rb_cBasicObject && !RCLASS_SUPER(super)) {
1687 rb_raise(rb_eTypeError, "can't inherit uninitialized class");
1688 }
1689 }
1690 RCLASS_SUPER(klass) = super;
1691 rb_make_metaclass(klass, RBASIC(super)->klass);
1692 rb_class_inherited(super, klass);
1693 rb_mod_initialize(klass);
1694
1695 return klass;
1696 }
1697
1698 /*
1699 * call-seq:
1700 * class.allocate() -> obj
1701 *
1702 * Allocates space for a new object of <i>class</i>'s class and does not
1703 * call initialize on the new instance. The returned object must be an
1704 * instance of <i>class</i>.
1705 *
1706 * klass = Class.new do
1707 * def initialize(*args)
1708 * @initialized = true
1709 * end
1710 *
1711 * def initialized?
1712 * @initialized || false
1713 * end
1714 * end
1715 *
1716 * klass.allocate.initialized? #=> false
1717 *
1718 */
1719
1720 VALUE
1721 rb_obj_alloc(VALUE klass)
1722 {
1723 VALUE obj;
1724 rb_alloc_func_t allocator;
1725
1726 if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
1727 rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
1728 }
1729 if (FL_TEST(klass, FL_SINGLETON)) {
1730 rb_raise(rb_eTypeError, "can't create instance of singleton class");
1731 }
1732 allocator = rb_get_alloc_func(klass);
1733 if (!allocator) {
1734 rb_raise(rb_eTypeError, "allocator undefined for %"PRIsVALUE,
1735 klass);
1736 }
1737
1738 #if !defined(DTRACE_PROBES_DISABLED) || !DTRACE_PROBES_DISABLED
1739 if (RUBY_DTRACE_OBJECT_CREATE_ENABLED()) {
1740 const char * file = rb_sourcefile();
1741 RUBY_DTRACE_OBJECT_CREATE(rb_class2name(klass),
1742 file ? file : "",
1743 rb_sourceline());
1744 }
1745 #endif
1746
1747 obj = (*allocator)(klass);
1748
1749 if (rb_obj_class(obj) != rb_class_real(klass)) {
1750 rb_raise(rb_eTypeError, "wrong instance allocation");
1751 }
1752 return obj;
1753 }
1754
1755 static VALUE
1756 rb_class_allocate_instance(VALUE klass)
1757 {
1758 NEWOBJ_OF(obj, struct RObject, klass, T_OBJECT);
1759 return (VALUE)obj;
1760 }
1761
1762 /*
1763 * call-seq:
1764 * class.new(args, ...) -> obj
1765 *
1766 * Calls <code>allocate</code> to create a new object of
1767 * <i>class</i>'s class, then invokes that object's
1768 * <code>initialize</code> method, passing it <i>args</i>.
1769 * This is the method that ends up getting called whenever
1770 * an object is constructed using .new.
1771 *
1772 */
1773
1774 VALUE
1775 rb_class_new_instance(int argc, VALUE *argv, VALUE klass)
1776 {
1777 VALUE obj;
1778
1779 obj = rb_obj_alloc(klass);
1780 rb_obj_call_init(obj, argc, argv);
1781
1782 return obj;
1783 }
1784
1785 /*
1786 * call-seq:
1787 * class.superclass -> a_super_class or nil
1788 *
1789 * Returns the superclass of <i>class</i>, or <code>nil</code>.
1790 *
1791 * File.superclass #=> IO
1792 * IO.superclass #=> Object
1793 * Object.superclass #=> BasicObject
1794 * class Foo; end
1795 * class Bar < Foo; end
1796 * Bar.superclass #=> Foo
1797 *
1798 * returns nil when the given class hasn't a parent class:
1799 *
1800 * BasicObject.superclass #=> nil
1801 *
1802 */
1803
1804 VALUE
1805 rb_class_superclass(VALUE klass)
1806 {
1807 VALUE super = RCLASS_SUPER(klass);
1808
1809 if (!super) {
1810 if (klass == rb_cBasicObject) return Qnil;
1811 rb_raise(rb_eTypeError, "uninitialized class");
1812 }
1813 while (RB_TYPE_P(super, T_ICLASS)) {
1814 super = RCLASS_SUPER(super);
1815 }
1816 if (!super) {
1817 return Qnil;
1818 }
1819 return super;
1820 }
1821
1822 VALUE
1823 rb_class_get_superclass(VALUE klass)
1824 {
1825 return RCLASS_SUPER(klass);
1826 }
1827
1828 /*
1829 * call-seq:
1830 * attr_reader(symbol, ...) -> nil
1831 * attr(symbol, ...) -> nil
1832 *
1833 * Creates instance variables and corresponding methods that return the
1834 * value of each instance variable. Equivalent to calling
1835 * ``<code>attr</code><i>:name</i>'' on each name in turn.
1836 */
1837
1838 static VALUE
1839 rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
1840 {
1841 int i;
1842
1843 for (i=0; i<argc; i++) {
1844 rb_attr(klass, rb_to_id(argv[i]), TRUE, FALSE, TRUE);
1845 }
1846 return Qnil;
1847 }
1848
1849 VALUE
1850 rb_mod_attr(int argc, VALUE *argv, VALUE klass)
1851 {
1852 if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
1853 rb_warning("optional boolean argument is obsoleted");
1854 rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), TRUE);
1855 return Qnil;
1856 }
1857 return rb_mod_attr_reader(argc, argv, klass);
1858 }
1859
1860 /*
1861 * call-seq:
1862 * attr_writer(symbol, ...) -> nil
1863 *
1864 * Creates an accessor method to allow assignment to the attribute
1865 * <i>symbol</i><code>.id2name</code>.
1866 */
1867
1868 static VALUE
1869 rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
1870 {
1871 int i;
1872
1873 for (i=0; i<argc; i++) {
1874 rb_attr(klass, rb_to_id(argv[i]), FALSE, TRUE, TRUE);
1875 }
1876 return Qnil;
1877 }
1878
1879 /*
1880 * call-seq:
1881 * attr_accessor(symbol, ...) -> nil
1882 *
1883 * Defines a named attribute for this module, where the name is
1884 * <i>symbol.</i><code>id2name</code>, creating an instance variable
1885 * (<code>@name</code>) and a corresponding access method to read it.
1886 * Also creates a method called <code>name=</code> to set the attribute.
1887 *
1888 * module Mod
1889 * attr_accessor(:one, :two)
1890 * end
1891 * Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
1892 */
1893
1894 static VALUE
1895 rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
1896 {
1897 int i;
1898
1899 for (i=0; i<argc; i++) {
1900 rb_attr(klass, rb_to_id(argv[i]), TRUE, TRUE, TRUE);
1901 }
1902 return Qnil;
1903 }
1904
1905 /*
1906 * call-seq:
1907 * mod.const_get(sym, inherit=true) -> obj
1908 * mod.const_get(str, inherit=true) -> obj
1909 *
1910 * Checks for a constant with the given name in <i>mod</i>
1911 * If +inherit+ is set, the lookup will also search
1912 * the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
1913 *
1914 * The value of the constant is returned if a definition is found,
1915 * otherwise a +NameError+ is raised.
1916 *
1917 * Math.const_get(:PI) #=> 3.14159265358979
1918 *
1919 * This method will recursively look up constant names if a namespaced
1920 * class name is provided. For example:
1921 *
1922 * module Foo; class Bar; end end
1923 * Object.const_get 'Foo::Bar'
1924 *
1925 * The +inherit+ flag is respected on each lookup. For example:
1926 *
1927 * module Foo
1928 * class Bar
1929 * VAL = 10
1930 * end
1931 *
1932 * class Baz < Bar; end
1933 * end
1934 *
1935 * Object.const_get 'Foo::Baz::VAL' # => 10
1936 * Object.const_get 'Foo::Baz::VAL', false # => NameError
1937 */
1938
1939 static VALUE
1940 rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
1941 {
1942 VALUE name, recur;
1943 rb_encoding *enc;
1944 const char *pbeg, *p, *path, *pend;
1945 ID id;
1946 int nestable = 1;
1947
1948 if (argc == 1) {
1949 name = argv[0];
1950 recur = Qtrue;
1951 }
1952 else {
1953 rb_scan_args(argc, argv, "11", &name, &recur);
1954 }
1955
1956 if (SYMBOL_P(name)) {
1957 name = rb_sym_to_s(name);
1958 nestable = 0;
1959 }
1960
1961 name = rb_check_string_type(name);
1962 Check_Type(name, T_STRING);
1963
1964 enc = rb_enc_get(name);
1965 path = RSTRING_PTR(name);
1966
1967 if (!rb_enc_asciicompat(enc)) {
1968 rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
1969 }
1970
1971 pbeg = p = path;
1972 pend = path + RSTRING_LEN(name);
1973
1974 if (p >= pend || !*p) {
1975 wrong_name:
1976 rb_raise(rb_eNameError, "wrong constant name %"PRIsVALUE,
1977 QUOTE(name));
1978 }
1979
1980 if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
1981 if (!nestable) goto wrong_name;
1982 mod = rb_cObject;
1983 p += 2;
1984 pbeg = p;
1985 }
1986
1987 while (p < pend) {
1988 VALUE part;
1989 long len, beglen;
1990
1991 while (p < pend && *p != ':') p++;
1992
1993 if (pbeg == p) goto wrong_name;
1994
1995 id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
1996 beglen = pbeg-path;
1997
1998 if (p < pend && p[0] == ':') {
1999 if (!nestable) goto wrong_name;
2000 if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2001 p += 2;
2002 pbeg = p;
2003 }
2004
2005 if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2006 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2007 QUOTE(name));
2008 }
2009
2010 if (!id) {
2011 if (!ISUPPER(*pbeg) || !rb_enc_symname2_p(pbeg, len, enc)) {
2012 part = rb_str_subseq(name, beglen, len);
2013 rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
2014 QUOTE(part));
2015 }
2016 else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
2017 id = rb_intern3(pbeg, len, enc);
2018 }
2019 else {
2020 part = rb_str_subseq(name, beglen, len);
2021 rb_name_error_str(part, "uninitialized constant %"PRIsVALUE"%"PRIsVALUE,
2022 rb_str_subseq(name, 0, beglen),
2023 QUOTE(part));
2024 }
2025 }
2026 if (!rb_is_const_id(id)) {
2027 rb_name_error(id, "wrong constant name %"PRIsVALUE,
2028 QUOTE_ID(id));
2029 }
2030 mod = RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
2031 }
2032
2033 return mod;
2034 }
2035
2036 /*
2037 * call-seq:
2038 * mod.const_set(sym, obj) -> obj
2039 *
2040 * Sets the named constant to the given object, returning that object.
2041 * Creates a new constant if no constant with the given name previously
2042 * existed.
2043 *
2044 * Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
2045 * Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
2046 */
2047
2048 static VALUE
2049 rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
2050 {
2051 ID id = rb_to_id(name);
2052
2053 if (!rb_is_const_id(id)) {
2054 rb_name_error(id, "wrong constant name %"PRIsVALUE,
2055 QUOTE_ID(id));
2056 }
2057 rb_const_set(mod, id, value);
2058 return value;
2059 }
2060
2061 /*
2062 * call-seq:
2063 * mod.const_defined?(sym, inherit=true) -> true or false
2064 *
2065 * Checks for a constant with the given name in <i>mod</i>
2066 * If +inherit+ is set, the lookup will also search
2067 * the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
2068 *
2069 * Returns whether or not a definition is found:
2070 *
2071 * Math.const_defined? "PI" #=> true
2072 * IO.const_defined? :SYNC #=> true
2073 * IO.const_defined? :SYNC, false #=> false
2074 */
2075
2076 static VALUE
2077 rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
2078 {
2079 VALUE name, recur;
2080 ID id;
2081
2082 if (argc == 1) {
2083 name = argv[0];
2084 recur = Qtrue;
2085 }
2086 else {
2087 rb_scan_args(argc, argv, "11", &name, &recur);
2088 }
2089 if (!(id = rb_check_id(&name))) {
2090 if (rb_is_const_name(name)) {
2091 return Qfalse;
2092 }
2093 else {
2094 rb_name_error_str(name, "wrong constant name %"PRIsVALUE,
2095 QUOTE(name));
2096 }
2097 }
2098 if (!rb_is_const_id(id)) {
2099 rb_name_error(id, "wrong constant name %"PRIsVALUE,
2100 QUOTE_ID(id));
2101 }
2102 return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
2103 }
2104
2105 /*
2106 * call-seq:
2107 * obj.instance_variable_get(symbol) -> obj
2108 *
2109 * Returns the value of the given instance variable, or nil if the
2110 * instance variable is not set. The <code>@</code> part of the
2111 * variable name should be included for regular instance
2112 * variables. Throws a <code>NameError</code> exception if the
2113 * supplied symbol is not valid as an instance variable name.
2114 *
2115 * class Fred
2116 * def initialize(p1, p2)
2117 * @a, @b = p1, p2
2118 * end
2119 * end
2120 * fred = Fred.new('cat', 99)
2121 * fred.instance_variable_get(:@a) #=> "cat"
2122 * fred.instance_variable_get("@b") #=> 99
2123 */
2124
2125 static VALUE
2126 rb_obj_ivar_get(VALUE obj, VALUE iv)
2127 {
2128 ID id = rb_check_id(&iv);
2129
2130 if (!id) {
2131 if (rb_is_instance_name(iv)) {
2132 return Qnil;
2133 }
2134 else {
2135 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
2136 QUOTE(iv));
2137 }
2138 }
2139 if (!rb_is_instance_id(id)) {
2140 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
2141 QUOTE_ID(id));
2142 }
2143 return rb_ivar_get(obj, id);
2144 }
2145
2146 /*
2147 * call-seq:
2148 * obj.instance_variable_set(symbol, obj) -> obj
2149 *
2150 * Sets the instance variable names by <i>symbol</i> to
2151 * <i>object</i>, thereby frustrating the efforts of the class's
2152 * author to attempt to provide proper encapsulation. The variable
2153 * did not have to exist prior to this call.
2154 *
2155 * class Fred
2156 * def initialize(p1, p2)
2157 * @a, @b = p1, p2
2158 * end
2159 * end
2160 * fred = Fred.new('cat', 99)
2161 * fred.instance_variable_set(:@a, 'dog') #=> "dog"
2162 * fred.instance_variable_set(:@c, 'cat') #=> "cat"
2163 * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
2164 */
2165
2166 static VALUE
2167 rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
2168 {
2169 ID id = rb_to_id(iv);
2170
2171 if (!rb_is_instance_id(id)) {
2172 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
2173 QUOTE_ID(id));
2174 }
2175 return rb_ivar_set(obj, id, val);
2176 }
2177
2178 /*
2179 * call-seq:
2180 * obj.instance_variable_defined?(symbol) -> true or false
2181 *
2182 * Returns <code>true</code> if the given instance variable is
2183 * defined in <i>obj</i>.
2184 *
2185 * class Fred
2186 * def initialize(p1, p2)
2187 * @a, @b = p1, p2
2188 * end
2189 * end
2190 * fred = Fred.new('cat', 99)
2191 * fred.instance_variable_defined?(:@a) #=> true
2192 * fred.instance_variable_defined?("@b") #=> true
2193 * fred.instance_variable_defined?("@c") #=> false
2194 */
2195
2196 static VALUE
2197 rb_obj_ivar_defined(VALUE obj, VALUE iv)
2198 {
2199 ID id = rb_check_id(&iv);
2200
2201 if (!id) {
2202 if (rb_is_instance_name(iv)) {
2203 return Qfalse;
2204 }
2205 else {
2206 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
2207 QUOTE(iv));
2208 }
2209 }
2210 if (!rb_is_instance_id(id)) {
2211 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
2212 QUOTE_ID(id));
2213 }
2214 return rb_ivar_defined(obj, id);
2215 }
2216
2217 /*
2218 * call-seq:
2219 * mod.class_variable_get(symbol) -> obj
2220 *
2221 * Returns the value of the given class variable (or throws a
2222 * <code>NameError</code> exception). The <code>@@</code> part of the
2223 * variable name should be included for regular class variables
2224 *
2225 * class Fred
2226 * @@foo = 99
2227 * end
2228 * Fred.class_variable_get(:@@foo) #=> 99
2229 */
2230
2231 static VALUE
2232 rb_mod_cvar_get(VALUE obj, VALUE iv)
2233 {
2234 ID id = rb_check_id(&iv);
2235
2236 if (!id) {
2237 if (rb_is_class_name(iv)) {
2238 rb_name_error_str(iv, "uninitialized class variable %"PRIsVALUE" in %"PRIsVALUE"",
2239 iv, rb_class_name(obj));
2240 }
2241 else {
2242 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name",
2243 QUOTE(iv));
2244 }
2245 }
2246 if (!rb_is_class_id(id)) {
2247 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
2248 QUOTE_ID(id));
2249 }
2250 return rb_cvar_get(obj, id);
2251 }
2252
2253 /*
2254 * call-seq:
2255 * obj.class_variable_set(symbol, obj) -> obj
2256 *
2257 * Sets the class variable names by <i>symbol</i> to
2258 * <i>object</i>.
2259 *
2260 * class Fred
2261 * @@foo = 99
2262 * def foo
2263 * @@foo
2264 * end
2265 * end
2266 * Fred.class_variable_set(:@@foo, 101) #=> 101
2267 * Fred.new.foo #=> 101
2268 */
2269
2270 static VALUE
2271 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
2272 {
2273 ID id = rb_to_id(iv);
2274
2275 if (!rb_is_class_id(id)) {
2276 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
2277 QUOTE_ID(id));
2278 }
2279 rb_cvar_set(obj, id, val);
2280 return val;
2281 }
2282
2283 /*
2284 * call-seq:
2285 * obj.class_variable_defined?(symbol) -> true or false
2286 *
2287 * Returns <code>true</code> if the given class variable is defined
2288 * in <i>obj</i>.
2289 *
2290 * class Fred
2291 * @@foo = 99
2292 * end
2293 * Fred.class_variable_defined?(:@@foo) #=> true
2294 * Fred.class_variable_defined?(:@@bar) #=> false
2295 */
2296
2297 static VALUE
2298 rb_mod_cvar_defined(VALUE obj, VALUE iv)
2299 {
2300 ID id = rb_check_id(&iv);
2301
2302 if (!id) {
2303 if (rb_is_class_name(iv)) {
2304 return Qfalse;
2305 }
2306 else {
2307 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name",
2308 QUOTE(iv));
2309 }
2310 }
2311 if (!rb_is_class_id(id)) {
2312 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
2313 QUOTE_ID(id));
2314 }
2315 return rb_cvar_defined(obj, id);
2316 }
2317
2318 static struct conv_method_tbl {
2319 const char *method;
2320 ID id;
2321 } conv_method_names[] = {
2322 {"to_int", 0},
2323 {"to_ary", 0},
2324 {"to_str", 0},
2325 {"to_sym", 0},
2326 {"to_hash", 0},
2327 {"to_proc", 0},
2328 {"to_io", 0},
2329 {"to_a", 0},
2330 {"to_s", 0},
2331 {NULL, 0}
2332 };
2333 #define IMPLICIT_CONVERSIONS 7
2334
2335 static VALUE
2336 convert_type(VALUE val, const char *tname, const char *method, int raise)
2337 {
2338 ID m = 0;
2339 int i;
2340 VALUE r;
2341
2342 for (i=0; conv_method_names[i].method; i++) {
2343 if (conv_method_names[i].method[0] == method[0] &&
2344 strcmp(conv_method_names[i].method, method) == 0) {
2345 m = conv_method_names[i].id;
2346 break;
2347 }
2348 }
2349 if (!m) m = rb_intern(method);
2350 r = rb_check_funcall(val, m, 0, 0);
2351 if (r == Qundef) {
2352 if (raise) {
2353 rb_raise(rb_eTypeError, i < IMPLICIT_CONVERSIONS
2354 ? "no implicit conversion of %s into %s"
2355 : "can't convert %s into %s",
2356 NIL_P(val) ? "nil" :
2357 val == Qtrue ? "true" :
2358 val == Qfalse ? "false" :
2359 rb_obj_classname(val),
2360 tname);
2361 }
2362 return Qnil;
2363 }
2364 return r;
2365 }
2366
2367 VALUE
2368 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
2369 {
2370 VALUE v;
2371
2372 if (TYPE(val) == type) return val;
2373 v = convert_type(val, tname, method, TRUE);
2374 if (TYPE(v) != type) {
2375 const char *cname = rb_obj_classname(val);
2376 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
2377 cname, tname, cname, method, rb_obj_classname(v));
2378 }
2379 return v;
2380 }
2381
2382 VALUE
2383 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
2384 {
2385 VALUE v;
2386
2387 /* always convert T_DATA */
2388 if (TYPE(val) == type && type != T_DATA) return val;
2389 v = convert_type(val, tname, method, FALSE);
2390 if (NIL_P(v)) return Qnil;
2391 if (TYPE(v) != type) {
2392 const char *cname = rb_obj_classname(val);
2393 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
2394 cname, tname, cname, method, rb_obj_classname(v));
2395 }
2396 return v;
2397 }
2398
2399
2400 static VALUE
2401 rb_to_integer(VALUE val, const char *method)
2402 {
2403 VALUE v;
2404
2405 if (FIXNUM_P(val)) return val;
2406 if (RB_TYPE_P(val, T_BIGNUM)) return val;
2407 v = convert_type(val, "Integer", method, TRUE);
2408 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
2409 const char *cname = rb_obj_classname(val);
2410 rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)",
2411 cname, cname, method, rb_obj_classname(v));
2412 }
2413 return v;
2414 }
2415
2416 VALUE
2417 rb_check_to_integer(VALUE val, const char *method)
2418 {
2419 VALUE v;
2420
2421 if (FIXNUM_P(val)) return val;
2422 if (RB_TYPE_P(val, T_BIGNUM)) return val;
2423 v = convert_type(val, "Integer", method, FALSE);
2424 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
2425 return Qnil;
2426 }
2427 return v;
2428 }
2429
2430 VALUE
2431 rb_to_int(VALUE val)
2432 {
2433 return rb_to_integer(val, "to_int");
2434 }
2435
2436 VALUE
2437 rb_check_to_int(VALUE val)
2438 {
2439 return rb_check_to_integer(val, "to_int");
2440 }
2441
2442 static VALUE
2443 rb_convert_to_integer(VALUE val, int base)
2444 {
2445 VALUE tmp;
2446
2447 switch (TYPE(val)) {
2448 case T_FLOAT:
2449 if (base != 0) goto arg_error;
2450 if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX
2451 && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) {
2452 break;
2453 }
2454 return rb_dbl2big(RFLOAT_VALUE(val));
2455
2456 case T_FIXNUM:
2457 case T_BIGNUM:
2458 if (base != 0) goto arg_error;
2459 return val;
2460
2461 case T_STRING:
2462 string_conv:
2463 return rb_str_to_inum(val, base, TRUE);
2464
2465 case T_NIL:
2466 if (base != 0) goto arg_error;
2467 rb_raise(rb_eTypeError, "can't convert nil into Integer");
2468 break;
2469
2470 default:
2471 break;
2472 }
2473 if (base != 0) {
2474 tmp = rb_check_string_type(val);
2475 if (!NIL_P(tmp)) goto string_conv;
2476 arg_error:
2477 rb_raise(rb_eArgError, "base specified for non string value");
2478 }
2479 tmp = convert_type(val, "Integer", "to_int", FALSE);
2480 if (NIL_P(tmp)) {
2481 return rb_to_integer(val, "to_i");
2482 }
2483 return tmp;
2484
2485 }
2486
2487 VALUE
2488 rb_Integer(VALUE val)
2489 {
2490 return rb_convert_to_integer(val, 0);
2491 }
2492
2493 /*
2494 * call-seq:
2495 * Integer(arg,base=0) -> integer
2496 *
2497 * Converts <i>arg</i> to a <code>Fixnum</code> or <code>Bignum</code>.
2498 * Numeric types are converted directly (with floating point numbers
2499 * being truncated). <i>base</i> (0, or between 2 and 36) is a base for
2500 * integer string representation. If <i>arg</i> is a <code>String</code>,
2501 * when <i>base</i> is omitted or equals to zero, radix indicators
2502 * (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
2503 * In any case, strings should be strictly conformed to numeric
2504 * representation. This behavior is different from that of
2505 * <code>String#to_i</code>. Non string values will be converted using
2506 * <code>to_int</code>, and <code>to_i</code>.
2507 *
2508 * Integer(123.999) #=> 123
2509 * Integer("0x1a") #=> 26
2510 * Integer(Time.new) #=> 1204973019
2511 * Integer("0930", 10) #=> 930
2512 * Integer("111", 2) #=> 7
2513 */
2514
2515 static VALUE
2516 rb_f_integer(int argc, VALUE *argv, VALUE obj)
2517 {
2518 VALUE arg = Qnil;
2519 int base = 0;
2520
2521 switch (argc) {
2522 case 2:
2523 base = NUM2INT(argv[1]);
2524 case 1:
2525 arg = argv[0];
2526 break;
2527 default:
2528 /* should cause ArgumentError */
2529 rb_scan_args(argc, argv, "11", NULL, NULL);
2530 }
2531 return rb_convert_to_integer(arg, base);
2532 }
2533
2534 double
2535 rb_cstr_to_dbl(const char *p, int badcheck)
2536 {
2537 const char *q;
2538 char *end;
2539 double d;
2540 const char *ellipsis = "";
2541 int w;
2542 enum {max_width = 20};
2543 #define OutOfRange() ((end - p > max_width) ? \
2544 (w = max_width, ellipsis = "...") : \
2545 (w = (int)(end - p), ellipsis = ""))
2546
2547 if (!p) return 0.0;
2548 q = p;
2549 while (ISSPACE(*p)) p++;
2550
2551 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2552 return 0.0;
2553 }
2554
2555 d = strtod(p, &end);
2556 if (errno == ERANGE) {
2557 OutOfRange();
2558 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
2559 errno = 0;
2560 }
2561 if (p == end) {
2562 if (badcheck) {
2563 bad:
2564 rb_invalid_str(q, "Float()");
2565 }
2566 return d;
2567 }
2568 if (*end) {
2569 char buf[DBL_DIG * 4 + 10];
2570 char *n = buf;
2571 char *e = buf + sizeof(buf) - 1;
2572 char prev = 0;
2573
2574 while (p < end && n < e) prev = *n++ = *p++;
2575 while (*p) {
2576 if (*p == '_') {
2577 /* remove underscores between digits */
2578 if (badcheck) {
2579 if (n == buf || !ISDIGIT(prev)) goto bad;
2580 ++p;
2581 if (!ISDIGIT(*p)) goto bad;
2582 }
2583 else {
2584 while (*++p == '_');
2585 continue;
2586 }
2587 }
2588 prev = *p++;
2589 if (n < e) *n++ = prev;
2590 }
2591 *n = '\0';
2592 p = buf;
2593
2594 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2595 return 0.0;
2596 }
2597
2598 d = strtod(p, &end);
2599 if (errno == ERANGE) {
2600 OutOfRange();
2601 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
2602 errno = 0;
2603 }
2604 if (badcheck) {
2605 if (!end || p == end) goto bad;
2606 while (*end && ISSPACE(*end)) end++;
2607 if (*end) goto bad;
2608 }
2609 }
2610 if (errno == ERANGE) {
2611 errno = 0;
2612 OutOfRange();
2613 rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
2614 }
2615 return d;
2616 }
2617
2618 double
2619 rb_str_to_dbl(VALUE str, int badcheck)
2620 {
2621 char *s;
2622 long len;
2623 double ret;
2624 VALUE v = 0;
2625
2626 StringValue(str);
2627 s = RSTRING_PTR(str);
2628 len = RSTRING_LEN(str);
2629 if (s) {
2630 if (badcheck && memchr(s, '\0', len)) {
2631 rb_raise(rb_eArgError, "string for Float contains null byte");
2632 }
2633 if (s[len]) { /* no sentinel somehow */
2634 char *p = ALLOCV(v, len);
2635 MEMCPY(p, s, char, len);
2636 p[len] = '\0';
2637 s = p;
2638 }
2639 }
2640 ret = rb_cstr_to_dbl(s, badcheck);
2641 if (v)
2642 ALLOCV_END(v);
2643 return ret;
2644 }
2645
2646 VALUE
2647 rb_Float(VALUE val)
2648 {
2649 switch (TYPE(val)) {
2650 case T_FIXNUM:
2651 return DBL2NUM((double)FIX2LONG(val));
2652
2653 case T_FLOAT:
2654 return val;
2655
2656 case T_BIGNUM:
2657 return DBL2NUM(rb_big2dbl(val));
2658
2659 case T_STRING:
2660 return DBL2NUM(rb_str_to_dbl(val, TRUE));
2661
2662 case T_NIL:
2663 rb_raise(rb_eTypeError, "can't convert nil into Float");
2664 break;
2665
2666 default:
2667 return rb_convert_type(val, T_FLOAT, "Float", "to_f");
2668 }
2669
2670 UNREACHABLE;
2671 }
2672
2673 /*
2674 * call-seq:
2675 * Float(arg) -> float
2676 *
2677 * Returns <i>arg</i> converted to a float. Numeric types are converted
2678 * directly, the rest are converted using <i>arg</i>.to_f. As of Ruby
2679 * 1.8, converting <code>nil</code> generates a <code>TypeError</code>.
2680 *
2681 * Float(1) #=> 1.0
2682 * Float("123.456") #=> 123.456
2683 */
2684
2685 static VALUE
2686 rb_f_float(VALUE obj, VALUE arg)
2687 {
2688 return rb_Float(arg);
2689 }
2690
2691 VALUE
2692 rb_to_float(VALUE val)
2693 {
2694 if (RB_TYPE_P(val, T_FLOAT)) return val;
2695 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
2696 rb_raise(rb_eTypeError, "can't convert %s into Float",
2697 NIL_P(val) ? "nil" :
2698 val == Qtrue ? "true" :
2699 val == Qfalse ? "false" :
2700 rb_obj_classname(val));
2701 }
2702 return rb_convert_type(val, T_FLOAT, "Float", "to_f");
2703 }
2704
2705 VALUE
2706 rb_check_to_float(VALUE val)
2707 {
2708 if (RB_TYPE_P(val, T_FLOAT)) return val;
2709 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
2710 return Qnil;
2711 }
2712 return rb_check_convert_type(val, T_FLOAT, "Float", "to_f");
2713 }
2714
2715 double
2716 rb_num2dbl(VALUE val)
2717 {
2718 switch (TYPE(val)) {
2719 case T_FLOAT:
2720 return RFLOAT_VALUE(val);
2721
2722 case T_STRING:
2723 rb_raise(rb_eTypeError, "no implicit conversion to float from string");
2724 break;
2725
2726 case T_NIL:
2727 rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
2728 break;
2729
2730 default:
2731 break;
2732 }
2733
2734 return RFLOAT_VALUE(rb_Float(val));
2735 }
2736
2737 VALUE
2738 rb_String(VALUE val)
2739 {
2740 VALUE tmp = rb_check_string_type(val);
2741 if (NIL_P(tmp))
2742 tmp = rb_convert_type(val, T_STRING, "String", "to_s");
2743 return tmp;
2744 }
2745
2746
2747 /*
2748 * call-seq:
2749 * String(arg) -> string
2750 *
2751 * Converts <i>arg</i> to a <code>String</code> by calling its
2752 * <code>to_s</code> method.
2753 *
2754 * String(self) #=> "main"
2755 * String(self.class) #=> "Object"
2756 * String(123456) #=> "123456"
2757 */
2758
2759 static VALUE
2760 rb_f_string(VALUE obj, VALUE arg)
2761 {
2762 return rb_String(arg);
2763 }
2764
2765 VALUE
2766 rb_Array(VALUE val)
2767 {
2768 VALUE tmp = rb_check_array_type(val);
2769
2770 if (NIL_P(tmp)) {
2771 tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
2772 if (NIL_P(tmp)) {
2773 return rb_ary_new3(1, val);
2774 }
2775 }
2776 return tmp;
2777 }
2778
2779 /*
2780 * call-seq:
2781 * Array(arg) -> array
2782 *
2783 * Returns +arg+ as an Array.
2784 *
2785 * First tries to call Array#to_ary on +arg+, then Array#to_a.
2786 *
2787 * Array(1..5) #=> [1, 2, 3, 4, 5]
2788 */
2789
2790 static VALUE
2791 rb_f_array(VALUE obj, VALUE arg)
2792 {
2793 return rb_Array(arg);
2794 }
2795
2796 VALUE
2797 rb_Hash(VALUE val)
2798 {
2799 VALUE tmp;
2800
2801 if (NIL_P(val)) return rb_hash_new();
2802 tmp = rb_check_hash_type(val);
2803 if (NIL_P(tmp)) {
2804 if (RB_TYPE_P(val, T_ARRAY) && RARRAY_LEN(val) == 0)
2805 return rb_hash_new();
2806 rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
2807 }
2808 return tmp;
2809 }
2810
2811 /*
2812 * call-seq:
2813 * Hash(arg) -> hash
2814 *
2815 * Converts <i>arg</i> to a <code>Hash</code> by calling
2816 * <i>arg</i><code>.to_hash</code>. Returns an empty <code>Hash</code> when
2817 * <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>.
2818 *
2819 * Hash([]) #=> {}
2820 * Hash(nil) #=> nil
2821 * Hash(key: :value) #=> {:key => :value}
2822 * Hash([1, 2, 3]) #=> TypeError
2823 */
2824
2825 static VALUE
2826 rb_f_hash(VALUE obj, VALUE arg)
2827 {
2828 return rb_Hash(arg);
2829 }
2830
2831 /*
2832 * Document-class: Class
2833 *
2834 * Classes in Ruby are first-class objects---each is an instance of
2835 * class <code>Class</code>.
2836 *
2837 * Typically, you create a new class by using:
2838 *
2839 * class Name
2840 * # some class describing the class behavior
2841 * end
2842 *
2843 * When a new class is created, an object of type Class is initialized and
2844 * assigned to a global constant (<code>Name</code> in this case).
2845 *
2846 * When <code>Name.new</code> is called to create a new object, the
2847 * <code>new</code> method in <code>Class</code> is run by default.
2848 * This can be demonstrated by overriding <code>new</code> in
2849 * <code>Class</code>:
2850 *
2851 * class Class
2852 * alias oldNew new
2853 * def new(*args)
2854 * print "Creating a new ", self.name, "\n"
2855 * oldNew(*args)
2856 * end
2857 * end
2858 *
2859 *
2860 * class Name
2861 * end
2862 *
2863 *
2864 * n = Name.new
2865 *
2866 * <em>produces:</em>
2867 *
2868 * Creating a new Name
2869 *
2870 * Classes, modules, and objects are interrelated. In the diagram
2871 * that follows, the vertical arrows represent inheritance, and the
2872 * parentheses meta-classes. All metaclasses are instances
2873 * of the class `Class'.
2874 * +---------+ +-...
2875 * | | |
2876 * BasicObject-----|-->(BasicObject)-------|-...
2877 * ^ | ^ |
2878 * | | | |
2879 * Object---------|----->(Object)---------|-...
2880 * ^ | ^ |
2881 * | | | |
2882 * +-------+ | +--------+ |
2883 * | | | | | |
2884 * | Module-|---------|--->(Module)-|-...
2885 * | ^ | | ^ |
2886 * | | | | | |
2887 * | Class-|---------|---->(Class)-|-...
2888 * | ^ | | ^ |
2889 * | +---+ | +----+
2890 * | |
2891 * obj--->OtherClass---------->(OtherClass)-----------...
2892 *
2893 */
2894
2895
2914 /* Document-class: BasicObject
2915 *
2916 * BasicObject is the parent class of all classes in Ruby. It's an explicit
2917 * blank class.
2918 *
2919 * BasicObject can be used for creating object hierarchies independent of
2920 * Ruby's object hierarchy, proxy objects like the Delegator class, or other
2921 * uses where namespace pollution from Ruby's methods and classes must be
2922 * avoided.
2923 *
2924 * To avoid polluting BasicObject for other users an appropriately named
2925 * subclass of BasicObject should be created instead of directly modifying
2926 * BasicObject:
2927 *
2928 * class MyObjectSystem < BasicObject
2929 * end
2930 *
2931 * BasicObject does not include Kernel (for methods like +puts+) and
2932 * BasicObject is outside of the namespace of the standard library so common
2933 * classes will not be found without a using a full class path.
2934 *
2935 * A variety of strategies can be used to provide useful portions of the
2936 * standard library to subclasses of BasicObject. A subclass could
2937 * <code>include Kernel</code> to obtain +puts+, +exit+, etc. A custom
2938 * Kernel-like module could be created and included or delegation can be used
2939 * via #method_missing:
2940 *
2941 * class MyObjectSystem < BasicObject
2942 * DELEGATE = [:puts, :p]
2943 *
2944 * def method_missing(name, *args, &block)
2945 * super unless DELEGATE.include? name
2946 * ::Kernel.send(name, *args, &block)
2947 * end
2948 *
2949 * def respond_to_missing?(name, include_private = false)
2950 * DELEGATE.include?(name) or super
2951 * end
2952 * end
2953 *
2954 * Access to classes and modules from the Ruby standard library can be
2955 * obtained in a BasicObject subclass by referencing the desired constant
2956 * from the root like <code>::File</code> or <code>::Enumerator</code>.
2957 * Like #method_missing, #const_missing can be used to delegate constant
2958 * lookup to +Object+:
2959 *
2960 * class MyObjectSystem < BasicObject
2961 * def self.const_missing(name)
2962 * ::Object.const_get(name)
2963 * end
2964 * end
2965 */
2966
2967 /* Document-class: Object
2968 *
2969 * Object is the default root of all Ruby objects. Object inherits from
2970 * BasicObject which allows creating alternate object hierarchies. Methods
2971 * on object are available to all classes unless explicitly overridden.
2972 *
2973 * Object mixes in the Kernel module, making the built-in kernel functions
2974 * globally accessible. Although the instance methods of Object are defined
2975 * by the Kernel module, we have chosen to document them here for clarity.
2976 *
2977 * When referencing constants in classes inheriting from Object you do not
2978 * need to use the full namespace. For example, referencing +File+ inside
2979 * +YourClass+ will find the top-level File class.
2980 *
2981 * In the descriptions of Object's methods, the parameter <i>symbol</i> refers
2982 * to a symbol, which is either a quoted string or a Symbol (such as
2983 * <code>:name</code>).
2984 */
2985
2986 void
2987 Init_Object(void)
2988 {
2989 int i;
2990
2991 Init_class_hierarchy();
2992
2993 #if 0
2994 // teach RDoc about these classes
2995 rb_cBasicObject = rb_define_class("BasicObject", Qnil);
2996 rb_cObject = rb_define_class("Object", rb_cBasicObject);
2997 rb_cModule = rb_define_class("Module", rb_cObject);
2998 rb_cClass = rb_define_class("Class", rb_cModule);
2999 #endif
3000
3001 #undef rb_intern
3002 #define rb_intern(str) rb_intern_const(str)
3003
3004 rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0);
3005 rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
3006 rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
3007 rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
3008 rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
3009 rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);
3010
3011 rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1);
3012 rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1);
3013 rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
3014
3015 /* Document-module: Kernel
3016 *
3017 * The Kernel module is included by class Object, so its methods are
3018 * available in every Ruby object.
3019 *
3020 * The Kernel instance methods are documented in class Object while the
3021 * module methods are documented here. These methods are called without a
3022 * receiver and thus can be called in functional form:
3023 *
3024 * sprintf "%.1f", 1.234 #=> "1.2"
3025 *
3026 */
3027 rb_mKernel = rb_define_module("Kernel");
3028 rb_include_module(rb_cObject, rb_mKernel);
3029 rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1);
3030 rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1);
3031 rb_define_private_method(rb_cModule, "extended", rb_obj_dummy, 1);
3032 rb_define_private_method(rb_cModule, "prepended", rb_obj_dummy, 1);
3033 rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
3034 rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
3035 rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
3036
3037 rb_define_method(rb_mKernel, "nil?", rb_false, 0);
3038 rb_define_method(rb_mKernel, "===", rb_equal, 1);
3039 rb_define_method(rb_mKernel, "=~", rb_obj_match, 1);
3040 rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
3041 rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
3042 rb_define_method(rb_mKernel, "hash", rb_obj_hash, 0);
3043 rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
3044
3045 rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
3046 rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
3047 rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
3048 rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
3049 rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
3050 rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
3051 rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1);
3052
3053 rb_define_method(rb_mKernel, "taint", rb_obj_taint, 0);
3054 rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
3055 rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
3056 rb_define_method(rb_mKernel, "untrust", rb_obj_untrust, 0);
3057 rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0);
3058 rb_define_method(rb_mKernel, "trust", rb_obj_trust, 0);
3059 rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
3060 rb_define_method(rb_mKernel, "frozen?", rb_obj_frozen_p, 0);
3061
3062 rb_define_method(rb_mKernel, "to_s", rb_any_to_s, 0);
3063 rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
3064 rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */
3065 rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
3066 rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
3067 rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */
3068 rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */
3069 rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
3070 rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
3071 rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
3072 rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
3073 rb_define_method(rb_mKernel, "remove_instance_variable",
3074 rb_obj_remove_instance_variable, 1); /* in variable.c */
3075
3076 rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
3077 rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
3078 rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
3079 rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
3080
3081 rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */
3082 rb_define_global_function("format", rb_f_sprintf, -1); /* in sprintf.c */
3083
3084 rb_define_global_function("Integer", rb_f_integer, -1);
3085 rb_define_global_function("Float", rb_f_float, 1);
3086
3087 rb_define_global_function("String", rb_f_string, 1);
3088 rb_define_global_function("Array", rb_f_array, 1);
3089 rb_define_global_function("Hash", rb_f_hash, 1);
3090
3091 rb_cNilClass = rb_define_class("NilClass", rb_cObject);
3092 rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
3093 rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
3094 rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
3095 rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
3096 rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0);
3097 rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
3098 rb_define_method(rb_cNilClass, "&", false_and, 1);
3099 rb_define_method(rb_cNilClass, "|", false_or, 1);
3100 rb_define_method(rb_cNilClass, "^", false_xor, 1);
3101
3102 rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
3103 rb_undef_alloc_func(rb_cNilClass);
3104 rb_undef_method(CLASS_OF(rb_cNilClass), "new");
3105 /*
3106 * An alias of +nil+
3107 */
3108 rb_define_global_const("NIL", Qnil);
3109
3110 rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
3111 rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
3112 rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
3113 rb_define_method(rb_cModule, "<=>", rb_mod_cmp, 1);
3114 rb_define_method(rb_cModule, "<", rb_mod_lt, 1);
3115 rb_define_method(rb_cModule, "<=", rb_class_inherited_p, 1);
3116 rb_define_method(rb_cModule, ">", rb_mod_gt, 1);
3117 rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
3118 rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
3119 rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
3120 rb_define_alias(rb_cModule, "inspect", "to_s");
3121 rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
3122 rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
3123 rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */
3124 rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
3125
3126 rb_define_private_method(rb_cModule, "attr", rb_mod_attr, -1);
3127 rb_define_private_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
3128 rb_define_private_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
3129 rb_define_private_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
3130
3131 rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
3132 rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
3133 rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
3134 rb_define_method(rb_cModule, "public_instance_methods",
3135 rb_class_public_instance_methods, -1); /* in class.c */
3136 rb_define_method(rb_cModule, "protected_instance_methods",
3137 rb_class_protected_instance_methods, -1); /* in class.c */
3138 rb_define_method(rb_cModule, "private_instance_methods",
3139 rb_class_private_instance_methods, -1); /* in class.c */
3140
3141 rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
3142 rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
3143 rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
3144 rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
3145 rb_define_private_method(rb_cModule, "remove_const",
3146 rb_mod_remove_const, 1); /* in variable.c */
3147 rb_define_method(rb_cModule, "const_missing",
3148 rb_mod_const_missing, 1); /* in variable.c */
3149 rb_define_method(rb_cModule, "class_variables",
3150 rb_mod_class_variables, -1); /* in variable.c */
3151 rb_define_method(rb_cModule, "remove_class_variable",
3152 rb_mod_remove_cvar, 1); /* in variable.c */
3153 rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
3154 rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
3155 rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
3156 rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); /* in variable.c */
3157 rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); /* in variable.c */
3158
3159 rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
3160 rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
3161 rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
3162 rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
3163 rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
3164 rb_undef_method(rb_cClass, "extend_object");
3165 rb_undef_method(rb_cClass, "append_features");
3166 rb_undef_method(rb_cClass, "prepend_features");
3167
3168 /*
3169 * Document-class: Data
3170 *
3171 * This is a recommended base class for C extensions using Data_Make_Struct
3172 * or Data_Wrap_Struct, see README.EXT for details.
3173 */
3174 rb_cData = rb_define_class("Data", rb_cObject);
3175 rb_undef_alloc_func(rb_cData);
3176
3177 rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
3178 rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
3179 rb_define_alias(rb_cTrueClass, "inspect", "to_s");
3180 rb_define_method(rb_cTrueClass, "&", true_and, 1);
3181 rb_define_method(rb_cTrueClass, "|", true_or, 1);
3182 rb_define_method(rb_cTrueClass, "^", true_xor, 1);
3183 rb_undef_alloc_func(rb_cTrueClass);
3184 rb_undef_method(CLASS_OF(rb_cTrueClass), "new");
3185 /*
3186 * An alias of +true+
3187 */
3188 rb_define_global_const("TRUE", Qtrue);
3189
3190 rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
3191 rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
3192 rb_define_alias(rb_cFalseClass, "inspect", "to_s");
3193 rb_define_method(rb_cFalseClass, "&", false_and, 1);
3194 rb_define_method(rb_cFalseClass, "|", false_or, 1);
3195 rb_define_method(rb_cFalseClass, "^", false_xor, 1);
3196 rb_undef_alloc_func(rb_cFalseClass);
3197 rb_undef_method(CLASS_OF(rb_cFalseClass), "new");
3198 /*
3199 * An alias of +false+
3200 */
3201 rb_define_global_const("FALSE", Qfalse);
3202
3203 id_eq = rb_intern("==");
3204 id_eql = rb_intern("eql?");
3205 id_match = rb_intern("=~");
3206 id_inspect = rb_intern("inspect");
3207 id_init_copy = rb_intern("initialize_copy");
3208 id_init_clone = rb_intern("initialize_clone");
3209 id_init_dup = rb_intern("initialize_dup");
3210 id_const_missing = rb_intern("const_missing");
3211
3212 for (i=0; conv_method_names[i].method; i++) {
3213 conv_method_names[i].id = rb_intern(conv_method_names[i].method);
3214 }
3215 }
FIXNUM_MAX
#define FIXNUM_MAX
RB_TYPE_P
#define RB_TYPE_P(obj, type)
rb_const_get_at
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:1882
rb_to_int
VALUE rb_to_int(VALUE)
Definition: object.c:2431
tmp
volatile VALUE tmp
Definition: tcltklib.c:10209
rb_obj_ivar_defined
static VALUE rb_obj_ivar_defined(VALUE obj, VALUE iv)
Definition: object.c:2197
nil_to_h
static VALUE nil_to_h(VALUE obj)
Definition: object.c:1108
ROBJECT_EMBED
#define ROBJECT_EMBED
n
ssize_t n
Definition: bigdecimal.c:5655
rb_mod_attr_accessor
static VALUE rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
Definition: object.c:1895
rb_mod_cmp
static VALUE rb_mod_cmp(VALUE mod, VALUE arg)
Definition: object.c:1575
rb_cBasicObject
RUBY_EXTERN VALUE rb_cBasicObject
Definition: ripper.y:1425
rb_mod_include_p
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Definition: class.c:890
rb_special_const_p
static int rb_special_const_p(VALUE obj)
Definition: ripper.y:1560
rb_any_to_s
VALUE rb_any_to_s(VALUE)
Definition: object.c:384
rb_eql
int rb_eql(VALUE, VALUE)
Definition: object.c:67
FALSE
#define FALSE
Definition: nkf.h:174
rb_check_inheritable
void rb_check_inheritable(VALUE super)
Ensures a class can be derived from super.
Definition: class.c:95
rb_alloc_func_t
VALUE(* rb_alloc_func_t)(VALUE)
Definition: ripper.y:352
rb_is_class_name
int rb_is_class_name(VALUE name)
Definition: ripper.c:17180
rb_cNilClass
RUBY_EXTERN VALUE rb_cNilClass
Definition: ripper.y:1447
rb_cModule
RUBY_EXTERN VALUE rb_cModule
Definition: ripper.y:1445
OBJ_INFECT
#define OBJ_INFECT(x, s)
rb_class_new_instance
VALUE rb_class_new_instance(int, VALUE *, VALUE)
Definition: object.c:1775
st_table
Definition: ripper.y:77
rb_obj_classname
const char * rb_obj_classname(VALUE)
Definition: variable.c:396
rb_obj_private_methods
VALUE rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1194
p
Win32OLEIDispatch * p
Definition: win32ole.c:786
rb_convert_to_integer
static VALUE rb_convert_to_integer(VALUE val, int base)
Definition: object.c:2443
FL_TEST
#define FL_TEST(x, f)
st_lookup
int st_lookup(st_table *, st_data_t, st_data_t *)
rb_check_trusted
#define rb_check_trusted(obj)
ruby.h
rb_sym_to_s
VALUE rb_sym_to_s(VALUE)
Definition: string.c:7906
FL_EXIVAR
#define FL_EXIVAR
DBL_DIG
#define DBL_DIG
Definition: numeric.c:59
st_init_numtable
st_table * st_init_numtable(void)
Definition: st.c:272
rb_class_private_instance_methods
VALUE rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1109
T_ICLASS
#define T_ICLASS
rb_cstr_to_dbl
double rb_cstr_to_dbl(const char *, int)
Definition: object.c:2535
rb_usascii_str_new2
#define rb_usascii_str_new2
rb_secure
void rb_secure(int)
Definition: safe.c:79
id_eql
static ID id_eql
Definition: object.c:38
rb_const_get
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:1876
rb_obj_remove_instance_variable
VALUE rb_obj_remove_instance_variable(VALUE, VALUE)
Definition: variable.c:1400
rb_define_global_const
void rb_define_global_const(const char *, VALUE)
Definition: variable.c:2216
i
ssize_t i
Definition: bigdecimal.c:5655
rb_mod_ge
static VALUE rb_mod_ge(VALUE mod, VALUE arg)
Definition: object.c:1535
rb_check_frozen
#define rb_check_frozen(obj)
rb_mod_cvar_set
static VALUE rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
Definition: object.c:2271
probes.h
rb_mod_ancestors
VALUE rb_mod_ancestors(VALUE mod)
Definition: class.c:920
rb_str_subseq
VALUE rb_str_subseq(VALUE, long, long)
Definition: string.c:1668
rb_refinement_module_get_refined_class
VALUE rb_refinement_module_get_refined_class(VALUE module)
Definition: eval.c:1129
ROBJECT_EMBED_LEN_MAX
#define ROBJECT_EMBED_LEN_MAX
RFLOAT_VALUE
#define RFLOAT_VALUE(v)
rb_mod_lt
static VALUE rb_mod_lt(VALUE mod, VALUE arg)
Definition: object.c:1515
ret
int ret
Definition: tcltklib.c:280
rb_iv_get
VALUE rb_iv_get(VALUE, const char *)
Definition: variable.c:2583
rb_define_private_method
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1356
RCLASS_ORIGIN
#define RCLASS_ORIGIN(c)
a
Real * a
Definition: bigdecimal.c:1182
rb_obj_freeze
VALUE rb_obj_freeze(VALUE)
Definition: object.c:989
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:511
OBJ_FREEZE
#define OBJ_FREEZE(x)
rb_obj_dup
VALUE rb_obj_dup(VALUE)
Definition: object.c:338
rb_define_alloc_func
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
rb_obj_tap
VALUE rb_obj_tap(VALUE obj)
Definition: object.c:613
rb_mod_remove_cvar
VALUE rb_mod_remove_cvar(VALUE, VALUE)
Definition: variable.c:2549
OBJ_TAINTED
#define OBJ_TAINTED(x)
UNREACHABLE
#define UNREACHABLE
Definition: ruby.h:40
rb_mod_attr_reader
static VALUE rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
Definition: object.c:1839
enc
VALUE enc
Definition: tcltklib.c:10311
QUOTE_ID
#define QUOTE_ID(id)
path
gz path
Definition: zlib.c:2279
init_copy
static void init_copy(VALUE dest, VALUE obj)
Definition: object.c:223
false_and
static VALUE false_and(VALUE obj, VALUE obj2)
Definition: object.c:1237
TYPE
#define TYPE(x)
constant.h
rb_cTrueClass
RUBY_EXTERN VALUE rb_cTrueClass
Definition: ripper.y:1461
RUBY_DTRACE_OBJECT_CREATE_ENABLED
#define RUBY_DTRACE_OBJECT_CREATE_ENABLED()
Definition: probes.h:51
rb_mod_init_copy
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
Definition: class.c:196
ROBJECT
#define ROBJECT(obj)
nil_inspect
static VALUE nil_inspect(VALUE obj)
Definition: object.c:1121
RSTRING_PTR
#define RSTRING_PTR(str)
CLASS_OF
#define CLASS_OF(v)
NIL_P
NIL_P(eventloop_thread)
Definition: tcltklib.c:4068
ROBJECT_IVPTR
#define ROBJECT_IVPTR(o)
rb_mod_attr_writer
static VALUE rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
Definition: object.c:1869
rb_gc_copy_finalizer
void rb_gc_copy_finalizer(VALUE, VALUE)
Definition: gc.c:1349
T_ARRAY
#define T_ARRAY
id_match
static ID id_match
Definition: object.c:38
rb_obj_tainted
VALUE rb_obj_tainted(VALUE)
Definition: object.c:861
rb_obj_ivar_get
static VALUE rb_obj_ivar_get(VALUE obj, VALUE iv)
Definition: object.c:2126
Init_class_hierarchy
void Init_class_hierarchy(void)
Definition: class.c:401
xfree
#define xfree
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1780
Qtrue
return Qtrue
Definition: tcltklib.c:9610
rb_obj_class
VALUE rb_obj_class(VALUE)
Definition: object.c:194
rb_obj_id
VALUE rb_obj_id(VALUE)
Definition: gc.c:1688
conv_method_names
static struct conv_method_tbl conv_method_names[]
rb_class_name
VALUE rb_class_name(VALUE)
Definition: variable.c:383
rb_obj_cmp
static VALUE rb_obj_cmp(VALUE obj1, VALUE obj2)
Definition: object.c:1356
index
int index
Definition: tcltklib.c:4478
rb_cvar_defined
VALUE rb_cvar_defined(VALUE, ID)
Definition: variable.c:2389
rb_ary_new3
VALUE rb_ary_new3(long n,...)
Definition: array.c:432
rb_mod_name
VALUE rb_mod_name(VALUE)
Definition: variable.c:210
rb_eSecurityError
VALUE rb_eSecurityError
Definition: error.c:520
T_NIL
#define T_NIL
rb_obj_untrusted
VALUE rb_obj_untrusted(VALUE)
Definition: object.c:915
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:695
rb_obj_match
static VALUE rb_obj_match(VALUE obj1, VALUE obj2)
Definition: object.c:1316
rb_mod_const_get
static VALUE rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
Definition: object.c:1940
r
r
Definition: bigdecimal.c:1196
FL_UNTRUSTED
#define FL_UNTRUSTED
rb_big2dbl
double rb_big2dbl(VALUE x)
Definition: bignum.c:1429
rb_define_global_function
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1526
ISDIGIT
#define ISDIGIT(c)
rb_check_id
ID rb_check_id(volatile VALUE *namep)
Returns ID for the given name if it is interned already, or 0.
Definition: ripper.c:17099
rb_check_id_cstr
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Definition: ripper.c:17146
T_FLOAT
#define T_FLOAT
nil_to_i
static VALUE nil_to_i(VALUE obj)
Definition: object.c:1046
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1362
T_OBJECT
#define T_OBJECT
rb_str_append
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2121
rb_obj_not_match
static VALUE rb_obj_not_match(VALUE obj1, VALUE obj2)
Definition: object.c:1330
rb_Hash
VALUE rb_Hash(VALUE)
Definition: object.c:2797
rb_obj_dummy
static VALUE rb_obj_dummy(void)
Definition: object.c:848
rb_f_sprintf
VALUE rb_f_sprintf(int, const VALUE *)
Definition: sprintf.c:433
rb_ivar_get
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1116
rb_equal
VALUE rb_equal(VALUE, VALUE)
Definition: object.c:56
rb_str_concat
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:2162
rb_enc_symname2_p
int rb_enc_symname2_p(const char *name, long len, rb_encoding *enc)
Definition: ripper.c:16741
d
d
Definition: strlcat.c:58
rb_name_error_str
void rb_name_error_str(VALUE str, const char *fmt,...)
Definition: error.c:914
OutOfRange
#define OutOfRange()
rb_copy_generic_ivar
void rb_copy_generic_ivar(VALUE, VALUE)
Definition: variable.c:1047
rb_cvar_get
VALUE rb_cvar_get(VALUE, ID)
Definition: variable.c:2362
rb_name_error
void rb_name_error(ID id, const char *fmt,...)
Definition: error.c:899
rb_f_array
static VALUE rb_f_array(VALUE obj, VALUE arg)
Definition: object.c:2791
rb_hash_start
st_index_t rb_hash_start(st_index_t)
Definition: random.c:1416
rb_mod_cvar_get
static VALUE rb_mod_cvar_get(VALUE obj, VALUE iv)
Definition: object.c:2232
st_data_t
unsigned long st_data_t
Definition: ripper.y:35
strtod
#define strtod(s, e)
Definition: util.h:76
rb_usascii_str_new
VALUE rb_usascii_str_new(const char *, long)
Definition: string.c:431
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1470
rb_is_const_id
int rb_is_const_id(ID id)
Definition: ripper.c:17046
rb_is_instance_id
int rb_is_instance_id(ID id)
Definition: ripper.c:17064
rb_eNameError
VALUE rb_eNameError
Definition: error.c:516
FL_FINALIZE
#define FL_FINALIZE
rb_check_to_float
VALUE rb_check_to_float(VALUE)
Definition: object.c:2706
true_or
static VALUE true_or(VALUE obj, VALUE obj2)
Definition: object.c:1181
rb_obj_not
VALUE rb_obj_not(VALUE obj)
Definition: object.c:151
rb_default_external_encoding
rb_encoding * rb_default_external_encoding(void)
Definition: encoding.c:1288
rb_obj_is_instance_of
VALUE rb_obj_is_instance_of(VALUE, VALUE)
Definition: object.c:545
false_or
static VALUE false_or(VALUE obj, VALUE obj2)
Definition: object.c:1253
id_inspect
static ID id_inspect
Definition: object.c:38
rb_class_inherited
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition: class.c:473
rb_to_integer
static VALUE rb_to_integer(VALUE val, const char *method)
Definition: object.c:2401
m
BDIGIT m
Definition: bigdecimal.c:5085
rb_obj_untrust
VALUE rb_obj_untrust(VALUE)
Definition: object.c:930
FIXNUM_P
#define FIXNUM_P(f)
Qfalse
return Qfalse
Definition: tcltklib.c:6779
rb_Float
VALUE rb_Float(VALUE)
Definition: object.c:2647
rb_dbl2big
VALUE rb_dbl2big(double d)
Definition: bignum.c:1353
rb_block_given_p
int rb_block_given_p(void)
Definition: eval.c:672
rb_class_allocate_instance
static VALUE rb_class_allocate_instance(VALUE klass)
Definition: object.c:1756
rb_f_hash
static VALUE rb_f_hash(VALUE obj, VALUE arg)
Definition: object.c:2826
RARRAY_LEN
#define RARRAY_LEN(a)
Qnil
#define Qnil
Definition: tcltklib.c:1896
nil_to_f
static VALUE nil_to_f(VALUE obj)
Definition: object.c:1061
val
#define val
Definition: tcltklib.c:1949
rb_true
static VALUE rb_true(VALUE obj)
Definition: object.c:1285
rb_str_to_inum
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
Definition: bignum.c:777
RCLASS_IV_TBL
#define RCLASS_IV_TBL(c)
rb_mKernel
RUBY_EXTERN VALUE rb_mKernel
Definition: ripper.y:1414
str
static VALUE char * str
Definition: tcltklib.c:3547
rb_mod_attr
VALUE rb_mod_attr(int argc, VALUE *argv, VALUE klass)
Definition: object.c:1850
rb_mod_freeze
static VALUE rb_mod_freeze(VALUE mod)
Definition: object.c:1445
Check_Type
#define Check_Type(v, t)
flags
int flags
Definition: tcltklib.c:3023
ID
unsigned long ID
Definition: ripper.y:105
rb_mod_private_constant
VALUE rb_mod_private_constant(int argc, VALUE *argv, VALUE obj)
Definition: variable.c:2269
RCLASS_SUPER
#define RCLASS_SUPER(c)
rb_str_cat2
VALUE rb_str_cat2(VALUE, const char *)
Definition: string.c:1982
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:499
obj
static VALUE VALUE obj
Definition: tcltklib.c:3158
RSTRING_LEN
#define RSTRING_LEN(str)
INT2FIX
#define INT2FIX(i)
rb_check_to_integer
VALUE rb_check_to_integer(VALUE, const char *)
Definition: object.c:2417
rb_obj_singleton_class
static VALUE rb_obj_singleton_class(VALUE obj)
Definition: object.c:217
rb_mod_class_variables
VALUE rb_mod_class_variables(int, VALUE *, VALUE)
Definition: variable.c:2508
FIX2LONG
#define FIX2LONG(x)
FIXNUM_MIN
#define FIXNUM_MIN
rb_class_initialize
static VALUE rb_class_initialize(int argc, VALUE *argv, VALUE klass)
Definition: object.c:1673
T_STRING
#define T_STRING
rb_num2dbl
double rb_num2dbl(VALUE)
Definition: object.c:2716
conv_method_tbl
Definition: object.c:2318
method
volatile ID method
Definition: tcltklib.c:3599
rb_str_to_dbl
double rb_str_to_dbl(VALUE, int)
Definition: object.c:2619
rb_sourcefile
#define rb_sourcefile()
Definition: tcltklib.c:97
rb_hash_end
#define rb_hash_end(h)
false_xor
static VALUE false_xor(VALUE obj, VALUE obj2)
Definition: object.c:1272
rb_check_hash_type
VALUE rb_check_hash_type(VALUE)
Definition: hash.c:461
ISUPPER
#define ISUPPER(c)
Definition: ruby.h:1633
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
rb_obj_protected_methods
VALUE rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1179
rb_eEncCompatError
VALUE rb_eEncCompatError
Definition: error.c:518
DBL2NUM
#define DBL2NUM(dbl)
rb_check_funcall
VALUE rb_check_funcall(VALUE, ID, int, VALUE *)
Definition: vm_eval.c:408
encoding.h
rb_f_float
static VALUE rb_f_float(VALUE obj, VALUE arg)
Definition: object.c:2686
rb_str_dup
VALUE rb_str_dup(VALUE)
Definition: string.c:946
rb_obj_as_string
VALUE rb_obj_as_string(VALUE)
Definition: string.c:895
rb_cInteger
RUBY_EXTERN VALUE rb_cInteger
Definition: ripper.y:1441
argv
VALUE * argv
Definition: tcltklib.c:1971
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1539
rb_yield
VALUE rb_yield(VALUE)
Definition: vm_eval.c:934
rb_mod_eqq
static VALUE rb_mod_eqq(VALUE mod, VALUE arg)
Definition: object.c:1462
rb_mod_initialize
static VALUE rb_mod_initialize(VALUE module)
Definition: object.c:1634
RTEST
#define RTEST(v)
id
const int id
Definition: nkf.c:209
rb_obj_clone
VALUE rb_obj_clone(VALUE)
Definition: object.c:296
errno
int errno
util.h
TRUE
#define TRUE
Definition: nkf.h:175
result
q result
Definition: tcltklib.c:7070
id_init_clone
static ID id_init_clone
Definition: object.c:39
rb_obj_hash
VALUE rb_obj_hash(VALUE obj)
Definition: object.c:129
rb_sprintf
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1270
value
volatile VALUE value
Definition: tcltklib.c:9442
StringValue
#define StringValue(v)
true_to_s
static VALUE true_to_s(VALUE obj)
Definition: object.c:1144
rb_const_defined
int rb_const_defined(VALUE, ID)
Definition: variable.c:2103
s
register char * s
Definition: os2.c:56
CONST_ID
#define CONST_ID(var, str)
QUOTE
#define QUOTE(str)
rb_String
VALUE rb_String(VALUE)
Definition: object.c:2738
rb_class_inherited_p
VALUE rb_class_inherited_p(VALUE, VALUE)
Definition: object.c:1480
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
internal.h
rb_attr
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:796
rb_const_set
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:2141
OBJ_FROZEN
#define OBJ_FROZEN(x)
rb_mod_public_constant
VALUE rb_mod_public_constant(int argc, VALUE *argv, VALUE obj)
Definition: variable.c:2283
type
int type
Definition: tcltklib.c:111
FL_TAINT
#define FL_TAINT
id_init_dup
static ID id_init_dup
Definition: object.c:39
rb_obj_public_methods
VALUE rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1209
T_FIXNUM
#define T_FIXNUM
rb_obj_untaint
VALUE rb_obj_untaint(VALUE)
Definition: object.c:897
argc
int argc
Definition: tcltklib.c:1970
FL_FREEZE
#define FL_FREEZE
nil_to_s
static VALUE nil_to_s(VALUE obj)
Definition: object.c:1074
rb_obj_trust
VALUE rb_obj_trust(VALUE)
Definition: object.c:949
rb_const_defined_at
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:2109
rb_obj_frozen_p
VALUE rb_obj_frozen_p(VALUE)
Definition: object.c:1018
bad
#define bad(x)
Definition: _sdbm.c:125
rb_undef_alloc_func
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:482
rb_make_metaclass
VALUE rb_make_metaclass(VALUE obj, VALUE unused)
Definition: class.c:430
ST_CONTINUE
Definition: ripper.y:108
rb_attr_get
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1122
rb_sourceline
int rb_sourceline(void)
Definition: vm.c:816
IMPLICIT_CONVERSIONS
#define IMPLICIT_CONVERSIONS
Definition: object.c:2333
ptr
return ptr
Definition: tcltklib.c:784
rb_mod_constants
VALUE rb_mod_constants(int, VALUE *, VALUE)
Definition: variable.c:2046
c
VpDivd * c
Definition: bigdecimal.c:1205
Init_Object
void Init_Object(void)
Initializes the world of objects and classes.
Definition: object.c:2987
rb_class_instance_methods
VALUE rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1071
rb_ivar_defined
VALUE rb_ivar_defined(VALUE, ID)
Definition: variable.c:1204
rb_free_const_table
void rb_free_const_table(st_table *tbl)
Definition: gc.c:814
ALLOCV
#define ALLOCV(v, n)
T_BIGNUM
#define T_BIGNUM
rb_obj_ivar_set
static VALUE rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
Definition: object.c:2167
RCLASS_CONST_TBL
#define RCLASS_CONST_TBL(c)
MEMCPY
#define MEMCPY(p1, p2, type, n)
end
gz end
Definition: zlib.c:2272
rb_to_id
ID rb_to_id(VALUE)
Definition: string.c:8154
rb_obj_taint
VALUE rb_obj_taint(VALUE)
Definition: object.c:878
true_xor
static VALUE true_xor(VALUE obj, VALUE obj2)
Definition: object.c:1197
recur
#define recur(fmt)
Definition: date_strptime.c:147
rb_obj_equal
VALUE rb_obj_equal(VALUE obj1, VALUE obj2)
Definition: object.c:109
rb_is_const_name
int rb_is_const_name(VALUE name)
Definition: ripper.c:17174
rb_class2name
const char * rb_class2name(VALUE)
Definition: variable.c:389
rb_ivar_set
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1128
rb_obj_init_copy
VALUE rb_obj_init_copy(VALUE, VALUE)
Definition: object.c:354
arg
arg
Definition: ripper.y:1315
rb_class_protected_instance_methods
VALUE rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1086
rb_Integer
VALUE rb_Integer(VALUE)
Definition: object.c:2488
NEWOBJ_OF
#define NEWOBJ_OF(obj, type, klass, flags)
rb_enc_get
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:772
conv_method_tbl::method
const char * method
Definition: object.c:2319
rb_class_public_instance_methods
VALUE rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1124
rb_singleton_class_attached
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attach a object to a singleton class.
Definition: class.c:289
NUM2LONG
#define NUM2LONG(x)
SYMBOL_P
#define SYMBOL_P(x)
rb_check_to_int
VALUE rb_check_to_int(VALUE)
Definition: object.c:2437
FL_SINGLETON
#define FL_SINGLETON
rb_cvar_set
void rb_cvar_set(VALUE, ID, VALUE)
Definition: variable.c:2329
rb_module_new
VALUE rb_module_new(void)
Definition: class.c:596
Qundef
#define Qundef
rb_obj_is_kind_of
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:582
rb_method_basic_definition_p
int rb_method_basic_definition_p(VALUE, ID)
Definition: vm_method.c:1498
T_CLASS
#define T_CLASS
convert_type
static VALUE convert_type(VALUE val, const char *tname, const char *method, int raise)
Definition: object.c:2336
rb_enc_str_asciionly_p
int rb_enc_str_asciionly_p(VALUE)
Definition: string.c:340
rb_obj_singleton_methods
VALUE rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1248
true_and
static VALUE true_and(VALUE obj, VALUE obj2)
Definition: object.c:1159
rb_mod_remove_const
VALUE rb_mod_remove_const(VALUE, VALUE)
Definition: variable.c:1916
rb_check_array_type
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:557
rb_f_string
static VALUE rb_f_string(VALUE obj, VALUE arg)
Definition: object.c:2760
rb_obj_inspect
static VALUE rb_obj_inspect(VALUE obj)
Definition: object.c:496
rb_cClass
RUBY_EXTERN VALUE rb_cClass
Definition: ripper.y:1430
nil_to_a
static VALUE nil_to_a(VALUE obj)
Definition: object.c:1091
rb_mod_const_missing
VALUE rb_mod_const_missing(VALUE, VALUE)
Definition: variable.c:1518
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ripper.y:1426
st_index_t
st_data_t st_index_t
Definition: ripper.y:63
ALLOC_N
#define ALLOC_N(type, n)
LONG2FIX
#define LONG2FIX(i)
RBASIC
#define RBASIC(obj)
rb_obj_init_dup_clone
VALUE rb_obj_init_dup_clone(VALUE obj, VALUE orig)
Definition: object.c:367
klass
klass
Definition: tcltklib.c:3504
rb_obj_methods
VALUE rb_obj_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1151
rb_get_alloc_func
rb_alloc_func_t rb_get_alloc_func(VALUE)
Definition: vm_method.c:488
inspect_obj
static VALUE inspect_obj(VALUE obj, VALUE str, int recur)
Definition: object.c:446
st_insert
int st_insert(st_table *, st_data_t, st_data_t)
st.h
rb_mod_cvar_defined
static VALUE rb_mod_cvar_defined(VALUE obj, VALUE iv)
Definition: object.c:2298
rb_exec_recursive
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4852
rb_class_s_alloc
static VALUE rb_class_s_alloc(VALUE klass)
Definition: object.c:1602
rb_mod_to_s
static VALUE rb_mod_to_s(VALUE klass)
Definition: object.c:1401
rb_ary_new2
VALUE rb_ary_new2(long capa)
Definition: array.c:417
OBJ_UNTRUST
#define OBJ_UNTRUST(x)
rb_is_class_id
int rb_is_class_id(ID id)
Definition: ripper.c:17052
rb_mod_const_set
static VALUE rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
Definition: object.c:2049
rb_safe_level
#define rb_safe_level()
Definition: tcltklib.c:94
id_eq
static ID id_eq
Definition: object.c:38
T_MODULE
#define T_MODULE
inspect_i
static int inspect_i(st_data_t k, st_data_t v, st_data_t a)
Definition: object.c:417
rb_mod_const_defined
static VALUE rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
Definition: object.c:2077
rb_enc_asciicompat
#define rb_enc_asciicompat(enc)
NUM2INT
#define NUM2INT(x)
rb_hash_new
VALUE rb_hash_new(void)
Definition: hash.c:234
rb_is_instance_name
int rb_is_instance_name(VALUE name)
Definition: ripper.c:17192
rb_obj_alloc
VALUE rb_obj_alloc(VALUE)
Definition: object.c:1721
rb_id2name
const char * rb_id2name(ID id)
Definition: ripper.c:17005
rb_obj_not_equal
VALUE rb_obj_not_equal(VALUE obj1, VALUE obj2)
Definition: object.c:164
rb_mod_included_modules
VALUE rb_mod_included_modules(VALUE mod)
Definition: class.c:854
BUILTIN_TYPE
#define BUILTIN_TYPE(x)
rb_class_real
VALUE rb_class_real(VALUE)
Definition: object.c:171
PRIsVALUE
#define PRIsVALUE
OBJ_UNTRUSTED
#define OBJ_UNTRUSTED(x)
RObject
Definition: ripper.y:709
rb_false
static VALUE rb_false(VALUE obj)
Definition: object.c:1300
e
BDIGIT e
Definition: bigdecimal.c:5085
VALUE
unsigned long VALUE
Definition: ripper.y:104
rb_ivar_count
st_index_t rb_ivar_count(VALUE)
Definition: variable.c:1299
rb_f_integer
static VALUE rb_f_integer(int argc, VALUE *argv, VALUE obj)
Definition: object.c:2516
rb_warning
void rb_warning(const char *fmt,...)
Definition: error.c:229
rb_class_get_superclass
VALUE rb_class_get_superclass(VALUE)
Definition: object.c:1823
rb_to_float
VALUE rb_to_float(VALUE)
Definition: object.c:2692
rb_intern3
ID rb_intern3(const char *name, long len, rb_encoding *enc)
Definition: ripper.c:16793
st_copy
st_table * st_copy(st_table *)
Definition: st.c:658
false_to_s
static VALUE false_to_s(VALUE obj)
Definition: object.c:1221
SPECIAL_CONST_P
#define SPECIAL_CONST_P(x)
OBJ_TAINT
#define OBJ_TAINT(x)
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:617
rb_cFalseClass
RUBY_EXTERN VALUE rb_cFalseClass
Definition: ripper.y:1434
rb_intern
#define rb_intern(str)
v
BDIGIT v
Definition: bigdecimal.c:5656
rb_mod_gt
static VALUE rb_mod_gt(VALUE mod, VALUE arg)
Definition: object.c:1556
rb_ivar_foreach
void rb_ivar_foreach(VALUE, int(*)(ANYARGS), st_data_t)
Definition: variable.c:1271
mod
#define mod(x, y)
Definition: date_strftime.c:28
rb_cData
RUBY_EXTERN VALUE rb_cData
Definition: ripper.y:1433
RCLASS_M_TBL
#define RCLASS_M_TBL(c)
immediate_frozen_tbl
static st_table * immediate_frozen_tbl
Definition: object.c:965
class_or_module_required
static VALUE class_or_module_required(VALUE c)
Definition: object.c:511
NULL
#define NULL
Definition: _sdbm.c:103
q
q
Definition: tcltklib.c:2968
T_DATA
#define T_DATA
name
const char * name
Definition: nkf.c:208
rb_check_string_type
VALUE rb_check_string_type(VALUE)
Definition: string.c:1508
conv_method_tbl::id
ID id
Definition: object.c:2320
rb_obj_call_init
void rb_obj_call_init(VALUE obj, int argc, VALUE *argv)
Definition: eval.c:1227
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
rb_invalid_str
void rb_invalid_str(const char *str, const char *type)
Definition: error.c:1139
rb_class_boot
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition: class.c:76
rb_obj_instance_variables
#define rb_obj_instance_variables(object)
Definition: generator.h:21
rb_eArgError
VALUE rb_eArgError
Definition: error.c:512
rb_cNumeric
RUBY_EXTERN VALUE rb_cNumeric
Definition: ripper.y:1448
cmp
static ID cmp
Definition: compar.c:16
rb_convert_type
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2368
rb_check_convert_type
VALUE rb_check_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2383
T_MASK
#define T_MASK
Definition: md5.c:131
RUBY_DTRACE_OBJECT_CREATE
#define RUBY_DTRACE_OBJECT_CREATE(arg0, arg1, arg2)
Definition: probes.h:52
st_free_table
void st_free_table(st_table *)
Definition: st.c:334
rb_mod_module_exec
VALUE rb_mod_module_exec(int, VALUE *, VALUE)
Definition: vm_eval.c:1695
rb_class_superclass
VALUE rb_class_superclass(VALUE)
Definition: object.c:1805
FL_UNSET
#define FL_UNSET(x, f)
ISSPACE
#define ISSPACE(c)
Definition: ruby.h:1632
rb_singleton_class_clone_and_attach
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
Definition: class.c:247
id_const_missing
static ID id_const_missing
Definition: object.c:40
rb_inspect
VALUE rb_inspect(VALUE)
Definition: object.c:402
rb_obj_infect
void rb_obj_infect(VALUE, VALUE)
Definition: object.c:960
OnigEncodingTypeST
Definition: ripper.y:161
ALLOCV_END
#define ALLOCV_END(v)
rb_module_s_alloc
static VALUE rb_module_s_alloc(VALUE klass)
Definition: object.c:1593
CLASS_OR_MODULE_P
#define CLASS_OR_MODULE_P(obj)
Definition: object.c:42
rb_Array
VALUE rb_Array(VALUE)
Definition: object.c:2766
id_init_copy
static ID id_init_copy
Definition: object.c:39
len
size_t len
Definition: tcltklib.c:3568