Ruby 2.1.5p273(2014-11-13revision48405)

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