Ruby 3.2.3p157 (2024-01-18 revision 52bb2ac0a6971d0391efa2275f7a66bff319087c)
object.c
1/**********************************************************************
2
3 object.c -
4
5 $Author$
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/internal/config.h"
15
16#include <ctype.h>
17#include <errno.h>
18#include <float.h>
19#include <math.h>
20#include <stdio.h>
21
22#include "constant.h"
23#include "id.h"
24#include "internal.h"
25#include "internal/array.h"
26#include "internal/class.h"
27#include "internal/error.h"
28#include "internal/eval.h"
29#include "internal/inits.h"
30#include "internal/numeric.h"
31#include "internal/object.h"
32#include "internal/struct.h"
33#include "internal/string.h"
34#include "internal/symbol.h"
35#include "internal/variable.h"
36#include "variable.h"
37#include "probes.h"
38#include "ruby/encoding.h"
39#include "ruby/st.h"
40#include "ruby/util.h"
41#include "ruby/assert.h"
42#include "builtin.h"
43#include "shape.h"
44
56
60
61static VALUE rb_cNilClass_to_s;
62static VALUE rb_cTrueClass_to_s;
63static VALUE rb_cFalseClass_to_s;
64
67#define id_eq idEq
68#define id_eql idEqlP
69#define id_match idEqTilde
70#define id_inspect idInspect
71#define id_init_copy idInitialize_copy
72#define id_init_clone idInitialize_clone
73#define id_init_dup idInitialize_dup
74#define id_const_missing idConst_missing
75#define id_to_f idTo_f
76
77#define CLASS_OR_MODULE_P(obj) \
78 (!SPECIAL_CONST_P(obj) && \
79 (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
80
85{
86 if (!SPECIAL_CONST_P(obj)) {
87 RBASIC_CLEAR_CLASS(obj);
88 }
89 return obj;
90}
91
94{
95 if (!SPECIAL_CONST_P(obj)) {
96 RBASIC_SET_CLASS(obj, klass);
97 }
98 return obj;
99}
100
101VALUE
103{
104 RBASIC(obj)->flags = type;
105 RBASIC_SET_CLASS(obj, klass);
106 return obj;
107}
108
117#define case_equal rb_equal
118 /* The default implementation of #=== is
119 * to call #== with the rb_equal() optimization. */
120
121VALUE
123{
124 VALUE result;
125
126 if (obj1 == obj2) return Qtrue;
127 result = rb_equal_opt(obj1, obj2);
128 if (UNDEF_P(result)) {
129 result = rb_funcall(obj1, id_eq, 1, obj2);
130 }
131 return RBOOL(RTEST(result));
132}
133
134int
135rb_eql(VALUE obj1, VALUE obj2)
136{
137 VALUE result;
138
139 if (obj1 == obj2) return TRUE;
140 result = rb_eql_opt(obj1, obj2);
141 if (UNDEF_P(result)) {
142 result = rb_funcall(obj1, id_eql, 1, obj2);
143 }
144 return RTEST(result);
145}
146
150MJIT_FUNC_EXPORTED VALUE
151rb_obj_equal(VALUE obj1, VALUE obj2)
152{
153 return RBOOL(obj1 == obj2);
154}
155
156VALUE rb_obj_hash(VALUE obj);
157
162MJIT_FUNC_EXPORTED VALUE
163rb_obj_not(VALUE obj)
164{
165 return RBOOL(!RTEST(obj));
166}
167
172MJIT_FUNC_EXPORTED VALUE
173rb_obj_not_equal(VALUE obj1, VALUE obj2)
174{
175 VALUE result = rb_funcall(obj1, id_eq, 1, obj2);
176 return rb_obj_not(result);
177}
178
179VALUE
181{
182 while (cl &&
183 ((RBASIC(cl)->flags & FL_SINGLETON) || BUILTIN_TYPE(cl) == T_ICLASS)) {
184 cl = RCLASS_SUPER(cl);
185 }
186 return cl;
187}
188
189VALUE
191{
192 return rb_class_real(CLASS_OF(obj));
193}
194
195/*
196 * call-seq:
197 * obj.singleton_class -> class
198 *
199 * Returns the singleton class of <i>obj</i>. This method creates
200 * a new singleton class if <i>obj</i> does not have one.
201 *
202 * If <i>obj</i> is <code>nil</code>, <code>true</code>, or
203 * <code>false</code>, it returns NilClass, TrueClass, or FalseClass,
204 * respectively.
205 * If <i>obj</i> is an Integer, a Float or a Symbol, it raises a TypeError.
206 *
207 * Object.new.singleton_class #=> #<Class:#<Object:0xb7ce1e24>>
208 * String.singleton_class #=> #<Class:String>
209 * nil.singleton_class #=> NilClass
210 */
211
212static VALUE
213rb_obj_singleton_class(VALUE obj)
214{
215 return rb_singleton_class(obj);
216}
217
219MJIT_FUNC_EXPORTED void
220rb_obj_copy_ivar(VALUE dest, VALUE obj)
221{
223
225 rb_shape_t * src_shape = rb_shape_get_shape(obj);
226
227 if (rb_shape_id(src_shape) == OBJ_TOO_COMPLEX_SHAPE_ID) {
228 st_table * table = rb_st_init_numtable_with_size(rb_st_table_size(ROBJECT_IV_HASH(obj)));
229
230 rb_ivar_foreach(obj, rb_obj_evacuate_ivs_to_hash_table, (st_data_t)table);
231 rb_shape_set_too_complex(dest);
232
233 ROBJECT(dest)->as.heap.ivptr = (VALUE *)table;
234
235 return;
236 }
237
238 uint32_t src_num_ivs = RBASIC_IV_COUNT(obj);
239 rb_shape_t * shape_to_set_on_dest = src_shape;
240 VALUE * src_buf;
241 VALUE * dest_buf;
242
243 if (!src_num_ivs) {
244 return;
245 }
246
247 // The copy should be mutable, so we don't want the frozen shape
248 if (rb_shape_frozen_shape_p(src_shape)) {
249 shape_to_set_on_dest = rb_shape_get_parent(src_shape);
250 }
251
252 src_buf = ROBJECT_IVPTR(obj);
253 dest_buf = ROBJECT_IVPTR(dest);
254
255 rb_shape_t * initial_shape = rb_shape_get_shape(dest);
256
257 if (initial_shape->size_pool_index != src_shape->size_pool_index) {
258 RUBY_ASSERT(initial_shape->type == SHAPE_T_OBJECT);
259
260 shape_to_set_on_dest = rb_shape_rebuild_shape(initial_shape, src_shape);
261 }
262
263 RUBY_ASSERT(src_num_ivs <= shape_to_set_on_dest->capacity);
264 if (initial_shape->capacity < shape_to_set_on_dest->capacity) {
265 rb_ensure_iv_list_size(dest, initial_shape->capacity, shape_to_set_on_dest->capacity);
266 dest_buf = ROBJECT_IVPTR(dest);
267 }
268
269 MEMCPY(dest_buf, src_buf, VALUE, src_num_ivs);
270
271 // Fire write barriers
272 for (uint32_t i = 0; i < src_num_ivs; i++) {
273 RB_OBJ_WRITTEN(dest, Qundef, dest_buf[i]);
274 }
275
276 rb_shape_set_shape(dest, shape_to_set_on_dest);
277}
278
279static void
280init_copy(VALUE dest, VALUE obj)
281{
282 if (OBJ_FROZEN(dest)) {
283 rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
284 }
285 RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
286 // Copies the shape id from obj to dest
287 RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR);
288 rb_copy_wb_protected_attribute(dest, obj);
289 rb_copy_generic_ivar(dest, obj);
290 rb_gc_copy_finalizer(dest, obj);
291
292 if (RB_TYPE_P(obj, T_OBJECT)) {
293 rb_obj_copy_ivar(dest, obj);
294 }
295}
296
297static VALUE immutable_obj_clone(VALUE obj, VALUE kwfreeze);
298static VALUE mutable_obj_clone(VALUE obj, VALUE kwfreeze);
299PUREFUNC(static inline int special_object_p(VALUE obj));
300static inline int
301special_object_p(VALUE obj)
302{
303 if (SPECIAL_CONST_P(obj)) return TRUE;
304 switch (BUILTIN_TYPE(obj)) {
305 case T_BIGNUM:
306 case T_FLOAT:
307 case T_SYMBOL:
308 case T_RATIONAL:
309 case T_COMPLEX:
310 /* not a comprehensive list */
311 return TRUE;
312 default:
313 return FALSE;
314 }
315}
316
317static VALUE
318obj_freeze_opt(VALUE freeze)
319{
320 switch (freeze) {
321 case Qfalse:
322 case Qtrue:
323 case Qnil:
324 break;
325 default:
326 rb_raise(rb_eArgError, "unexpected value for freeze: %"PRIsVALUE, rb_obj_class(freeze));
327 }
328
329 return freeze;
330}
331
332static VALUE
333rb_obj_clone2(rb_execution_context_t *ec, VALUE obj, VALUE freeze)
334{
335 VALUE kwfreeze = obj_freeze_opt(freeze);
336 if (!special_object_p(obj))
337 return mutable_obj_clone(obj, kwfreeze);
338 return immutable_obj_clone(obj, kwfreeze);
339}
340
342VALUE
343rb_immutable_obj_clone(int argc, VALUE *argv, VALUE obj)
344{
345 VALUE kwfreeze = rb_get_freeze_opt(argc, argv);
346 return immutable_obj_clone(obj, kwfreeze);
347}
348
349VALUE
350rb_get_freeze_opt(int argc, VALUE *argv)
351{
352 static ID keyword_ids[1];
353 VALUE opt;
354 VALUE kwfreeze = Qnil;
355
356 if (!keyword_ids[0]) {
357 CONST_ID(keyword_ids[0], "freeze");
358 }
359 rb_scan_args(argc, argv, "0:", &opt);
360 if (!NIL_P(opt)) {
361 rb_get_kwargs(opt, keyword_ids, 0, 1, &kwfreeze);
362 if (!UNDEF_P(kwfreeze))
363 kwfreeze = obj_freeze_opt(kwfreeze);
364 }
365 return kwfreeze;
366}
367
368static VALUE
369immutable_obj_clone(VALUE obj, VALUE kwfreeze)
370{
371 if (kwfreeze == Qfalse)
372 rb_raise(rb_eArgError, "can't unfreeze %"PRIsVALUE,
373 rb_obj_class(obj));
374 return obj;
375}
376
377static VALUE
378mutable_obj_clone(VALUE obj, VALUE kwfreeze)
379{
380 VALUE clone, singleton;
381 VALUE argv[2];
382
383 clone = rb_obj_alloc(rb_obj_class(obj));
384
385 singleton = rb_singleton_class_clone_and_attach(obj, clone);
386 RBASIC_SET_CLASS(clone, singleton);
387 if (FL_TEST(singleton, FL_SINGLETON)) {
388 rb_singleton_class_attached(singleton, clone);
389 }
390
391 init_copy(clone, obj);
392
393 switch (kwfreeze) {
394 case Qnil:
395 rb_funcall(clone, id_init_clone, 1, obj);
396 RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
397 if (RB_OBJ_FROZEN(obj)) {
398 rb_shape_transition_shape_frozen(clone);
399 }
400 break;
401 case Qtrue:
402 {
403 static VALUE freeze_true_hash;
404 if (!freeze_true_hash) {
405 freeze_true_hash = rb_hash_new();
406 rb_gc_register_mark_object(freeze_true_hash);
407 rb_hash_aset(freeze_true_hash, ID2SYM(idFreeze), Qtrue);
408 rb_obj_freeze(freeze_true_hash);
409 }
410
411 argv[0] = obj;
412 argv[1] = freeze_true_hash;
413 rb_funcallv_kw(clone, id_init_clone, 2, argv, RB_PASS_KEYWORDS);
414 RBASIC(clone)->flags |= FL_FREEZE;
415 rb_shape_transition_shape_frozen(clone);
416 break;
417 }
418 case Qfalse:
419 {
420 static VALUE freeze_false_hash;
421 if (!freeze_false_hash) {
422 freeze_false_hash = rb_hash_new();
423 rb_gc_register_mark_object(freeze_false_hash);
424 rb_hash_aset(freeze_false_hash, ID2SYM(idFreeze), Qfalse);
425 rb_obj_freeze(freeze_false_hash);
426 }
427
428 argv[0] = obj;
429 argv[1] = freeze_false_hash;
430 rb_funcallv_kw(clone, id_init_clone, 2, argv, RB_PASS_KEYWORDS);
431 break;
432 }
433 default:
434 rb_bug("invalid kwfreeze passed to mutable_obj_clone");
435 }
436
437 return clone;
438}
439
440VALUE
442{
443 if (special_object_p(obj)) return obj;
444 return mutable_obj_clone(obj, Qnil);
445}
446
447/*
448 * call-seq:
449 * obj.dup -> an_object
450 *
451 * Produces a shallow copy of <i>obj</i>---the instance variables of
452 * <i>obj</i> are copied, but not the objects they reference.
453 *
454 * This method may have class-specific behavior. If so, that
455 * behavior will be documented under the #+initialize_copy+ method of
456 * the class.
457 *
458 * === on dup vs clone
459 *
460 * In general, #clone and #dup may have different semantics in
461 * descendant classes. While #clone is used to duplicate an object,
462 * including its internal state, #dup typically uses the class of the
463 * descendant object to create the new instance.
464 *
465 * When using #dup, any modules that the object has been extended with will not
466 * be copied.
467 *
468 * class Klass
469 * attr_accessor :str
470 * end
471 *
472 * module Foo
473 * def foo; 'foo'; end
474 * end
475 *
476 * s1 = Klass.new #=> #<Klass:0x401b3a38>
477 * s1.extend(Foo) #=> #<Klass:0x401b3a38>
478 * s1.foo #=> "foo"
479 *
480 * s2 = s1.clone #=> #<Klass:0x401be280>
481 * s2.foo #=> "foo"
482 *
483 * s3 = s1.dup #=> #<Klass:0x401c1084>
484 * s3.foo #=> NoMethodError: undefined method `foo' for #<Klass:0x401c1084>
485 */
486VALUE
488{
489 VALUE dup;
490
491 if (special_object_p(obj)) {
492 return obj;
493 }
494 dup = rb_obj_alloc(rb_obj_class(obj));
495 init_copy(dup, obj);
496 rb_funcall(dup, id_init_dup, 1, obj);
497
498 return dup;
499}
500
501/*
502 * call-seq:
503 * obj.itself -> obj
504 *
505 * Returns the receiver.
506 *
507 * string = "my string"
508 * string.itself.object_id == string.object_id #=> true
509 *
510 */
511
512static VALUE
513rb_obj_itself(VALUE obj)
514{
515 return obj;
516}
517
518VALUE
519rb_obj_size(VALUE self, VALUE args, VALUE obj)
520{
521 return LONG2FIX(1);
522}
523
524static VALUE
525block_given_p(rb_execution_context_t *ec, VALUE self)
526{
527 return RBOOL(rb_block_given_p());
528}
529
535VALUE
537{
538 if (obj == orig) return obj;
539 rb_check_frozen(obj);
540 if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
541 rb_raise(rb_eTypeError, "initialize_copy should take same class object");
542 }
543 return obj;
544}
545
552VALUE
554{
555 rb_funcall(obj, id_init_copy, 1, orig);
556 return obj;
557}
558
566static VALUE
567rb_obj_init_clone(int argc, VALUE *argv, VALUE obj)
568{
569 VALUE orig, opts;
570 if (rb_scan_args(argc, argv, "1:", &orig, &opts) < argc) {
571 /* Ignore a freeze keyword */
572 rb_get_freeze_opt(1, &opts);
573 }
574 rb_funcall(obj, id_init_copy, 1, orig);
575 return obj;
576}
577
578/*
579 * call-seq:
580 * obj.to_s -> string
581 *
582 * Returns a string representing <i>obj</i>. The default #to_s prints
583 * the object's class and an encoding of the object id. As a special
584 * case, the top-level object that is the initial execution context
585 * of Ruby programs returns ``main''.
586 *
587 */
588VALUE
590{
591 VALUE str;
592 VALUE cname = rb_class_name(CLASS_OF(obj));
593
594 str = rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)obj);
595
596 return str;
597}
598
599VALUE
601{
602 VALUE str = rb_obj_as_string(rb_funcallv(obj, id_inspect, 0, 0));
603
604 rb_encoding *enc = rb_default_internal_encoding();
605 if (enc == NULL) enc = rb_default_external_encoding();
606 if (!rb_enc_asciicompat(enc)) {
607 if (!rb_enc_str_asciionly_p(str))
608 return rb_str_escape(str);
609 return str;
610 }
611 if (rb_enc_get(str) != enc && !rb_enc_str_asciionly_p(str))
612 return rb_str_escape(str);
613 return str;
614}
615
616static int
617inspect_i(st_data_t k, st_data_t v, st_data_t a)
618{
619 ID id = (ID)k;
620 VALUE value = (VALUE)v;
621 VALUE str = (VALUE)a;
622
623 /* need not to show internal data */
624 if (CLASS_OF(value) == 0) return ST_CONTINUE;
625 if (!rb_is_instance_id(id)) return ST_CONTINUE;
626 if (RSTRING_PTR(str)[0] == '-') { /* first element */
627 RSTRING_PTR(str)[0] = '#';
628 rb_str_cat2(str, " ");
629 }
630 else {
631 rb_str_cat2(str, ", ");
632 }
633 rb_str_catf(str, "%"PRIsVALUE"=", rb_id2str(id));
634 rb_str_buf_append(str, rb_inspect(value));
635
636 return ST_CONTINUE;
637}
638
639static VALUE
640inspect_obj(VALUE obj, VALUE str, int recur)
641{
642 if (recur) {
643 rb_str_cat2(str, " ...");
644 }
645 else {
646 rb_ivar_foreach(obj, inspect_i, str);
647 }
648 rb_str_cat2(str, ">");
649 RSTRING_PTR(str)[0] = '#';
650
651 return str;
652}
653
654/*
655 * call-seq:
656 * obj.inspect -> string
657 *
658 * Returns a string containing a human-readable representation of <i>obj</i>.
659 * The default #inspect shows the object's class name, an encoding of
660 * its memory address, and a list of the instance variables and their
661 * values (by calling #inspect on each of them). User defined classes
662 * should override this method to provide a better representation of
663 * <i>obj</i>. When overriding this method, it should return a string
664 * whose encoding is compatible with the default external encoding.
665 *
666 * [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
667 * Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
668 *
669 * class Foo
670 * end
671 * Foo.new.inspect #=> "#<Foo:0x0300c868>"
672 *
673 * class Bar
674 * def initialize
675 * @bar = 1
676 * end
677 * end
678 * Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
679 */
680
681static VALUE
682rb_obj_inspect(VALUE obj)
683{
684 if (rb_ivar_count(obj) > 0) {
685 VALUE str;
686 VALUE c = rb_class_name(CLASS_OF(obj));
687
688 str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void*)obj);
689 return rb_exec_recursive(inspect_obj, obj, str);
690 }
691 else {
692 return rb_any_to_s(obj);
693 }
694}
695
696static VALUE
697class_or_module_required(VALUE c)
698{
699 switch (OBJ_BUILTIN_TYPE(c)) {
700 case T_MODULE:
701 case T_CLASS:
702 case T_ICLASS:
703 break;
704
705 default:
706 rb_raise(rb_eTypeError, "class or module required");
707 }
708 return c;
709}
710
711static VALUE class_search_ancestor(VALUE cl, VALUE c);
712
713/*
714 * call-seq:
715 * obj.instance_of?(class) -> true or false
716 *
717 * Returns <code>true</code> if <i>obj</i> is an instance of the given
718 * class. See also Object#kind_of?.
719 *
720 * class A; end
721 * class B < A; end
722 * class C < B; end
723 *
724 * b = B.new
725 * b.instance_of? A #=> false
726 * b.instance_of? B #=> true
727 * b.instance_of? C #=> false
728 */
729
730VALUE
732{
733 c = class_or_module_required(c);
734 return RBOOL(rb_obj_class(obj) == c);
735}
736
737// Returns whether c is a proper (c != cl) subclass of cl
738// Both c and cl must be T_CLASS
739static VALUE
740class_search_class_ancestor(VALUE cl, VALUE c)
741{
744
745 size_t c_depth = RCLASS_SUPERCLASS_DEPTH(c);
746 size_t cl_depth = RCLASS_SUPERCLASS_DEPTH(cl);
747 VALUE *classes = RCLASS_SUPERCLASSES(cl);
748
749 // If c's inheritance chain is longer, it cannot be an ancestor
750 // We are checking for a proper subclass so don't check if they are equal
751 if (cl_depth <= c_depth)
752 return Qfalse;
753
754 // Otherwise check that c is in cl's inheritance chain
755 return RBOOL(classes[c_depth] == c);
756}
757
758/*
759 * call-seq:
760 * obj.is_a?(class) -> true or false
761 * obj.kind_of?(class) -> true or false
762 *
763 * Returns <code>true</code> if <i>class</i> is the class of
764 * <i>obj</i>, or if <i>class</i> is one of the superclasses of
765 * <i>obj</i> or modules included in <i>obj</i>.
766 *
767 * module M; end
768 * class A
769 * include M
770 * end
771 * class B < A; end
772 * class C < B; end
773 *
774 * b = B.new
775 * b.is_a? A #=> true
776 * b.is_a? B #=> true
777 * b.is_a? C #=> false
778 * b.is_a? M #=> true
779 *
780 * b.kind_of? A #=> true
781 * b.kind_of? B #=> true
782 * b.kind_of? C #=> false
783 * b.kind_of? M #=> true
784 */
785
786VALUE
788{
789 VALUE cl = CLASS_OF(obj);
790
792
793 // Fastest path: If the object's class is an exact match we know `c` is a
794 // class without checking type and can return immediately.
795 if (cl == c) return Qtrue;
796
797 // Note: YJIT needs this function to never allocate and never raise when
798 // `c` is a class or a module.
799
800 if (LIKELY(RB_TYPE_P(c, T_CLASS))) {
801 // Fast path: Both are T_CLASS
802 return class_search_class_ancestor(cl, c);
803 }
804 else if (RB_TYPE_P(c, T_ICLASS)) {
805 // First check if we inherit the includer
806 // If we do we can return true immediately
807 VALUE includer = RCLASS_INCLUDER(c);
808 if (cl == includer) return Qtrue;
809
810 // Usually includer is a T_CLASS here, except when including into an
811 // already included Module.
812 // If it is a class, attempt the fast class-to-class check and return
813 // true if there is a match.
814 if (RB_TYPE_P(includer, T_CLASS) && class_search_class_ancestor(cl, includer))
815 return Qtrue;
816
817 // We don't include the ICLASS directly, so must check if we inherit
818 // the module via another include
819 return RBOOL(class_search_ancestor(cl, RCLASS_ORIGIN(c)));
820 }
821 else if (RB_TYPE_P(c, T_MODULE)) {
822 // Slow path: check each ancestor in the linked list and its method table
823 return RBOOL(class_search_ancestor(cl, RCLASS_ORIGIN(c)));
824 }
825 else {
826 rb_raise(rb_eTypeError, "class or module required");
828 }
829}
830
831
832static VALUE
833class_search_ancestor(VALUE cl, VALUE c)
834{
835 while (cl) {
836 if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
837 return cl;
838 cl = RCLASS_SUPER(cl);
839 }
840 return 0;
841}
842
844VALUE
846{
847 cl = class_or_module_required(cl);
848 c = class_or_module_required(c);
849 return class_search_ancestor(cl, RCLASS_ORIGIN(c));
850}
851
852
853/*
854 * Document-method: inherited
855 *
856 * call-seq:
857 * inherited(subclass)
858 *
859 * Callback invoked whenever a subclass of the current class is created.
860 *
861 * Example:
862 *
863 * class Foo
864 * def self.inherited(subclass)
865 * puts "New subclass: #{subclass}"
866 * end
867 * end
868 *
869 * class Bar < Foo
870 * end
871 *
872 * class Baz < Bar
873 * end
874 *
875 * <em>produces:</em>
876 *
877 * New subclass: Bar
878 * New subclass: Baz
879 */
880#define rb_obj_class_inherited rb_obj_dummy1
881
882/* Document-method: method_added
883 *
884 * call-seq:
885 * method_added(method_name)
886 *
887 * Invoked as a callback whenever an instance method is added to the
888 * receiver.
889 *
890 * module Chatty
891 * def self.method_added(method_name)
892 * puts "Adding #{method_name.inspect}"
893 * end
894 * def self.some_class_method() end
895 * def some_instance_method() end
896 * end
897 *
898 * <em>produces:</em>
899 *
900 * Adding :some_instance_method
901 *
902 */
903#define rb_obj_mod_method_added rb_obj_dummy1
904
905/* Document-method: method_removed
906 *
907 * call-seq:
908 * method_removed(method_name)
909 *
910 * Invoked as a callback whenever an instance method is removed from the
911 * receiver.
912 *
913 * module Chatty
914 * def self.method_removed(method_name)
915 * puts "Removing #{method_name.inspect}"
916 * end
917 * def self.some_class_method() end
918 * def some_instance_method() end
919 * class << self
920 * remove_method :some_class_method
921 * end
922 * remove_method :some_instance_method
923 * end
924 *
925 * <em>produces:</em>
926 *
927 * Removing :some_instance_method
928 *
929 */
930#define rb_obj_mod_method_removed rb_obj_dummy1
931
932/* Document-method: method_undefined
933 *
934 * call-seq:
935 * method_undefined(method_name)
936 *
937 * Invoked as a callback whenever an instance method is undefined from the
938 * receiver.
939 *
940 * module Chatty
941 * def self.method_undefined(method_name)
942 * puts "Undefining #{method_name.inspect}"
943 * end
944 * def self.some_class_method() end
945 * def some_instance_method() end
946 * class << self
947 * undef_method :some_class_method
948 * end
949 * undef_method :some_instance_method
950 * end
951 *
952 * <em>produces:</em>
953 *
954 * Undefining :some_instance_method
955 *
956 */
957#define rb_obj_mod_method_undefined rb_obj_dummy1
958
959/*
960 * Document-method: singleton_method_added
961 *
962 * call-seq:
963 * singleton_method_added(symbol)
964 *
965 * Invoked as a callback whenever a singleton method is added to the
966 * receiver.
967 *
968 * module Chatty
969 * def Chatty.singleton_method_added(id)
970 * puts "Adding #{id.id2name}"
971 * end
972 * def self.one() end
973 * def two() end
974 * def Chatty.three() end
975 * end
976 *
977 * <em>produces:</em>
978 *
979 * Adding singleton_method_added
980 * Adding one
981 * Adding three
982 *
983 */
984#define rb_obj_singleton_method_added rb_obj_dummy1
985
986/*
987 * Document-method: singleton_method_removed
988 *
989 * call-seq:
990 * singleton_method_removed(symbol)
991 *
992 * Invoked as a callback whenever a singleton method is removed from
993 * the receiver.
994 *
995 * module Chatty
996 * def Chatty.singleton_method_removed(id)
997 * puts "Removing #{id.id2name}"
998 * end
999 * def self.one() end
1000 * def two() end
1001 * def Chatty.three() end
1002 * class << self
1003 * remove_method :three
1004 * remove_method :one
1005 * end
1006 * end
1007 *
1008 * <em>produces:</em>
1009 *
1010 * Removing three
1011 * Removing one
1012 */
1013#define rb_obj_singleton_method_removed rb_obj_dummy1
1014
1015/*
1016 * Document-method: singleton_method_undefined
1017 *
1018 * call-seq:
1019 * singleton_method_undefined(symbol)
1020 *
1021 * Invoked as a callback whenever a singleton method is undefined in
1022 * the receiver.
1023 *
1024 * module Chatty
1025 * def Chatty.singleton_method_undefined(id)
1026 * puts "Undefining #{id.id2name}"
1027 * end
1028 * def Chatty.one() end
1029 * class << self
1030 * undef_method(:one)
1031 * end
1032 * end
1033 *
1034 * <em>produces:</em>
1035 *
1036 * Undefining one
1037 */
1038#define rb_obj_singleton_method_undefined rb_obj_dummy1
1039
1040/* Document-method: const_added
1041 *
1042 * call-seq:
1043 * const_added(const_name)
1044 *
1045 * Invoked as a callback whenever a constant is assigned on the receiver
1046 *
1047 * module Chatty
1048 * def self.const_added(const_name)
1049 * super
1050 * puts "Added #{const_name.inspect}"
1051 * end
1052 * FOO = 1
1053 * end
1054 *
1055 * <em>produces:</em>
1056 *
1057 * Added :FOO
1058 *
1059 */
1060#define rb_obj_mod_const_added rb_obj_dummy1
1061
1062/*
1063 * Document-method: extended
1064 *
1065 * call-seq:
1066 * extended(othermod)
1067 *
1068 * The equivalent of <tt>included</tt>, but for extended modules.
1069 *
1070 * module A
1071 * def self.extended(mod)
1072 * puts "#{self} extended in #{mod}"
1073 * end
1074 * end
1075 * module Enumerable
1076 * extend A
1077 * end
1078 * # => prints "A extended in Enumerable"
1079 */
1080#define rb_obj_mod_extended rb_obj_dummy1
1081
1082/*
1083 * Document-method: included
1084 *
1085 * call-seq:
1086 * included(othermod)
1087 *
1088 * Callback invoked whenever the receiver is included in another
1089 * module or class. This should be used in preference to
1090 * <tt>Module.append_features</tt> if your code wants to perform some
1091 * action when a module is included in another.
1092 *
1093 * module A
1094 * def A.included(mod)
1095 * puts "#{self} included in #{mod}"
1096 * end
1097 * end
1098 * module Enumerable
1099 * include A
1100 * end
1101 * # => prints "A included in Enumerable"
1102 */
1103#define rb_obj_mod_included rb_obj_dummy1
1104
1105/*
1106 * Document-method: prepended
1107 *
1108 * call-seq:
1109 * prepended(othermod)
1110 *
1111 * The equivalent of <tt>included</tt>, but for prepended modules.
1112 *
1113 * module A
1114 * def self.prepended(mod)
1115 * puts "#{self} prepended to #{mod}"
1116 * end
1117 * end
1118 * module Enumerable
1119 * prepend A
1120 * end
1121 * # => prints "A prepended to Enumerable"
1122 */
1123#define rb_obj_mod_prepended rb_obj_dummy1
1124
1125/*
1126 * Document-method: initialize
1127 *
1128 * call-seq:
1129 * BasicObject.new
1130 *
1131 * Returns a new BasicObject.
1132 */
1133#define rb_obj_initialize rb_obj_dummy0
1134
1135/*
1136 * Not documented
1137 */
1138
1139static VALUE
1140rb_obj_dummy(void)
1141{
1142 return Qnil;
1143}
1144
1145static VALUE
1146rb_obj_dummy0(VALUE _)
1147{
1148 return rb_obj_dummy();
1149}
1150
1151static VALUE
1152rb_obj_dummy1(VALUE _x, VALUE _y)
1153{
1154 return rb_obj_dummy();
1155}
1156
1157/*
1158 * call-seq:
1159 * obj.freeze -> obj
1160 *
1161 * Prevents further modifications to <i>obj</i>. A
1162 * FrozenError will be raised if modification is attempted.
1163 * There is no way to unfreeze a frozen object. See also
1164 * Object#frozen?.
1165 *
1166 * This method returns self.
1167 *
1168 * a = [ "a", "b", "c" ]
1169 * a.freeze
1170 * a << "z"
1171 *
1172 * <em>produces:</em>
1173 *
1174 * prog.rb:3:in `<<': can't modify frozen Array (FrozenError)
1175 * from prog.rb:3
1176 *
1177 * Objects of the following classes are always frozen: Integer,
1178 * Float, Symbol.
1179 */
1180
1181VALUE
1183{
1184 if (!OBJ_FROZEN(obj)) {
1185 OBJ_FREEZE(obj);
1186 if (SPECIAL_CONST_P(obj)) {
1187 rb_bug("special consts should be frozen.");
1188 }
1189 }
1190 return obj;
1191}
1192
1193VALUE
1195{
1196 return RBOOL(OBJ_FROZEN(obj));
1197}
1198
1199
1200/*
1201 * Document-class: NilClass
1202 *
1203 * The class of the singleton object <code>nil</code>.
1204 */
1205
1206/*
1207 * call-seq:
1208 * nil.to_s -> ""
1209 *
1210 * Always returns the empty string.
1211 */
1212
1213MJIT_FUNC_EXPORTED VALUE
1214rb_nil_to_s(VALUE obj)
1215{
1216 return rb_cNilClass_to_s;
1217}
1218
1219/*
1220 * Document-method: to_a
1221 *
1222 * call-seq:
1223 * nil.to_a -> []
1224 *
1225 * Always returns an empty array.
1226 *
1227 * nil.to_a #=> []
1228 */
1229
1230static VALUE
1231nil_to_a(VALUE obj)
1232{
1233 return rb_ary_new2(0);
1234}
1235
1236/*
1237 * Document-method: to_h
1238 *
1239 * call-seq:
1240 * nil.to_h -> {}
1241 *
1242 * Always returns an empty hash.
1243 *
1244 * nil.to_h #=> {}
1245 */
1246
1247static VALUE
1248nil_to_h(VALUE obj)
1249{
1250 return rb_hash_new();
1251}
1252
1253/*
1254 * call-seq:
1255 * nil.inspect -> "nil"
1256 *
1257 * Always returns the string "nil".
1258 */
1259
1260static VALUE
1261nil_inspect(VALUE obj)
1262{
1263 return rb_usascii_str_new2("nil");
1264}
1265
1266/*
1267 * call-seq:
1268 * nil =~ other -> nil
1269 *
1270 * Dummy pattern matching -- always returns nil.
1271 *
1272 * This method makes it possible to `while gets =~ /re/ do`.
1273 */
1274
1275static VALUE
1276nil_match(VALUE obj1, VALUE obj2)
1277{
1278 return Qnil;
1279}
1280
1281/***********************************************************************
1282 * Document-class: TrueClass
1283 *
1284 * The global value <code>true</code> is the only instance of class
1285 * TrueClass and represents a logically true value in
1286 * boolean expressions. The class provides operators allowing
1287 * <code>true</code> to be used in logical expressions.
1288 */
1289
1290
1291/*
1292 * call-seq:
1293 * true.to_s -> "true"
1294 *
1295 * The string representation of <code>true</code> is "true".
1296 */
1297
1298MJIT_FUNC_EXPORTED VALUE
1299rb_true_to_s(VALUE obj)
1300{
1301 return rb_cTrueClass_to_s;
1302}
1303
1304
1305/*
1306 * call-seq:
1307 * true & obj -> true or false
1308 *
1309 * And---Returns <code>false</code> if <i>obj</i> is
1310 * <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
1311 */
1312
1313static VALUE
1314true_and(VALUE obj, VALUE obj2)
1315{
1316 return RBOOL(RTEST(obj2));
1317}
1318
1319/*
1320 * call-seq:
1321 * true | obj -> true
1322 *
1323 * Or---Returns <code>true</code>. As <i>obj</i> is an argument to
1324 * a method call, it is always evaluated; there is no short-circuit
1325 * evaluation in this case.
1326 *
1327 * true | puts("or")
1328 * true || puts("logical or")
1329 *
1330 * <em>produces:</em>
1331 *
1332 * or
1333 */
1334
1335static VALUE
1336true_or(VALUE obj, VALUE obj2)
1337{
1338 return Qtrue;
1339}
1340
1341
1342/*
1343 * call-seq:
1344 * true ^ obj -> !obj
1345 *
1346 * Exclusive Or---Returns <code>true</code> if <i>obj</i> is
1347 * <code>nil</code> or <code>false</code>, <code>false</code>
1348 * otherwise.
1349 */
1350
1351static VALUE
1352true_xor(VALUE obj, VALUE obj2)
1353{
1354 return rb_obj_not(obj2);
1355}
1356
1357
1358/*
1359 * Document-class: FalseClass
1360 *
1361 * The global value <code>false</code> is the only instance of class
1362 * FalseClass and represents a logically false value in
1363 * boolean expressions. The class provides operators allowing
1364 * <code>false</code> to participate correctly in logical expressions.
1365 *
1366 */
1367
1368/*
1369 * call-seq:
1370 * false.to_s -> "false"
1371 *
1372 * The string representation of <code>false</code> is "false".
1373 */
1374
1375MJIT_FUNC_EXPORTED VALUE
1376rb_false_to_s(VALUE obj)
1377{
1378 return rb_cFalseClass_to_s;
1379}
1380
1381/*
1382 * call-seq:
1383 * false & obj -> false
1384 * nil & obj -> false
1385 *
1386 * And---Returns <code>false</code>. <i>obj</i> is always
1387 * evaluated as it is the argument to a method call---there is no
1388 * short-circuit evaluation in this case.
1389 */
1390
1391static VALUE
1392false_and(VALUE obj, VALUE obj2)
1393{
1394 return Qfalse;
1395}
1396
1397
1398/*
1399 * call-seq:
1400 * false | obj -> true or false
1401 * nil | obj -> true or false
1402 *
1403 * Or---Returns <code>false</code> if <i>obj</i> is
1404 * <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
1405 */
1406
1407#define false_or true_and
1408
1409/*
1410 * call-seq:
1411 * false ^ obj -> true or false
1412 * nil ^ obj -> true or false
1413 *
1414 * Exclusive Or---If <i>obj</i> is <code>nil</code> or
1415 * <code>false</code>, returns <code>false</code>; otherwise, returns
1416 * <code>true</code>.
1417 *
1418 */
1419
1420#define false_xor true_and
1421
1422/*
1423 * call-seq:
1424 * nil.nil? -> true
1425 *
1426 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1427 */
1428
1429static VALUE
1430rb_true(VALUE obj)
1431{
1432 return Qtrue;
1433}
1434
1435/*
1436 * call-seq:
1437 * obj.nil? -> true or false
1438 *
1439 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1440 *
1441 * Object.new.nil? #=> false
1442 * nil.nil? #=> true
1443 */
1444
1445
1446MJIT_FUNC_EXPORTED VALUE
1447rb_false(VALUE obj)
1448{
1449 return Qfalse;
1450}
1451
1452/*
1453 * call-seq:
1454 * obj !~ other -> true or false
1455 *
1456 * Returns true if two objects do not match (using the <i>=~</i>
1457 * method), otherwise false.
1458 */
1459
1460static VALUE
1461rb_obj_not_match(VALUE obj1, VALUE obj2)
1462{
1463 VALUE result = rb_funcall(obj1, id_match, 1, obj2);
1464 return rb_obj_not(result);
1465}
1466
1467
1468/*
1469 * call-seq:
1470 * obj <=> other -> 0 or nil
1471 *
1472 * Returns 0 if +obj+ and +other+ are the same object
1473 * or <code>obj == other</code>, otherwise nil.
1474 *
1475 * The #<=> is used by various methods to compare objects, for example
1476 * Enumerable#sort, Enumerable#max etc.
1477 *
1478 * Your implementation of #<=> should return one of the following values: -1, 0,
1479 * 1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
1480 * 1 means self is bigger than other. Nil means the two values could not be
1481 * compared.
1482 *
1483 * When you define #<=>, you can include Comparable to gain the
1484 * methods #<=, #<, #==, #>=, #> and #between?.
1485 */
1486static VALUE
1487rb_obj_cmp(VALUE obj1, VALUE obj2)
1488{
1489 if (rb_equal(obj1, obj2))
1490 return INT2FIX(0);
1491 return Qnil;
1492}
1493
1494/***********************************************************************
1495 *
1496 * Document-class: Module
1497 *
1498 * A Module is a collection of methods and constants. The
1499 * methods in a module may be instance methods or module methods.
1500 * Instance methods appear as methods in a class when the module is
1501 * included, module methods do not. Conversely, module methods may be
1502 * called without creating an encapsulating object, while instance
1503 * methods may not. (See Module#module_function.)
1504 *
1505 * In the descriptions that follow, the parameter <i>sym</i> refers
1506 * to a symbol, which is either a quoted string or a
1507 * Symbol (such as <code>:name</code>).
1508 *
1509 * module Mod
1510 * include Math
1511 * CONST = 1
1512 * def meth
1513 * # ...
1514 * end
1515 * end
1516 * Mod.class #=> Module
1517 * Mod.constants #=> [:CONST, :PI, :E]
1518 * Mod.instance_methods #=> [:meth]
1519 *
1520 */
1521
1522/*
1523 * call-seq:
1524 * mod.to_s -> string
1525 *
1526 * Returns a string representing this module or class. For basic
1527 * classes and modules, this is the name. For singletons, we
1528 * show information on the thing we're attached to as well.
1529 */
1530
1531MJIT_FUNC_EXPORTED VALUE
1532rb_mod_to_s(VALUE klass)
1533{
1534 ID id_defined_at;
1535 VALUE refined_class, defined_at;
1536
1537 if (FL_TEST(klass, FL_SINGLETON)) {
1538 VALUE s = rb_usascii_str_new2("#<Class:");
1539 VALUE v = rb_ivar_get(klass, id__attached__);
1540
1541 if (CLASS_OR_MODULE_P(v)) {
1543 }
1544 else {
1546 }
1547 rb_str_cat2(s, ">");
1548
1549 return s;
1550 }
1551 refined_class = rb_refinement_module_get_refined_class(klass);
1552 if (!NIL_P(refined_class)) {
1553 VALUE s = rb_usascii_str_new2("#<refinement:");
1554
1555 rb_str_concat(s, rb_inspect(refined_class));
1556 rb_str_cat2(s, "@");
1557 CONST_ID(id_defined_at, "__defined_at__");
1558 defined_at = rb_attr_get(klass, id_defined_at);
1559 rb_str_concat(s, rb_inspect(defined_at));
1560 rb_str_cat2(s, ">");
1561 return s;
1562 }
1563 return rb_class_name(klass);
1564}
1565
1566/*
1567 * call-seq:
1568 * mod.freeze -> mod
1569 *
1570 * Prevents further modifications to <i>mod</i>.
1571 *
1572 * This method returns self.
1573 */
1574
1575static VALUE
1576rb_mod_freeze(VALUE mod)
1577{
1578 rb_class_name(mod);
1579 return rb_obj_freeze(mod);
1580}
1581
1582/*
1583 * call-seq:
1584 * mod === obj -> true or false
1585 *
1586 * Case Equality---Returns <code>true</code> if <i>obj</i> is an
1587 * instance of <i>mod</i> or an instance of one of <i>mod</i>'s descendants.
1588 * Of limited use for modules, but can be used in <code>case</code> statements
1589 * to classify objects by class.
1590 */
1591
1592static VALUE
1593rb_mod_eqq(VALUE mod, VALUE arg)
1594{
1595 return rb_obj_is_kind_of(arg, mod);
1596}
1597
1598/*
1599 * call-seq:
1600 * mod <= other -> true, false, or nil
1601 *
1602 * Returns true if <i>mod</i> is a subclass of <i>other</i> or
1603 * is the same as <i>other</i>. Returns
1604 * <code>nil</code> if there's no relationship between the two.
1605 * (Think of the relationship in terms of the class definition:
1606 * "class A < B" implies "A < B".)
1607 */
1608
1609VALUE
1611{
1612 if (mod == arg) return Qtrue;
1613
1614 if (RB_TYPE_P(arg, T_CLASS) && RB_TYPE_P(mod, T_CLASS)) {
1615 // comparison between classes
1616 size_t mod_depth = RCLASS_SUPERCLASS_DEPTH(mod);
1617 size_t arg_depth = RCLASS_SUPERCLASS_DEPTH(arg);
1618 if (arg_depth < mod_depth) {
1619 // check if mod < arg
1620 return RCLASS_SUPERCLASSES(mod)[arg_depth] == arg ?
1621 Qtrue :
1622 Qnil;
1623 }
1624 else if (arg_depth > mod_depth) {
1625 // check if mod > arg
1626 return RCLASS_SUPERCLASSES(arg)[mod_depth] == mod ?
1627 Qfalse :
1628 Qnil;
1629 }
1630 else {
1631 // Depths match, and we know they aren't equal: no relation
1632 return Qnil;
1633 }
1634 }
1635 else {
1636 if (!CLASS_OR_MODULE_P(arg) && !RB_TYPE_P(arg, T_ICLASS)) {
1637 rb_raise(rb_eTypeError, "compared with non class/module");
1638 }
1639 if (class_search_ancestor(mod, RCLASS_ORIGIN(arg))) {
1640 return Qtrue;
1641 }
1642 /* not mod < arg; check if mod > arg */
1643 if (class_search_ancestor(arg, mod)) {
1644 return Qfalse;
1645 }
1646 return Qnil;
1647 }
1648}
1649
1650/*
1651 * call-seq:
1652 * mod < other -> true, false, or nil
1653 *
1654 * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
1655 * <code>false</code> if <i>mod</i> is the same as <i>other</i>
1656 * or <i>mod</i> is an ancestor of <i>other</i>.
1657 * Returns <code>nil</code> if there's no relationship between the two.
1658 * (Think of the relationship in terms of the class definition:
1659 * "class A < B" implies "A < B".)
1660 *
1661 */
1662
1663static VALUE
1664rb_mod_lt(VALUE mod, VALUE arg)
1665{
1666 if (mod == arg) return Qfalse;
1667 return rb_class_inherited_p(mod, arg);
1668}
1669
1670
1671/*
1672 * call-seq:
1673 * mod >= other -> true, false, or nil
1674 *
1675 * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
1676 * two modules are the same. Returns
1677 * <code>nil</code> if there's no relationship between the two.
1678 * (Think of the relationship in terms of the class definition:
1679 * "class A < B" implies "B > A".)
1680 *
1681 */
1682
1683static VALUE
1684rb_mod_ge(VALUE mod, VALUE arg)
1685{
1686 if (!CLASS_OR_MODULE_P(arg)) {
1687 rb_raise(rb_eTypeError, "compared with non class/module");
1688 }
1689
1690 return rb_class_inherited_p(arg, mod);
1691}
1692
1693/*
1694 * call-seq:
1695 * mod > other -> true, false, or nil
1696 *
1697 * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
1698 * <code>false</code> if <i>mod</i> is the same as <i>other</i>
1699 * or <i>mod</i> is a descendant of <i>other</i>.
1700 * Returns <code>nil</code> if there's no relationship between the two.
1701 * (Think of the relationship in terms of the class definition:
1702 * "class A < B" implies "B > A".)
1703 *
1704 */
1705
1706static VALUE
1707rb_mod_gt(VALUE mod, VALUE arg)
1708{
1709 if (mod == arg) return Qfalse;
1710 return rb_mod_ge(mod, arg);
1711}
1712
1713/*
1714 * call-seq:
1715 * module <=> other_module -> -1, 0, +1, or nil
1716 *
1717 * Comparison---Returns -1, 0, +1 or nil depending on whether +module+
1718 * includes +other_module+, they are the same, or if +module+ is included by
1719 * +other_module+.
1720 *
1721 * Returns +nil+ if +module+ has no relationship with +other_module+, if
1722 * +other_module+ is not a module, or if the two values are incomparable.
1723 */
1724
1725static VALUE
1726rb_mod_cmp(VALUE mod, VALUE arg)
1727{
1728 VALUE cmp;
1729
1730 if (mod == arg) return INT2FIX(0);
1731 if (!CLASS_OR_MODULE_P(arg)) {
1732 return Qnil;
1733 }
1734
1735 cmp = rb_class_inherited_p(mod, arg);
1736 if (NIL_P(cmp)) return Qnil;
1737 if (cmp) {
1738 return INT2FIX(-1);
1739 }
1740 return INT2FIX(1);
1741}
1742
1743static VALUE rb_mod_initialize_exec(VALUE module);
1744
1745/*
1746 * call-seq:
1747 * Module.new -> mod
1748 * Module.new {|mod| block } -> mod
1749 *
1750 * Creates a new anonymous module. If a block is given, it is passed
1751 * the module object, and the block is evaluated in the context of this
1752 * module like #module_eval.
1753 *
1754 * fred = Module.new do
1755 * def meth1
1756 * "hello"
1757 * end
1758 * def meth2
1759 * "bye"
1760 * end
1761 * end
1762 * a = "my string"
1763 * a.extend(fred) #=> "my string"
1764 * a.meth1 #=> "hello"
1765 * a.meth2 #=> "bye"
1766 *
1767 * Assign the module to a constant (name starting uppercase) if you
1768 * want to treat it like a regular module.
1769 */
1770
1771static VALUE
1772rb_mod_initialize(VALUE module)
1773{
1774 return rb_mod_initialize_exec(module);
1775}
1776
1777static VALUE
1778rb_mod_initialize_exec(VALUE module)
1779{
1780 if (rb_block_given_p()) {
1781 rb_mod_module_exec(1, &module, module);
1782 }
1783 return Qnil;
1784}
1785
1786/* :nodoc: */
1787static VALUE
1788rb_mod_initialize_clone(int argc, VALUE* argv, VALUE clone)
1789{
1790 VALUE ret, orig, opts;
1791 rb_scan_args(argc, argv, "1:", &orig, &opts);
1792 ret = rb_obj_init_clone(argc, argv, clone);
1793 if (OBJ_FROZEN(orig))
1794 rb_class_name(clone);
1795 return ret;
1796}
1797
1798/*
1799 * call-seq:
1800 * Class.new(super_class=Object) -> a_class
1801 * Class.new(super_class=Object) { |mod| ... } -> a_class
1802 *
1803 * Creates a new anonymous (unnamed) class with the given superclass
1804 * (or Object if no parameter is given). You can give a
1805 * class a name by assigning the class object to a constant.
1806 *
1807 * If a block is given, it is passed the class object, and the block
1808 * is evaluated in the context of this class like
1809 * #class_eval.
1810 *
1811 * fred = Class.new do
1812 * def meth1
1813 * "hello"
1814 * end
1815 * def meth2
1816 * "bye"
1817 * end
1818 * end
1819 *
1820 * a = fred.new #=> #<#<Class:0x100381890>:0x100376b98>
1821 * a.meth1 #=> "hello"
1822 * a.meth2 #=> "bye"
1823 *
1824 * Assign the class to a constant (name starting uppercase) if you
1825 * want to treat it like a regular class.
1826 */
1827
1828static VALUE
1829rb_class_initialize(int argc, VALUE *argv, VALUE klass)
1830{
1831 VALUE super;
1832
1833 if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
1834 rb_raise(rb_eTypeError, "already initialized class");
1835 }
1836 if (rb_check_arity(argc, 0, 1) == 0) {
1837 super = rb_cObject;
1838 }
1839 else {
1840 super = argv[0];
1841 rb_check_inheritable(super);
1842 if (super != rb_cBasicObject && !RCLASS_SUPER(super)) {
1843 rb_raise(rb_eTypeError, "can't inherit uninitialized class");
1844 }
1845 }
1846 RCLASS_SET_SUPER(klass, super);
1847 rb_make_metaclass(klass, RBASIC(super)->klass);
1848 rb_class_inherited(super, klass);
1849 rb_mod_initialize_exec(klass);
1850
1851 return klass;
1852}
1853
1855void
1856rb_undefined_alloc(VALUE klass)
1857{
1858 rb_raise(rb_eTypeError, "allocator undefined for %"PRIsVALUE,
1859 klass);
1860}
1861
1862static rb_alloc_func_t class_get_alloc_func(VALUE klass);
1863static VALUE class_call_alloc_func(rb_alloc_func_t allocator, VALUE klass);
1864
1865/*
1866 * call-seq:
1867 * class.allocate() -> obj
1868 *
1869 * Allocates space for a new object of <i>class</i>'s class and does not
1870 * call initialize on the new instance. The returned object must be an
1871 * instance of <i>class</i>.
1872 *
1873 * klass = Class.new do
1874 * def initialize(*args)
1875 * @initialized = true
1876 * end
1877 *
1878 * def initialized?
1879 * @initialized || false
1880 * end
1881 * end
1882 *
1883 * klass.allocate.initialized? #=> false
1884 *
1885 */
1886
1887static VALUE
1888rb_class_alloc_m(VALUE klass)
1889{
1890 rb_alloc_func_t allocator = class_get_alloc_func(klass);
1891 if (!rb_obj_respond_to(klass, rb_intern("allocate"), 1)) {
1892 rb_raise(rb_eTypeError, "calling %"PRIsVALUE".allocate is prohibited",
1893 klass);
1894 }
1895 return class_call_alloc_func(allocator, klass);
1896}
1897
1898static VALUE
1899rb_class_alloc(VALUE klass)
1900{
1901 rb_alloc_func_t allocator = class_get_alloc_func(klass);
1902 return class_call_alloc_func(allocator, klass);
1903}
1904
1905static rb_alloc_func_t
1906class_get_alloc_func(VALUE klass)
1907{
1908 rb_alloc_func_t allocator;
1909
1910 if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
1911 rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
1912 }
1913 if (FL_TEST(klass, FL_SINGLETON)) {
1914 rb_raise(rb_eTypeError, "can't create instance of singleton class");
1915 }
1916 allocator = rb_get_alloc_func(klass);
1917 if (!allocator) {
1918 rb_undefined_alloc(klass);
1919 }
1920 return allocator;
1921}
1922
1923static VALUE
1924class_call_alloc_func(rb_alloc_func_t allocator, VALUE klass)
1925{
1926 VALUE obj;
1927
1928 RUBY_DTRACE_CREATE_HOOK(OBJECT, rb_class2name(klass));
1929
1930 obj = (*allocator)(klass);
1931
1932 if (rb_obj_class(obj) != rb_class_real(klass)) {
1933 rb_raise(rb_eTypeError, "wrong instance allocation");
1934 }
1935 return obj;
1936}
1937
1938VALUE
1940{
1941 Check_Type(klass, T_CLASS);
1942 return rb_class_alloc(klass);
1943}
1944
1945/*
1946 * call-seq:
1947 * class.new(args, ...) -> obj
1948 *
1949 * Calls #allocate to create a new object of <i>class</i>'s class,
1950 * then invokes that object's #initialize method, passing it
1951 * <i>args</i>. This is the method that ends up getting called
1952 * whenever an object is constructed using <code>.new</code>.
1953 *
1954 */
1955
1956VALUE
1957rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
1958{
1959 VALUE obj;
1960
1961 obj = rb_class_alloc(klass);
1962 rb_obj_call_init_kw(obj, argc, argv, RB_PASS_CALLED_KEYWORDS);
1963
1964 return obj;
1965}
1966
1967VALUE
1968rb_class_new_instance_kw(int argc, const VALUE *argv, VALUE klass, int kw_splat)
1969{
1970 VALUE obj;
1971 Check_Type(klass, T_CLASS);
1972
1973 obj = rb_class_alloc(klass);
1974 rb_obj_call_init_kw(obj, argc, argv, kw_splat);
1975
1976 return obj;
1977}
1978
1979VALUE
1980rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
1981{
1982 return rb_class_new_instance_kw(argc, argv, klass, RB_NO_KEYWORDS);
1983}
1984
1994VALUE
1996{
1997 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS));
1998
1999 VALUE super = RCLASS_SUPER(klass);
2000
2001 if (!super) {
2002 if (klass == rb_cBasicObject) return Qnil;
2003 rb_raise(rb_eTypeError, "uninitialized class");
2004 }
2005
2006 if (!RCLASS_SUPERCLASS_DEPTH(klass)) {
2007 return Qnil;
2008 }
2009 else {
2010 super = RCLASS_SUPERCLASSES(klass)[RCLASS_SUPERCLASS_DEPTH(klass) - 1];
2011 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS));
2012 return super;
2013 }
2014}
2015
2016VALUE
2018{
2019 return RCLASS(klass)->super;
2020}
2021
2022static const char bad_instance_name[] = "`%1$s' is not allowed as an instance variable name";
2023static const char bad_class_name[] = "`%1$s' is not allowed as a class variable name";
2024static const char bad_const_name[] = "wrong constant name %1$s";
2025static const char bad_attr_name[] = "invalid attribute name `%1$s'";
2026#define wrong_constant_name bad_const_name
2027
2029#define id_for_var(obj, name, type) id_for_setter(obj, name, type, bad_##type##_name)
2031#define id_for_setter(obj, name, type, message) \
2032 check_setter_id(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2033static ID
2034check_setter_id(VALUE obj, VALUE *pname,
2035 int (*valid_id_p)(ID), int (*valid_name_p)(VALUE),
2036 const char *message, size_t message_len)
2037{
2038 ID id = rb_check_id(pname);
2039 VALUE name = *pname;
2040
2041 if (id ? !valid_id_p(id) : !valid_name_p(name)) {
2042 rb_name_err_raise_str(rb_fstring_new(message, message_len),
2043 obj, name);
2044 }
2045 return id;
2046}
2047
2048static int
2049rb_is_attr_name(VALUE name)
2050{
2051 return rb_is_local_name(name) || rb_is_const_name(name);
2052}
2053
2054static int
2055rb_is_attr_id(ID id)
2056{
2057 return rb_is_local_id(id) || rb_is_const_id(id);
2058}
2059
2060static ID
2061id_for_attr(VALUE obj, VALUE name)
2062{
2063 ID id = id_for_var(obj, name, attr);
2064 if (!id) id = rb_intern_str(name);
2065 return id;
2066}
2067
2068/*
2069 * call-seq:
2070 * attr_reader(symbol, ...) -> array
2071 * attr(symbol, ...) -> array
2072 * attr_reader(string, ...) -> array
2073 * attr(string, ...) -> array
2074 *
2075 * Creates instance variables and corresponding methods that return the
2076 * value of each instance variable. Equivalent to calling
2077 * ``<code>attr</code><i>:name</i>'' on each name in turn.
2078 * String arguments are converted to symbols.
2079 * Returns an array of defined method names as symbols.
2080 */
2081
2082static VALUE
2083rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
2084{
2085 int i;
2086 VALUE names = rb_ary_new2(argc);
2087
2088 for (i=0; i<argc; i++) {
2089 ID id = id_for_attr(klass, argv[i]);
2090 rb_attr(klass, id, TRUE, FALSE, TRUE);
2091 rb_ary_push(names, ID2SYM(id));
2092 }
2093 return names;
2094}
2095
2100VALUE
2101rb_mod_attr(int argc, VALUE *argv, VALUE klass)
2102{
2103 if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
2104 ID id = id_for_attr(klass, argv[0]);
2105 VALUE names = rb_ary_new();
2106
2107 rb_category_warning(RB_WARN_CATEGORY_DEPRECATED, "optional boolean argument is obsoleted");
2108 rb_attr(klass, id, 1, RTEST(argv[1]), TRUE);
2109 rb_ary_push(names, ID2SYM(id));
2110 if (argv[1] == Qtrue) rb_ary_push(names, ID2SYM(rb_id_attrset(id)));
2111 return names;
2112 }
2113 return rb_mod_attr_reader(argc, argv, klass);
2114}
2115
2116/*
2117 * call-seq:
2118 * attr_writer(symbol, ...) -> array
2119 * attr_writer(string, ...) -> array
2120 *
2121 * Creates an accessor method to allow assignment to the attribute
2122 * <i>symbol</i><code>.id2name</code>.
2123 * String arguments are converted to symbols.
2124 * Returns an array of defined method names as symbols.
2125 */
2126
2127static VALUE
2128rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
2129{
2130 int i;
2131 VALUE names = rb_ary_new2(argc);
2132
2133 for (i=0; i<argc; i++) {
2134 ID id = id_for_attr(klass, argv[i]);
2135 rb_attr(klass, id, FALSE, TRUE, TRUE);
2136 rb_ary_push(names, ID2SYM(rb_id_attrset(id)));
2137 }
2138 return names;
2139}
2140
2141/*
2142 * call-seq:
2143 * attr_accessor(symbol, ...) -> array
2144 * attr_accessor(string, ...) -> array
2145 *
2146 * Defines a named attribute for this module, where the name is
2147 * <i>symbol.</i><code>id2name</code>, creating an instance variable
2148 * (<code>@name</code>) and a corresponding access method to read it.
2149 * Also creates a method called <code>name=</code> to set the attribute.
2150 * String arguments are converted to symbols.
2151 * Returns an array of defined method names as symbols.
2152 *
2153 * module Mod
2154 * attr_accessor(:one, :two) #=> [:one, :one=, :two, :two=]
2155 * end
2156 * Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
2157 */
2158
2159static VALUE
2160rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
2161{
2162 int i;
2163 VALUE names = rb_ary_new2(argc * 2);
2164
2165 for (i=0; i<argc; i++) {
2166 ID id = id_for_attr(klass, argv[i]);
2167
2168 rb_attr(klass, id, TRUE, TRUE, TRUE);
2169 rb_ary_push(names, ID2SYM(id));
2170 rb_ary_push(names, ID2SYM(rb_id_attrset(id)));
2171 }
2172 return names;
2173}
2174
2175/*
2176 * call-seq:
2177 * mod.const_get(sym, inherit=true) -> obj
2178 * mod.const_get(str, inherit=true) -> obj
2179 *
2180 * Checks for a constant with the given name in <i>mod</i>.
2181 * If +inherit+ is set, the lookup will also search
2182 * the ancestors (and +Object+ if <i>mod</i> is a +Module+).
2183 *
2184 * The value of the constant is returned if a definition is found,
2185 * otherwise a +NameError+ is raised.
2186 *
2187 * Math.const_get(:PI) #=> 3.14159265358979
2188 *
2189 * This method will recursively look up constant names if a namespaced
2190 * class name is provided. For example:
2191 *
2192 * module Foo; class Bar; end end
2193 * Object.const_get 'Foo::Bar'
2194 *
2195 * The +inherit+ flag is respected on each lookup. For example:
2196 *
2197 * module Foo
2198 * class Bar
2199 * VAL = 10
2200 * end
2201 *
2202 * class Baz < Bar; end
2203 * end
2204 *
2205 * Object.const_get 'Foo::Baz::VAL' # => 10
2206 * Object.const_get 'Foo::Baz::VAL', false # => NameError
2207 *
2208 * If the argument is not a valid constant name a +NameError+ will be
2209 * raised with a warning "wrong constant name".
2210 *
2211 * Object.const_get 'foobar' #=> NameError: wrong constant name foobar
2212 *
2213 */
2214
2215static VALUE
2216rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
2217{
2218 VALUE name, recur;
2219 rb_encoding *enc;
2220 const char *pbeg, *p, *path, *pend;
2221 ID id;
2222
2223 rb_check_arity(argc, 1, 2);
2224 name = argv[0];
2225 recur = (argc == 1) ? Qtrue : argv[1];
2226
2227 if (SYMBOL_P(name)) {
2228 if (!rb_is_const_sym(name)) goto wrong_name;
2229 id = rb_check_id(&name);
2230 if (!id) return rb_const_missing(mod, name);
2231 return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
2232 }
2233
2234 path = StringValuePtr(name);
2235 enc = rb_enc_get(name);
2236
2237 if (!rb_enc_asciicompat(enc)) {
2238 rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
2239 }
2240
2241 pbeg = p = path;
2242 pend = path + RSTRING_LEN(name);
2243
2244 if (p >= pend || !*p) {
2245 goto wrong_name;
2246 }
2247
2248 if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
2249 mod = rb_cObject;
2250 p += 2;
2251 pbeg = p;
2252 }
2253
2254 while (p < pend) {
2255 VALUE part;
2256 long len, beglen;
2257
2258 while (p < pend && *p != ':') p++;
2259
2260 if (pbeg == p) goto wrong_name;
2261
2262 id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
2263 beglen = pbeg-path;
2264
2265 if (p < pend && p[0] == ':') {
2266 if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2267 p += 2;
2268 pbeg = p;
2269 }
2270
2271 if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2272 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2273 QUOTE(name));
2274 }
2275
2276 if (!id) {
2277 part = rb_str_subseq(name, beglen, len);
2278 OBJ_FREEZE(part);
2279 if (!rb_is_const_name(part)) {
2280 name = part;
2281 goto wrong_name;
2282 }
2283 else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
2284 part = rb_str_intern(part);
2285 mod = rb_const_missing(mod, part);
2286 continue;
2287 }
2288 else {
2289 rb_mod_const_missing(mod, part);
2290 }
2291 }
2292 if (!rb_is_const_id(id)) {
2293 name = ID2SYM(id);
2294 goto wrong_name;
2295 }
2296#if 0
2297 mod = rb_const_get_0(mod, id, beglen > 0 || !RTEST(recur), RTEST(recur), FALSE);
2298#else
2299 if (!RTEST(recur)) {
2300 mod = rb_const_get_at(mod, id);
2301 }
2302 else if (beglen == 0) {
2303 mod = rb_const_get(mod, id);
2304 }
2305 else {
2306 mod = rb_const_get_from(mod, id);
2307 }
2308#endif
2309 }
2310
2311 return mod;
2312
2313 wrong_name:
2314 rb_name_err_raise(wrong_constant_name, mod, name);
2316}
2317
2318/*
2319 * call-seq:
2320 * mod.const_set(sym, obj) -> obj
2321 * mod.const_set(str, obj) -> obj
2322 *
2323 * Sets the named constant to the given object, returning that object.
2324 * Creates a new constant if no constant with the given name previously
2325 * existed.
2326 *
2327 * Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
2328 * Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
2329 *
2330 * If +sym+ or +str+ is not a valid constant name a +NameError+ will be
2331 * raised with a warning "wrong constant name".
2332 *
2333 * Object.const_set('foobar', 42) #=> NameError: wrong constant name foobar
2334 *
2335 */
2336
2337static VALUE
2338rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
2339{
2340 ID id = id_for_var(mod, name, const);
2341 if (!id) id = rb_intern_str(name);
2342 rb_const_set(mod, id, value);
2343
2344 return value;
2345}
2346
2347/*
2348 * call-seq:
2349 * mod.const_defined?(sym, inherit=true) -> true or false
2350 * mod.const_defined?(str, inherit=true) -> true or false
2351 *
2352 * Says whether _mod_ or its ancestors have a constant with the given name:
2353 *
2354 * Float.const_defined?(:EPSILON) #=> true, found in Float itself
2355 * Float.const_defined?("String") #=> true, found in Object (ancestor)
2356 * BasicObject.const_defined?(:Hash) #=> false
2357 *
2358 * If _mod_ is a +Module+, additionally +Object+ and its ancestors are checked:
2359 *
2360 * Math.const_defined?(:String) #=> true, found in Object
2361 *
2362 * In each of the checked classes or modules, if the constant is not present
2363 * but there is an autoload for it, +true+ is returned directly without
2364 * autoloading:
2365 *
2366 * module Admin
2367 * autoload :User, 'admin/user'
2368 * end
2369 * Admin.const_defined?(:User) #=> true
2370 *
2371 * If the constant is not found the callback +const_missing+ is *not* called
2372 * and the method returns +false+.
2373 *
2374 * If +inherit+ is false, the lookup only checks the constants in the receiver:
2375 *
2376 * IO.const_defined?(:SYNC) #=> true, found in File::Constants (ancestor)
2377 * IO.const_defined?(:SYNC, false) #=> false, not found in IO itself
2378 *
2379 * In this case, the same logic for autoloading applies.
2380 *
2381 * If the argument is not a valid constant name a +NameError+ is raised with the
2382 * message "wrong constant name _name_":
2383 *
2384 * Hash.const_defined? 'foobar' #=> NameError: wrong constant name foobar
2385 *
2386 */
2387
2388static VALUE
2389rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
2390{
2391 VALUE name, recur;
2392 rb_encoding *enc;
2393 const char *pbeg, *p, *path, *pend;
2394 ID id;
2395
2396 rb_check_arity(argc, 1, 2);
2397 name = argv[0];
2398 recur = (argc == 1) ? Qtrue : argv[1];
2399
2400 if (SYMBOL_P(name)) {
2401 if (!rb_is_const_sym(name)) goto wrong_name;
2402 id = rb_check_id(&name);
2403 if (!id) return Qfalse;
2404 return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
2405 }
2406
2407 path = StringValuePtr(name);
2408 enc = rb_enc_get(name);
2409
2410 if (!rb_enc_asciicompat(enc)) {
2411 rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
2412 }
2413
2414 pbeg = p = path;
2415 pend = path + RSTRING_LEN(name);
2416
2417 if (p >= pend || !*p) {
2418 goto wrong_name;
2419 }
2420
2421 if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
2422 mod = rb_cObject;
2423 p += 2;
2424 pbeg = p;
2425 }
2426
2427 while (p < pend) {
2428 VALUE part;
2429 long len, beglen;
2430
2431 while (p < pend && *p != ':') p++;
2432
2433 if (pbeg == p) goto wrong_name;
2434
2435 id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
2436 beglen = pbeg-path;
2437
2438 if (p < pend && p[0] == ':') {
2439 if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2440 p += 2;
2441 pbeg = p;
2442 }
2443
2444 if (!id) {
2445 part = rb_str_subseq(name, beglen, len);
2446 OBJ_FREEZE(part);
2447 if (!rb_is_const_name(part)) {
2448 name = part;
2449 goto wrong_name;
2450 }
2451 else {
2452 return Qfalse;
2453 }
2454 }
2455 if (!rb_is_const_id(id)) {
2456 name = ID2SYM(id);
2457 goto wrong_name;
2458 }
2459
2460#if 0
2461 mod = rb_const_search(mod, id, beglen > 0 || !RTEST(recur), RTEST(recur), FALSE);
2462 if (UNDEF_P(mod)) return Qfalse;
2463#else
2464 if (!RTEST(recur)) {
2465 if (!rb_const_defined_at(mod, id))
2466 return Qfalse;
2467 if (p == pend) return Qtrue;
2468 mod = rb_const_get_at(mod, id);
2469 }
2470 else if (beglen == 0) {
2471 if (!rb_const_defined(mod, id))
2472 return Qfalse;
2473 if (p == pend) return Qtrue;
2474 mod = rb_const_get(mod, id);
2475 }
2476 else {
2477 if (!rb_const_defined_from(mod, id))
2478 return Qfalse;
2479 if (p == pend) return Qtrue;
2480 mod = rb_const_get_from(mod, id);
2481 }
2482#endif
2483
2484 if (p < pend && !RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2485 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2486 QUOTE(name));
2487 }
2488 }
2489
2490 return Qtrue;
2491
2492 wrong_name:
2493 rb_name_err_raise(wrong_constant_name, mod, name);
2495}
2496
2497/*
2498 * call-seq:
2499 * mod.const_source_location(sym, inherit=true) -> [String, Integer]
2500 * mod.const_source_location(str, inherit=true) -> [String, Integer]
2501 *
2502 * Returns the Ruby source filename and line number containing the definition
2503 * of the constant specified. If the named constant is not found, +nil+ is returned.
2504 * If the constant is found, but its source location can not be extracted
2505 * (constant is defined in C code), empty array is returned.
2506 *
2507 * _inherit_ specifies whether to lookup in <code>mod.ancestors</code> (+true+
2508 * by default).
2509 *
2510 * # test.rb:
2511 * class A # line 1
2512 * C1 = 1
2513 * C2 = 2
2514 * end
2515 *
2516 * module M # line 6
2517 * C3 = 3
2518 * end
2519 *
2520 * class B < A # line 10
2521 * include M
2522 * C4 = 4
2523 * end
2524 *
2525 * class A # continuation of A definition
2526 * C2 = 8 # constant redefinition; warned yet allowed
2527 * end
2528 *
2529 * p B.const_source_location('C4') # => ["test.rb", 12]
2530 * p B.const_source_location('C3') # => ["test.rb", 7]
2531 * p B.const_source_location('C1') # => ["test.rb", 2]
2532 *
2533 * p B.const_source_location('C3', false) # => nil -- don't lookup in ancestors
2534 *
2535 * p A.const_source_location('C2') # => ["test.rb", 16] -- actual (last) definition place
2536 *
2537 * p Object.const_source_location('B') # => ["test.rb", 10] -- top-level constant could be looked through Object
2538 * p Object.const_source_location('A') # => ["test.rb", 1] -- class reopening is NOT considered new definition
2539 *
2540 * p B.const_source_location('A') # => ["test.rb", 1] -- because Object is in ancestors
2541 * p M.const_source_location('A') # => ["test.rb", 1] -- Object is not ancestor, but additionally checked for modules
2542 *
2543 * p Object.const_source_location('A::C1') # => ["test.rb", 2] -- nesting is supported
2544 * p Object.const_source_location('String') # => [] -- constant is defined in C code
2545 *
2546 *
2547 */
2548static VALUE
2549rb_mod_const_source_location(int argc, VALUE *argv, VALUE mod)
2550{
2551 VALUE name, recur, loc = Qnil;
2552 rb_encoding *enc;
2553 const char *pbeg, *p, *path, *pend;
2554 ID id;
2555
2556 rb_check_arity(argc, 1, 2);
2557 name = argv[0];
2558 recur = (argc == 1) ? Qtrue : argv[1];
2559
2560 if (SYMBOL_P(name)) {
2561 if (!rb_is_const_sym(name)) goto wrong_name;
2562 id = rb_check_id(&name);
2563 if (!id) return Qnil;
2564 return RTEST(recur) ? rb_const_source_location(mod, id) : rb_const_source_location_at(mod, id);
2565 }
2566
2567 path = StringValuePtr(name);
2568 enc = rb_enc_get(name);
2569
2570 if (!rb_enc_asciicompat(enc)) {
2571 rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
2572 }
2573
2574 pbeg = p = path;
2575 pend = path + RSTRING_LEN(name);
2576
2577 if (p >= pend || !*p) {
2578 goto wrong_name;
2579 }
2580
2581 if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
2582 mod = rb_cObject;
2583 p += 2;
2584 pbeg = p;
2585 }
2586
2587 while (p < pend) {
2588 VALUE part;
2589 long len, beglen;
2590
2591 while (p < pend && *p != ':') p++;
2592
2593 if (pbeg == p) goto wrong_name;
2594
2595 id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
2596 beglen = pbeg-path;
2597
2598 if (p < pend && p[0] == ':') {
2599 if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2600 p += 2;
2601 pbeg = p;
2602 }
2603
2604 if (!id) {
2605 part = rb_str_subseq(name, beglen, len);
2606 OBJ_FREEZE(part);
2607 if (!rb_is_const_name(part)) {
2608 name = part;
2609 goto wrong_name;
2610 }
2611 else {
2612 return Qnil;
2613 }
2614 }
2615 if (!rb_is_const_id(id)) {
2616 name = ID2SYM(id);
2617 goto wrong_name;
2618 }
2619 if (p < pend) {
2620 if (RTEST(recur)) {
2621 mod = rb_const_get(mod, id);
2622 }
2623 else {
2624 mod = rb_const_get_at(mod, id);
2625 }
2626 if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2627 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2628 QUOTE(name));
2629 }
2630 }
2631 else {
2632 if (RTEST(recur)) {
2633 loc = rb_const_source_location(mod, id);
2634 }
2635 else {
2636 loc = rb_const_source_location_at(mod, id);
2637 }
2638 break;
2639 }
2640 recur = Qfalse;
2641 }
2642
2643 return loc;
2644
2645 wrong_name:
2646 rb_name_err_raise(wrong_constant_name, mod, name);
2648}
2649
2650/*
2651 * call-seq:
2652 * obj.instance_variable_get(symbol) -> obj
2653 * obj.instance_variable_get(string) -> obj
2654 *
2655 * Returns the value of the given instance variable, or nil if the
2656 * instance variable is not set. The <code>@</code> part of the
2657 * variable name should be included for regular instance
2658 * variables. Throws a NameError exception if the
2659 * supplied symbol is not valid as an instance variable name.
2660 * String arguments are converted to symbols.
2661 *
2662 * class Fred
2663 * def initialize(p1, p2)
2664 * @a, @b = p1, p2
2665 * end
2666 * end
2667 * fred = Fred.new('cat', 99)
2668 * fred.instance_variable_get(:@a) #=> "cat"
2669 * fred.instance_variable_get("@b") #=> 99
2670 */
2671
2672static VALUE
2673rb_obj_ivar_get(VALUE obj, VALUE iv)
2674{
2675 ID id = id_for_var(obj, iv, instance);
2676
2677 if (!id) {
2678 return Qnil;
2679 }
2680 return rb_ivar_get(obj, id);
2681}
2682
2683/*
2684 * call-seq:
2685 * obj.instance_variable_set(symbol, obj) -> obj
2686 * obj.instance_variable_set(string, obj) -> obj
2687 *
2688 * Sets the instance variable named by <i>symbol</i> to the given
2689 * object. This may circumvent the encapsulation intended by
2690 * the author of the class, so it should be used with care.
2691 * The variable does not have to exist prior to this call.
2692 * If the instance variable name is passed as a string, that string
2693 * is converted to a symbol.
2694 *
2695 * class Fred
2696 * def initialize(p1, p2)
2697 * @a, @b = p1, p2
2698 * end
2699 * end
2700 * fred = Fred.new('cat', 99)
2701 * fred.instance_variable_set(:@a, 'dog') #=> "dog"
2702 * fred.instance_variable_set(:@c, 'cat') #=> "cat"
2703 * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
2704 */
2705
2706static VALUE
2707rb_obj_ivar_set_m(VALUE obj, VALUE iv, VALUE val)
2708{
2709 ID id = id_for_var(obj, iv, instance);
2710 if (!id) id = rb_intern_str(iv);
2711 return rb_ivar_set(obj, id, val);
2712}
2713
2714/*
2715 * call-seq:
2716 * obj.instance_variable_defined?(symbol) -> true or false
2717 * obj.instance_variable_defined?(string) -> true or false
2718 *
2719 * Returns <code>true</code> if the given instance variable is
2720 * defined in <i>obj</i>.
2721 * String arguments are converted to symbols.
2722 *
2723 * class Fred
2724 * def initialize(p1, p2)
2725 * @a, @b = p1, p2
2726 * end
2727 * end
2728 * fred = Fred.new('cat', 99)
2729 * fred.instance_variable_defined?(:@a) #=> true
2730 * fred.instance_variable_defined?("@b") #=> true
2731 * fred.instance_variable_defined?("@c") #=> false
2732 */
2733
2734static VALUE
2735rb_obj_ivar_defined(VALUE obj, VALUE iv)
2736{
2737 ID id = id_for_var(obj, iv, instance);
2738
2739 if (!id) {
2740 return Qfalse;
2741 }
2742 return rb_ivar_defined(obj, id);
2743}
2744
2745/*
2746 * call-seq:
2747 * mod.class_variable_get(symbol) -> obj
2748 * mod.class_variable_get(string) -> obj
2749 *
2750 * Returns the value of the given class variable (or throws a
2751 * NameError exception). The <code>@@</code> part of the
2752 * variable name should be included for regular class variables.
2753 * String arguments are converted to symbols.
2754 *
2755 * class Fred
2756 * @@foo = 99
2757 * end
2758 * Fred.class_variable_get(:@@foo) #=> 99
2759 */
2760
2761static VALUE
2762rb_mod_cvar_get(VALUE obj, VALUE iv)
2763{
2764 ID id = id_for_var(obj, iv, class);
2765
2766 if (!id) {
2767 rb_name_err_raise("uninitialized class variable %1$s in %2$s",
2768 obj, iv);
2769 }
2770 return rb_cvar_get(obj, id);
2771}
2772
2773/*
2774 * call-seq:
2775 * obj.class_variable_set(symbol, obj) -> obj
2776 * obj.class_variable_set(string, obj) -> obj
2777 *
2778 * Sets the class variable named by <i>symbol</i> to the given
2779 * object.
2780 * If the class variable name is passed as a string, that string
2781 * is converted to a symbol.
2782 *
2783 * class Fred
2784 * @@foo = 99
2785 * def foo
2786 * @@foo
2787 * end
2788 * end
2789 * Fred.class_variable_set(:@@foo, 101) #=> 101
2790 * Fred.new.foo #=> 101
2791 */
2792
2793static VALUE
2794rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
2795{
2796 ID id = id_for_var(obj, iv, class);
2797 if (!id) id = rb_intern_str(iv);
2798 rb_cvar_set(obj, id, val);
2799 return val;
2800}
2801
2802/*
2803 * call-seq:
2804 * obj.class_variable_defined?(symbol) -> true or false
2805 * obj.class_variable_defined?(string) -> true or false
2806 *
2807 * Returns <code>true</code> if the given class variable is defined
2808 * in <i>obj</i>.
2809 * String arguments are converted to symbols.
2810 *
2811 * class Fred
2812 * @@foo = 99
2813 * end
2814 * Fred.class_variable_defined?(:@@foo) #=> true
2815 * Fred.class_variable_defined?(:@@bar) #=> false
2816 */
2817
2818static VALUE
2819rb_mod_cvar_defined(VALUE obj, VALUE iv)
2820{
2821 ID id = id_for_var(obj, iv, class);
2822
2823 if (!id) {
2824 return Qfalse;
2825 }
2826 return rb_cvar_defined(obj, id);
2827}
2828
2829/*
2830 * call-seq:
2831 * mod.singleton_class? -> true or false
2832 *
2833 * Returns <code>true</code> if <i>mod</i> is a singleton class or
2834 * <code>false</code> if it is an ordinary class or module.
2835 *
2836 * class C
2837 * end
2838 * C.singleton_class? #=> false
2839 * C.singleton_class.singleton_class? #=> true
2840 */
2841
2842static VALUE
2843rb_mod_singleton_p(VALUE klass)
2844{
2845 return RBOOL(RB_TYPE_P(klass, T_CLASS) && FL_TEST(klass, FL_SINGLETON));
2846}
2847
2849static const struct conv_method_tbl {
2850 const char method[6];
2851 unsigned short id;
2852} conv_method_names[] = {
2853#define M(n) {#n, (unsigned short)idTo_##n}
2854 M(int),
2855 M(ary),
2856 M(str),
2857 M(sym),
2858 M(hash),
2859 M(proc),
2860 M(io),
2861 M(a),
2862 M(s),
2863 M(i),
2864 M(f),
2865 M(r),
2866#undef M
2867};
2868#define IMPLICIT_CONVERSIONS 7
2869
2870static int
2871conv_method_index(const char *method)
2872{
2873 static const char prefix[] = "to_";
2874
2875 if (strncmp(prefix, method, sizeof(prefix)-1) == 0) {
2876 const char *const meth = &method[sizeof(prefix)-1];
2877 int i;
2878 for (i=0; i < numberof(conv_method_names); i++) {
2879 if (conv_method_names[i].method[0] == meth[0] &&
2880 strcmp(conv_method_names[i].method, meth) == 0) {
2881 return i;
2882 }
2883 }
2884 }
2885 return numberof(conv_method_names);
2886}
2887
2888static VALUE
2889convert_type_with_id(VALUE val, const char *tname, ID method, int raise, int index)
2890{
2891 VALUE r = rb_check_funcall(val, method, 0, 0);
2892 if (UNDEF_P(r)) {
2893 if (raise) {
2894 const char *msg =
2895 ((index < 0 ? conv_method_index(rb_id2name(method)) : index)
2896 < IMPLICIT_CONVERSIONS) ?
2897 "no implicit conversion of" : "can't convert";
2898 const char *cname = NIL_P(val) ? "nil" :
2899 val == Qtrue ? "true" :
2900 val == Qfalse ? "false" :
2901 NULL;
2902 if (cname)
2903 rb_raise(rb_eTypeError, "%s %s into %s", msg, cname, tname);
2904 rb_raise(rb_eTypeError, "%s %"PRIsVALUE" into %s", msg,
2905 rb_obj_class(val),
2906 tname);
2907 }
2908 return Qnil;
2909 }
2910 return r;
2911}
2912
2913static VALUE
2914convert_type(VALUE val, const char *tname, const char *method, int raise)
2915{
2916 int i = conv_method_index(method);
2917 ID m = i < numberof(conv_method_names) ?
2918 conv_method_names[i].id : rb_intern(method);
2919 return convert_type_with_id(val, tname, m, raise, i);
2920}
2921
2923NORETURN(static void conversion_mismatch(VALUE, const char *, const char *, VALUE));
2924static void
2925conversion_mismatch(VALUE val, const char *tname, const char *method, VALUE result)
2926{
2927 VALUE cname = rb_obj_class(val);
2929 "can't convert %"PRIsVALUE" to %s (%"PRIsVALUE"#%s gives %"PRIsVALUE")",
2930 cname, tname, cname, method, rb_obj_class(result));
2931}
2932
2933VALUE
2934rb_convert_type(VALUE val, int type, const char *tname, const char *method)
2935{
2936 VALUE v;
2937
2938 if (TYPE(val) == type) return val;
2939 v = convert_type(val, tname, method, TRUE);
2940 if (TYPE(v) != type) {
2941 conversion_mismatch(val, tname, method, v);
2942 }
2943 return v;
2944}
2945
2947VALUE
2948rb_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
2949{
2950 VALUE v;
2951
2952 if (TYPE(val) == type) return val;
2953 v = convert_type_with_id(val, tname, method, TRUE, -1);
2954 if (TYPE(v) != type) {
2955 conversion_mismatch(val, tname, RSTRING_PTR(rb_id2str(method)), v);
2956 }
2957 return v;
2958}
2959
2960VALUE
2961rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
2962{
2963 VALUE v;
2964
2965 /* always convert T_DATA */
2966 if (TYPE(val) == type && type != T_DATA) return val;
2967 v = convert_type(val, tname, method, FALSE);
2968 if (NIL_P(v)) return Qnil;
2969 if (TYPE(v) != type) {
2970 conversion_mismatch(val, tname, method, v);
2971 }
2972 return v;
2973}
2974
2976MJIT_FUNC_EXPORTED VALUE
2977rb_check_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
2978{
2979 VALUE v;
2980
2981 /* always convert T_DATA */
2982 if (TYPE(val) == type && type != T_DATA) return val;
2983 v = convert_type_with_id(val, tname, method, FALSE, -1);
2984 if (NIL_P(v)) return Qnil;
2985 if (TYPE(v) != type) {
2986 conversion_mismatch(val, tname, RSTRING_PTR(rb_id2str(method)), v);
2987 }
2988 return v;
2989}
2990
2991#define try_to_int(val, mid, raise) \
2992 convert_type_with_id(val, "Integer", mid, raise, -1)
2993
2994ALWAYS_INLINE(static VALUE rb_to_integer_with_id_exception(VALUE val, const char *method, ID mid, int raise));
2995/* Integer specific rb_check_convert_type_with_id */
2996static inline VALUE
2997rb_to_integer_with_id_exception(VALUE val, const char *method, ID mid, int raise)
2998{
2999 VALUE v;
3000
3001 if (RB_INTEGER_TYPE_P(val)) return val;
3002 v = try_to_int(val, mid, raise);
3003 if (!raise && NIL_P(v)) return Qnil;
3004 if (!RB_INTEGER_TYPE_P(v)) {
3005 conversion_mismatch(val, "Integer", method, v);
3006 }
3007 return v;
3008}
3009#define rb_to_integer(val, method, mid) \
3010 rb_to_integer_with_id_exception(val, method, mid, TRUE)
3011
3012VALUE
3013rb_check_to_integer(VALUE val, const char *method)
3014{
3015 VALUE v;
3016
3017 if (RB_INTEGER_TYPE_P(val)) return val;
3018 v = convert_type(val, "Integer", method, FALSE);
3019 if (!RB_INTEGER_TYPE_P(v)) {
3020 return Qnil;
3021 }
3022 return v;
3023}
3024
3025VALUE
3027{
3028 return rb_to_integer(val, "to_int", idTo_int);
3029}
3030
3031VALUE
3033{
3034 if (RB_INTEGER_TYPE_P(val)) return val;
3035 val = try_to_int(val, idTo_int, FALSE);
3036 if (RB_INTEGER_TYPE_P(val)) return val;
3037 return Qnil;
3038}
3039
3040static VALUE
3041rb_check_to_i(VALUE val)
3042{
3043 if (RB_INTEGER_TYPE_P(val)) return val;
3044 val = try_to_int(val, idTo_i, FALSE);
3045 if (RB_INTEGER_TYPE_P(val)) return val;
3046 return Qnil;
3047}
3048
3049static VALUE
3050rb_convert_to_integer(VALUE val, int base, int raise_exception)
3051{
3052 VALUE tmp;
3053
3054 if (base) {
3055 tmp = rb_check_string_type(val);
3056
3057 if (! NIL_P(tmp)) {
3058 val = tmp;
3059 }
3060 else if (! raise_exception) {
3061 return Qnil;
3062 }
3063 else {
3064 rb_raise(rb_eArgError, "base specified for non string value");
3065 }
3066 }
3067 if (RB_FLOAT_TYPE_P(val)) {
3068 double f = RFLOAT_VALUE(val);
3069 if (!raise_exception && !isfinite(f)) return Qnil;
3070 if (FIXABLE(f)) return LONG2FIX((long)f);
3071 return rb_dbl2big(f);
3072 }
3073 else if (RB_INTEGER_TYPE_P(val)) {
3074 return val;
3075 }
3076 else if (RB_TYPE_P(val, T_STRING)) {
3077 return rb_str_convert_to_inum(val, base, TRUE, raise_exception);
3078 }
3079 else if (NIL_P(val)) {
3080 if (!raise_exception) return Qnil;
3081 rb_raise(rb_eTypeError, "can't convert nil into Integer");
3082 }
3083
3084 tmp = rb_protect(rb_check_to_int, val, NULL);
3085 if (RB_INTEGER_TYPE_P(tmp)) return tmp;
3087 if (!NIL_P(tmp = rb_check_string_type(val))) {
3088 return rb_str_convert_to_inum(tmp, base, TRUE, raise_exception);
3089 }
3090
3091 if (!raise_exception) {
3092 VALUE result = rb_protect(rb_check_to_i, val, NULL);
3094 return result;
3095 }
3096
3097 return rb_to_integer(val, "to_i", idTo_i);
3098}
3099
3100VALUE
3102{
3103 return rb_convert_to_integer(val, 0, TRUE);
3104}
3105
3106VALUE
3107rb_check_integer_type(VALUE val)
3108{
3109 return rb_to_integer_with_id_exception(val, "to_int", idTo_int, FALSE);
3110}
3111
3112int
3113rb_bool_expected(VALUE obj, const char *flagname, int raise)
3114{
3115 switch (obj) {
3116 case Qtrue:
3117 return TRUE;
3118 case Qfalse:
3119 return FALSE;
3120 default: {
3121 static const char message[] = "expected true or false as %s: %+"PRIsVALUE;
3122 if (raise) {
3123 rb_raise(rb_eArgError, message, flagname, obj);
3124 }
3125 rb_warning(message, flagname, obj);
3126 return !NIL_P(obj);
3127 }
3128 }
3129}
3130
3131int
3132rb_opts_exception_p(VALUE opts, int default_value)
3133{
3134 static const ID kwds[1] = {idException};
3135 VALUE exception;
3136 if (rb_get_kwargs(opts, kwds, 0, 1, &exception))
3137 return rb_bool_expected(exception, "exception", TRUE);
3138 return default_value;
3139}
3140
3141#define opts_exception_p(opts) rb_opts_exception_p((opts), TRUE)
3142
3143/*
3144 * call-seq:
3145 * Integer(object, base = 0, exception: true) -> integer or nil
3146 *
3147 * Returns an integer converted from +object+.
3148 *
3149 * Tries to convert +object+ to an integer
3150 * using +to_int+ first and +to_i+ second;
3151 * see below for exceptions.
3152 *
3153 * With a non-zero +base+, +object+ must be a string or convertible
3154 * to a string.
3155 *
3156 * ==== numeric objects
3157 *
3158 * With integer argument +object+ given, returns +object+:
3159 *
3160 * Integer(1) # => 1
3161 * Integer(-1) # => -1
3162 *
3163 * With floating-point argument +object+ given,
3164 * returns +object+ truncated to an intger:
3165 *
3166 * Integer(1.9) # => 1 # Rounds toward zero.
3167 * Integer(-1.9) # => -1 # Rounds toward zero.
3168 *
3169 * ==== string objects
3170 *
3171 * With string argument +object+ and zero +base+ given,
3172 * returns +object+ converted to an integer in base 10:
3173 *
3174 * Integer('100') # => 100
3175 * Integer('-100') # => -100
3176 *
3177 * With +base+ zero, string +object+ may contain leading characters
3178 * to specify the actual base (radix indicator):
3179 *
3180 * Integer('0100') # => 64 # Leading '0' specifies base 8.
3181 * Integer('0b100') # => 4 # Leading '0b', specifies base 2.
3182 * Integer('0x100') # => 256 # Leading '0x' specifies base 16.
3183 *
3184 * With a positive +base+ (in range 2..36) given, returns +object+
3185 * converted to an integer in the given base:
3186 *
3187 * Integer('100', 2) # => 4
3188 * Integer('100', 8) # => 64
3189 * Integer('-100', 16) # => -256
3190 *
3191 * With a negative +base+ (in range -36..-2) given, returns +object+
3192 * converted to an integer in the radix indicator if exists or
3193 * +-base+:
3194 *
3195 * Integer('0x100', -2) # => 256
3196 * Integer('100', -2) # => 4
3197 * Integer('0b100', -8) # => 4
3198 * Integer('100', -8) # => 64
3199 * Integer('0o100', -10) # => 64
3200 * Integer('100', -10) # => 100
3201 *
3202 * +base+ -1 is equal the -10 case.
3203 *
3204 * When converting strings, surrounding whitespace and embedded underscores
3205 * are allowed and ignored:
3206 *
3207 * Integer(' 100 ') # => 100
3208 * Integer('-1_0_0', 16) # => -256
3209 *
3210 * ==== other classes
3211 *
3212 * Examples with +object+ of various other classes:
3213 *
3214 * Integer(Rational(9, 10)) # => 0 # Rounds toward zero.
3215 * Integer(Complex(2, 0)) # => 2 # Imaginary part must be zero.
3216 * Integer(Time.now) # => 1650974042
3217 *
3218 * ==== keywords
3219 *
3220 * With optional keyword argument +exception+ given as +true+ (the default):
3221 *
3222 * - Raises TypeError if +object+ does not respond to +to_int+ or +to_i+.
3223 * - Raises TypeError if +object+ is +nil+.
3224 * - Raise ArgumentError if +object+ is an invalid string.
3225 *
3226 * With +exception+ given as +false+, an exception of any kind is suppressed
3227 * and +nil+ is returned.
3228 *
3229 */
3230
3231static VALUE
3232rb_f_integer(int argc, VALUE *argv, VALUE obj)
3233{
3234 VALUE arg = Qnil, opts = Qnil;
3235 int base = 0;
3236
3237 if (argc > 1) {
3238 int narg = 1;
3239 VALUE vbase = rb_check_to_int(argv[1]);
3240 if (!NIL_P(vbase)) {
3241 base = NUM2INT(vbase);
3242 narg = 2;
3243 }
3244 if (argc > narg) {
3245 VALUE hash = rb_check_hash_type(argv[argc-1]);
3246 if (!NIL_P(hash)) {
3247 opts = rb_extract_keywords(&hash);
3248 if (!hash) --argc;
3249 }
3250 }
3251 }
3252 rb_check_arity(argc, 1, 2);
3253 arg = argv[0];
3254
3255 return rb_convert_to_integer(arg, base, opts_exception_p(opts));
3256}
3257
3258static double
3259rb_cstr_to_dbl_raise(const char *p, int badcheck, int raise, int *error)
3260{
3261 const char *q;
3262 char *end;
3263 double d;
3264 const char *ellipsis = "";
3265 int w;
3266 enum {max_width = 20};
3267#define OutOfRange() ((end - p > max_width) ? \
3268 (w = max_width, ellipsis = "...") : \
3269 (w = (int)(end - p), ellipsis = ""))
3270
3271 if (!p) return 0.0;
3272 q = p;
3273 while (ISSPACE(*p)) p++;
3274
3275 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
3276 return 0.0;
3277 }
3278
3279 d = strtod(p, &end);
3280 if (errno == ERANGE) {
3281 OutOfRange();
3282 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
3283 errno = 0;
3284 }
3285 if (p == end) {
3286 if (badcheck) {
3287 goto bad;
3288 }
3289 return d;
3290 }
3291 if (*end) {
3292 char buf[DBL_DIG * 4 + 10];
3293 char *n = buf;
3294 char *const init_e = buf + DBL_DIG * 4;
3295 char *e = init_e;
3296 char prev = 0;
3297 int dot_seen = FALSE;
3298
3299 switch (*p) {case '+': case '-': prev = *n++ = *p++;}
3300 if (*p == '0') {
3301 prev = *n++ = '0';
3302 while (*++p == '0');
3303 }
3304 while (p < end && n < e) prev = *n++ = *p++;
3305 while (*p) {
3306 if (*p == '_') {
3307 /* remove an underscore between digits */
3308 if (n == buf || !ISDIGIT(prev) || (++p, !ISDIGIT(*p))) {
3309 if (badcheck) goto bad;
3310 break;
3311 }
3312 }
3313 prev = *p++;
3314 if (e == init_e && (prev == 'e' || prev == 'E' || prev == 'p' || prev == 'P')) {
3315 e = buf + sizeof(buf) - 1;
3316 *n++ = prev;
3317 switch (*p) {case '+': case '-': prev = *n++ = *p++;}
3318 if (*p == '0') {
3319 prev = *n++ = '0';
3320 while (*++p == '0');
3321 }
3322 continue;
3323 }
3324 else if (ISSPACE(prev)) {
3325 while (ISSPACE(*p)) ++p;
3326 if (*p) {
3327 if (badcheck) goto bad;
3328 break;
3329 }
3330 }
3331 else if (prev == '.' ? dot_seen++ : !ISDIGIT(prev)) {
3332 if (badcheck) goto bad;
3333 break;
3334 }
3335 if (n < e) *n++ = prev;
3336 }
3337 *n = '\0';
3338 p = buf;
3339
3340 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
3341 return 0.0;
3342 }
3343
3344 d = strtod(p, &end);
3345 if (errno == ERANGE) {
3346 OutOfRange();
3347 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
3348 errno = 0;
3349 }
3350 if (badcheck) {
3351 if (!end || p == end) goto bad;
3352 while (*end && ISSPACE(*end)) end++;
3353 if (*end) goto bad;
3354 }
3355 }
3356 if (errno == ERANGE) {
3357 errno = 0;
3358 OutOfRange();
3359 rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
3360 }
3361 return d;
3362
3363 bad:
3364 if (raise) {
3365 rb_invalid_str(q, "Float()");
3366 UNREACHABLE_RETURN(nan(""));
3367 }
3368 else {
3369 if (error) *error = 1;
3370 return 0.0;
3371 }
3372}
3373
3374double
3375rb_cstr_to_dbl(const char *p, int badcheck)
3376{
3377 return rb_cstr_to_dbl_raise(p, badcheck, TRUE, NULL);
3378}
3379
3380static double
3381rb_str_to_dbl_raise(VALUE str, int badcheck, int raise, int *error)
3382{
3383 char *s;
3384 long len;
3385 double ret;
3386 VALUE v = 0;
3387
3388 StringValue(str);
3389 s = RSTRING_PTR(str);
3390 len = RSTRING_LEN(str);
3391 if (s) {
3392 if (badcheck && memchr(s, '\0', len)) {
3393 if (raise)
3394 rb_raise(rb_eArgError, "string for Float contains null byte");
3395 else {
3396 if (error) *error = 1;
3397 return 0.0;
3398 }
3399 }
3400 if (s[len]) { /* no sentinel somehow */
3401 char *p = ALLOCV(v, (size_t)len + 1);
3402 MEMCPY(p, s, char, len);
3403 p[len] = '\0';
3404 s = p;
3405 }
3406 }
3407 ret = rb_cstr_to_dbl_raise(s, badcheck, raise, error);
3408 if (v)
3409 ALLOCV_END(v);
3410 return ret;
3411}
3412
3413FUNC_MINIMIZED(double rb_str_to_dbl(VALUE str, int badcheck));
3414
3415double
3416rb_str_to_dbl(VALUE str, int badcheck)
3417{
3418 return rb_str_to_dbl_raise(str, badcheck, TRUE, NULL);
3419}
3420
3422#define fix2dbl_without_to_f(x) (double)FIX2LONG(x)
3423#define big2dbl_without_to_f(x) rb_big2dbl(x)
3424#define int2dbl_without_to_f(x) \
3425 (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : big2dbl_without_to_f(x))
3426#define num2dbl_without_to_f(x) \
3427 (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : \
3428 RB_BIGNUM_TYPE_P(x) ? big2dbl_without_to_f(x) : \
3429 (Check_Type(x, T_FLOAT), RFLOAT_VALUE(x)))
3430static inline double
3431rat2dbl_without_to_f(VALUE x)
3432{
3433 VALUE num = rb_rational_num(x);
3434 VALUE den = rb_rational_den(x);
3435 return num2dbl_without_to_f(num) / num2dbl_without_to_f(den);
3436}
3437
3438#define special_const_to_float(val, pre, post) \
3439 switch (val) { \
3440 case Qnil: \
3441 rb_raise_static(rb_eTypeError, pre "nil" post); \
3442 case Qtrue: \
3443 rb_raise_static(rb_eTypeError, pre "true" post); \
3444 case Qfalse: \
3445 rb_raise_static(rb_eTypeError, pre "false" post); \
3446 }
3449static inline void
3450conversion_to_float(VALUE val)
3451{
3452 special_const_to_float(val, "can't convert ", " into Float");
3453}
3454
3455static inline void
3456implicit_conversion_to_float(VALUE val)
3457{
3458 special_const_to_float(val, "no implicit conversion to float from ", "");
3459}
3460
3461static int
3462to_float(VALUE *valp, int raise_exception)
3463{
3464 VALUE val = *valp;
3465 if (SPECIAL_CONST_P(val)) {
3466 if (FIXNUM_P(val)) {
3467 *valp = DBL2NUM(fix2dbl_without_to_f(val));
3468 return T_FLOAT;
3469 }
3470 else if (FLONUM_P(val)) {
3471 return T_FLOAT;
3472 }
3473 else if (raise_exception) {
3474 conversion_to_float(val);
3475 }
3476 }
3477 else {
3478 int type = BUILTIN_TYPE(val);
3479 switch (type) {
3480 case T_FLOAT:
3481 return T_FLOAT;
3482 case T_BIGNUM:
3483 *valp = DBL2NUM(big2dbl_without_to_f(val));
3484 return T_FLOAT;
3485 case T_RATIONAL:
3486 *valp = DBL2NUM(rat2dbl_without_to_f(val));
3487 return T_FLOAT;
3488 case T_STRING:
3489 return T_STRING;
3490 }
3491 }
3492 return T_NONE;
3493}
3494
3495static VALUE
3496convert_type_to_float_protected(VALUE val)
3497{
3498 return rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3499}
3500
3501static VALUE
3502rb_convert_to_float(VALUE val, int raise_exception)
3503{
3504 switch (to_float(&val, raise_exception)) {
3505 case T_FLOAT:
3506 return val;
3507 case T_STRING:
3508 if (!raise_exception) {
3509 int e = 0;
3510 double x = rb_str_to_dbl_raise(val, TRUE, raise_exception, &e);
3511 return e ? Qnil : DBL2NUM(x);
3512 }
3513 return DBL2NUM(rb_str_to_dbl(val, TRUE));
3514 case T_NONE:
3515 if (SPECIAL_CONST_P(val) && !raise_exception)
3516 return Qnil;
3517 }
3518
3519 if (!raise_exception) {
3520 int state;
3521 VALUE result = rb_protect(convert_type_to_float_protected, val, &state);
3522 if (state) rb_set_errinfo(Qnil);
3523 return result;
3524 }
3525
3526 return rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3527}
3528
3529FUNC_MINIMIZED(VALUE rb_Float(VALUE val));
3530
3531VALUE
3533{
3534 return rb_convert_to_float(val, TRUE);
3535}
3536
3537static VALUE
3538rb_f_float1(rb_execution_context_t *ec, VALUE obj, VALUE arg)
3539{
3540 return rb_convert_to_float(arg, TRUE);
3541}
3542
3543static VALUE
3544rb_f_float(rb_execution_context_t *ec, VALUE obj, VALUE arg, VALUE opts)
3545{
3546 int exception = rb_bool_expected(opts, "exception", TRUE);
3547 return rb_convert_to_float(arg, exception);
3548}
3549
3550static VALUE
3551numeric_to_float(VALUE val)
3552{
3553 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
3554 rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into Float",
3555 rb_obj_class(val));
3556 }
3557 return rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3558}
3559
3560VALUE
3562{
3563 switch (to_float(&val, TRUE)) {
3564 case T_FLOAT:
3565 return val;
3566 }
3567 return numeric_to_float(val);
3568}
3569
3570VALUE
3572{
3573 if (RB_FLOAT_TYPE_P(val)) return val;
3574 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
3575 return Qnil;
3576 }
3577 return rb_check_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3578}
3579
3580static inline int
3581basic_to_f_p(VALUE klass)
3582{
3583 return rb_method_basic_definition_p(klass, id_to_f);
3584}
3585
3587double
3588rb_num_to_dbl(VALUE val)
3589{
3590 if (SPECIAL_CONST_P(val)) {
3591 if (FIXNUM_P(val)) {
3592 if (basic_to_f_p(rb_cInteger))
3593 return fix2dbl_without_to_f(val);
3594 }
3595 else if (FLONUM_P(val)) {
3596 return rb_float_flonum_value(val);
3597 }
3598 else {
3599 conversion_to_float(val);
3600 }
3601 }
3602 else {
3603 switch (BUILTIN_TYPE(val)) {
3604 case T_FLOAT:
3605 return rb_float_noflonum_value(val);
3606 case T_BIGNUM:
3607 if (basic_to_f_p(rb_cInteger))
3608 return big2dbl_without_to_f(val);
3609 break;
3610 case T_RATIONAL:
3611 if (basic_to_f_p(rb_cRational))
3612 return rat2dbl_without_to_f(val);
3613 break;
3614 default:
3615 break;
3616 }
3617 }
3618 val = numeric_to_float(val);
3619 return RFLOAT_VALUE(val);
3620}
3621
3622double
3624{
3625 if (SPECIAL_CONST_P(val)) {
3626 if (FIXNUM_P(val)) {
3627 return fix2dbl_without_to_f(val);
3628 }
3629 else if (FLONUM_P(val)) {
3630 return rb_float_flonum_value(val);
3631 }
3632 else {
3633 implicit_conversion_to_float(val);
3634 }
3635 }
3636 else {
3637 switch (BUILTIN_TYPE(val)) {
3638 case T_FLOAT:
3639 return rb_float_noflonum_value(val);
3640 case T_BIGNUM:
3641 return big2dbl_without_to_f(val);
3642 case T_RATIONAL:
3643 return rat2dbl_without_to_f(val);
3644 case T_STRING:
3645 rb_raise(rb_eTypeError, "no implicit conversion to float from string");
3646 default:
3647 break;
3648 }
3649 }
3650 val = rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3651 return RFLOAT_VALUE(val);
3652}
3653
3654VALUE
3656{
3657 VALUE tmp = rb_check_string_type(val);
3658 if (NIL_P(tmp))
3659 tmp = rb_convert_type_with_id(val, T_STRING, "String", idTo_s);
3660 return tmp;
3661}
3662
3663
3664/*
3665 * call-seq:
3666 * String(object) -> object or new_string
3667 *
3668 * Returns a string converted from +object+.
3669 *
3670 * Tries to convert +object+ to a string
3671 * using +to_str+ first and +to_s+ second:
3672 *
3673 * String([0, 1, 2]) # => "[0, 1, 2]"
3674 * String(0..5) # => "0..5"
3675 * String({foo: 0, bar: 1}) # => "{:foo=>0, :bar=>1}"
3676 *
3677 * Raises +TypeError+ if +object+ cannot be converted to a string.
3678 */
3679
3680static VALUE
3681rb_f_string(VALUE obj, VALUE arg)
3682{
3683 return rb_String(arg);
3684}
3685
3686VALUE
3688{
3689 VALUE tmp = rb_check_array_type(val);
3690
3691 if (NIL_P(tmp)) {
3692 tmp = rb_check_to_array(val);
3693 if (NIL_P(tmp)) {
3694 return rb_ary_new3(1, val);
3695 }
3696 }
3697 return tmp;
3698}
3699
3700/*
3701 * call-seq:
3702 * Array(object) -> object or new_array
3703 *
3704 * Returns an array converted from +object+.
3705 *
3706 * Tries to convert +object+ to an array
3707 * using +to_ary+ first and +to_a+ second:
3708 *
3709 * Array([0, 1, 2]) # => [0, 1, 2]
3710 * Array({foo: 0, bar: 1}) # => [[:foo, 0], [:bar, 1]]
3711 * Array(0..4) # => [0, 1, 2, 3, 4]
3712 *
3713 * Returns +object+ in an array, <tt>[object]</tt>,
3714 * if +object+ cannot be converted:
3715 *
3716 * Array(:foo) # => [:foo]
3717 *
3718 */
3719
3720static VALUE
3721rb_f_array(VALUE obj, VALUE arg)
3722{
3723 return rb_Array(arg);
3724}
3725
3729VALUE
3731{
3732 VALUE tmp;
3733
3734 if (NIL_P(val)) return rb_hash_new();
3735 tmp = rb_check_hash_type(val);
3736 if (NIL_P(tmp)) {
3737 if (RB_TYPE_P(val, T_ARRAY) && RARRAY_LEN(val) == 0)
3738 return rb_hash_new();
3739 rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
3740 }
3741 return tmp;
3742}
3743
3744/*
3745 * call-seq:
3746 * Hash(object) -> object or new_hash
3747 *
3748 * Returns a hash converted from +object+.
3749 *
3750 * - If +object+ is:
3751 *
3752 * - A hash, returns +object+.
3753 * - An empty array or +nil+, returns an empty hash.
3754 *
3755 * - Otherwise, if <tt>object.to_hash</tt> returns a hash, returns that hash.
3756 * - Otherwise, returns TypeError.
3757 *
3758 * Examples:
3759 *
3760 * Hash({foo: 0, bar: 1}) # => {:foo=>0, :bar=>1}
3761 * Hash(nil) # => {}
3762 * Hash([]) # => {}
3763 *
3764 */
3765
3766static VALUE
3767rb_f_hash(VALUE obj, VALUE arg)
3768{
3769 return rb_Hash(arg);
3770}
3771
3773struct dig_method {
3774 VALUE klass;
3775 int basic;
3776};
3777
3778static ID id_dig;
3779
3780static int
3781dig_basic_p(VALUE obj, struct dig_method *cache)
3782{
3783 VALUE klass = RBASIC_CLASS(obj);
3784 if (klass != cache->klass) {
3785 cache->klass = klass;
3786 cache->basic = rb_method_basic_definition_p(klass, id_dig);
3787 }
3788 return cache->basic;
3789}
3790
3791static void
3792no_dig_method(int found, VALUE recv, ID mid, int argc, const VALUE *argv, VALUE data)
3793{
3794 if (!found) {
3795 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not have #dig method",
3796 CLASS_OF(data));
3797 }
3798}
3799
3801VALUE
3802rb_obj_dig(int argc, VALUE *argv, VALUE obj, VALUE notfound)
3803{
3804 struct dig_method hash = {Qnil}, ary = {Qnil}, strt = {Qnil};
3805
3806 for (; argc > 0; ++argv, --argc) {
3807 if (NIL_P(obj)) return notfound;
3808 if (!SPECIAL_CONST_P(obj)) {
3809 switch (BUILTIN_TYPE(obj)) {
3810 case T_HASH:
3811 if (dig_basic_p(obj, &hash)) {
3812 obj = rb_hash_aref(obj, *argv);
3813 continue;
3814 }
3815 break;
3816 case T_ARRAY:
3817 if (dig_basic_p(obj, &ary)) {
3818 obj = rb_ary_at(obj, *argv);
3819 continue;
3820 }
3821 break;
3822 case T_STRUCT:
3823 if (dig_basic_p(obj, &strt)) {
3824 obj = rb_struct_lookup(obj, *argv);
3825 continue;
3826 }
3827 break;
3828 default:
3829 break;
3830 }
3831 }
3832 return rb_check_funcall_with_hook_kw(obj, id_dig, argc, argv,
3833 no_dig_method, obj,
3835 }
3836 return obj;
3837}
3838
3839/*
3840 * call-seq:
3841 * sprintf(format_string *objects) -> string
3842 *
3843 * Returns the string resulting from formatting +objects+
3844 * into +format_string+.
3845 *
3846 * For details on +format_string+, see
3847 * {Format Specifications}[rdoc-ref:format_specifications.rdoc].
3848 *
3849 * Kernel#format is an alias for Kernel#sprintf.
3850 *
3851 */
3852
3853static VALUE
3854f_sprintf(int c, const VALUE *v, VALUE _)
3855{
3856 return rb_f_sprintf(c, v);
3857}
3858
3859/*
3860 * Document-class: Class
3861 *
3862 * Classes in Ruby are first-class objects---each is an instance of
3863 * class Class.
3864 *
3865 * Typically, you create a new class by using:
3866 *
3867 * class Name
3868 * # some code describing the class behavior
3869 * end
3870 *
3871 * When a new class is created, an object of type Class is initialized and
3872 * assigned to a global constant (Name in this case).
3873 *
3874 * When <code>Name.new</code> is called to create a new object, the
3875 * #new method in Class is run by default.
3876 * This can be demonstrated by overriding #new in Class:
3877 *
3878 * class Class
3879 * alias old_new new
3880 * def new(*args)
3881 * print "Creating a new ", self.name, "\n"
3882 * old_new(*args)
3883 * end
3884 * end
3885 *
3886 * class Name
3887 * end
3888 *
3889 * n = Name.new
3890 *
3891 * <em>produces:</em>
3892 *
3893 * Creating a new Name
3894 *
3895 * Classes, modules, and objects are interrelated. In the diagram
3896 * that follows, the vertical arrows represent inheritance, and the
3897 * parentheses metaclasses. All metaclasses are instances
3898 * of the class `Class'.
3899 * +---------+ +-...
3900 * | | |
3901 * BasicObject-----|-->(BasicObject)-------|-...
3902 * ^ | ^ |
3903 * | | | |
3904 * Object---------|----->(Object)---------|-...
3905 * ^ | ^ |
3906 * | | | |
3907 * +-------+ | +--------+ |
3908 * | | | | | |
3909 * | Module-|---------|--->(Module)-|-...
3910 * | ^ | | ^ |
3911 * | | | | | |
3912 * | Class-|---------|---->(Class)-|-...
3913 * | ^ | | ^ |
3914 * | +---+ | +----+
3915 * | |
3916 * obj--->OtherClass---------->(OtherClass)-----------...
3917 *
3918 */
3919
3920
3921/* Document-class: BasicObject
3922 *
3923 * BasicObject is the parent class of all classes in Ruby. It's an explicit
3924 * blank class.
3925 *
3926 * BasicObject can be used for creating object hierarchies independent of
3927 * Ruby's object hierarchy, proxy objects like the Delegator class, or other
3928 * uses where namespace pollution from Ruby's methods and classes must be
3929 * avoided.
3930 *
3931 * To avoid polluting BasicObject for other users an appropriately named
3932 * subclass of BasicObject should be created instead of directly modifying
3933 * BasicObject:
3934 *
3935 * class MyObjectSystem < BasicObject
3936 * end
3937 *
3938 * BasicObject does not include Kernel (for methods like +puts+) and
3939 * BasicObject is outside of the namespace of the standard library so common
3940 * classes will not be found without using a full class path.
3941 *
3942 * A variety of strategies can be used to provide useful portions of the
3943 * standard library to subclasses of BasicObject. A subclass could
3944 * <code>include Kernel</code> to obtain +puts+, +exit+, etc. A custom
3945 * Kernel-like module could be created and included or delegation can be used
3946 * via #method_missing:
3947 *
3948 * class MyObjectSystem < BasicObject
3949 * DELEGATE = [:puts, :p]
3950 *
3951 * def method_missing(name, *args, &block)
3952 * return super unless DELEGATE.include? name
3953 * ::Kernel.send(name, *args, &block)
3954 * end
3955 *
3956 * def respond_to_missing?(name, include_private = false)
3957 * DELEGATE.include?(name) or super
3958 * end
3959 * end
3960 *
3961 * Access to classes and modules from the Ruby standard library can be
3962 * obtained in a BasicObject subclass by referencing the desired constant
3963 * from the root like <code>::File</code> or <code>::Enumerator</code>.
3964 * Like #method_missing, #const_missing can be used to delegate constant
3965 * lookup to +Object+:
3966 *
3967 * class MyObjectSystem < BasicObject
3968 * def self.const_missing(name)
3969 * ::Object.const_get(name)
3970 * end
3971 * end
3972 *
3973 * === What's Here
3974 *
3975 * These are the methods defined for \BasicObject:
3976 *
3977 * - ::new: Returns a new \BasicObject instance.
3978 * - #!: Returns the boolean negation of +self+: +true+ or +false+.
3979 * - #!=: Returns whether +self+ and the given object are _not_ equal.
3980 * - #==: Returns whether +self+ and the given object are equivalent.
3981 * - #__id__: Returns the integer object identifier for +self+.
3982 * - #__send__: Calls the method identified by the given symbol.
3983 * - #equal?: Returns whether +self+ and the given object are the same object.
3984 * - #instance_eval: Evaluates the given string or block in the context of +self+.
3985 * - #instance_exec: Executes the given block in the context of +self+,
3986 * passing the given arguments.
3987 *
3988 */
3989
3990/* Document-class: Object
3991 *
3992 * Object is the default root of all Ruby objects. Object inherits from
3993 * BasicObject which allows creating alternate object hierarchies. Methods
3994 * on Object are available to all classes unless explicitly overridden.
3995 *
3996 * Object mixes in the Kernel module, making the built-in kernel functions
3997 * globally accessible. Although the instance methods of Object are defined
3998 * by the Kernel module, we have chosen to document them here for clarity.
3999 *
4000 * When referencing constants in classes inheriting from Object you do not
4001 * need to use the full namespace. For example, referencing +File+ inside
4002 * +YourClass+ will find the top-level File class.
4003 *
4004 * In the descriptions of Object's methods, the parameter <i>symbol</i> refers
4005 * to a symbol, which is either a quoted string or a Symbol (such as
4006 * <code>:name</code>).
4007 *
4008 * == What's Here
4009 *
4010 * First, what's elsewhere. \Class \Object:
4011 *
4012 * - Inherits from {class BasicObject}[rdoc-ref:BasicObject@What-27s+Here].
4013 * - Includes {module Kernel}[rdoc-ref:Kernel@What-27s+Here].
4014 *
4015 * Here, class \Object provides methods for:
4016 *
4017 * - {Querying}[rdoc-ref:Object@Querying]
4018 * - {Instance Variables}[rdoc-ref:Object@Instance+Variables]
4019 * - {Other}[rdoc-ref:Object@Other]
4020 *
4021 * === Querying
4022 *
4023 * - #!~: Returns +true+ if +self+ does not match the given object,
4024 * otherwise +false+.
4025 * - #<=>: Returns 0 if +self+ and the given object +object+ are the same
4026 * object, or if <tt>self == object</tt>; otherwise returns +nil+.
4027 * - #===: Implements case equality, effectively the same as calling #==.
4028 * - #eql?: Implements hash equality, effectively the same as calling #==.
4029 * - #kind_of? (aliased as #is_a?): Returns whether given argument is an ancestor
4030 * of the singleton class of +self+.
4031 * - #instance_of?: Returns whether +self+ is an instance of the given class.
4032 * - #instance_variable_defined?: Returns whether the given instance variable
4033 * is defined in +self+.
4034 * - #method: Returns the Method object for the given method in +self+.
4035 * - #methods: Returns an array of symbol names of public and protected methods
4036 * in +self+.
4037 * - #nil?: Returns +false+. (Only +nil+ responds +true+ to method <tt>nil?</tt>.)
4038 * - #object_id: Returns an integer corresponding to +self+ that is unique
4039 * for the current process
4040 * - #private_methods: Returns an array of the symbol names
4041 * of the private methods in +self+.
4042 * - #protected_methods: Returns an array of the symbol names
4043 * of the protected methods in +self+.
4044 * - #public_method: Returns the Method object for the given public method in +self+.
4045 * - #public_methods: Returns an array of the symbol names
4046 * of the public methods in +self+.
4047 * - #respond_to?: Returns whether +self+ responds to the given method.
4048 * - #singleton_class: Returns the singleton class of +self+.
4049 * - #singleton_method: Returns the Method object for the given singleton method
4050 * in +self+.
4051 * - #singleton_methods: Returns an array of the symbol names
4052 * of the singleton methods in +self+.
4053 *
4054 * - #define_singleton_method: Defines a singleton method in +self+
4055 * for the given symbol method-name and block or proc.
4056 * - #extend: Includes the given modules in the singleton class of +self+.
4057 * - #public_send: Calls the given public method in +self+ with the given argument.
4058 * - #send: Calls the given method in +self+ with the given argument.
4059 *
4060 * === Instance Variables
4061 *
4062 * - #instance_variable_get: Returns the value of the given instance variable
4063 * in +self+, or +nil+ if the instance variable is not set.
4064 * - #instance_variable_set: Sets the value of the given instance variable in +self+
4065 * to the given object.
4066 * - #instance_variables: Returns an array of the symbol names
4067 * of the instance variables in +self+.
4068 * - #remove_instance_variable: Removes the named instance variable from +self+.
4069 *
4070 * === Other
4071 *
4072 * - #clone: Returns a shallow copy of +self+, including singleton class
4073 * and frozen state.
4074 * - #define_singleton_method: Defines a singleton method in +self+
4075 * for the given symbol method-name and block or proc.
4076 * - #display: Prints +self+ to the given \IO stream or <tt>$stdout</tt>.
4077 * - #dup: Returns a shallow unfrozen copy of +self+.
4078 * - #enum_for (aliased as #to_enum): Returns an Enumerator for +self+
4079 * using the using the given method, arguments, and block.
4080 * - #extend: Includes the given modules in the singleton class of +self+.
4081 * - #freeze: Prevents further modifications to +self+.
4082 * - #hash: Returns the integer hash value for +self+.
4083 * - #inspect: Returns a human-readable string representation of +self+.
4084 * - #itself: Returns +self+.
4085 * - #method_missing: Method called when an undefined method is called on +self+.
4086 * - #public_send: Calls the given public method in +self+ with the given argument.
4087 * - #send: Calls the given method in +self+ with the given argument.
4088 * - #to_s: Returns a string representation of +self+.
4089 *
4090 */
4091
4111void
4112InitVM_Object(void)
4113{
4115
4116#if 0
4117 // teach RDoc about these classes
4118 rb_cBasicObject = rb_define_class("BasicObject", Qnil);
4122 rb_cRefinement = rb_define_class("Refinement", rb_cModule);
4123#endif
4124
4125 rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_initialize, 0);
4126 rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
4127 rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
4128 rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
4129 rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
4130 rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);
4131
4132 rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_singleton_method_added, 1);
4133 rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_singleton_method_removed, 1);
4134 rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_singleton_method_undefined, 1);
4135
4136 /* Document-module: Kernel
4137 *
4138 * The Kernel module is included by class Object, so its methods are
4139 * available in every Ruby object.
4140 *
4141 * The Kernel instance methods are documented in class Object while the
4142 * module methods are documented here. These methods are called without a
4143 * receiver and thus can be called in functional form:
4144 *
4145 * sprintf "%.1f", 1.234 #=> "1.2"
4146 *
4147 * == What's Here
4148 *
4149 * \Module \Kernel provides methods that are useful for:
4150 *
4151 * - {Converting}[rdoc-ref:Kernel@Converting]
4152 * - {Querying}[rdoc-ref:Kernel@Querying]
4153 * - {Exiting}[rdoc-ref:Kernel@Exiting]
4154 * - {Exceptions}[rdoc-ref:Kernel@Exceptions]
4155 * - {IO}[rdoc-ref:Kernel@IO]
4156 * - {Procs}[rdoc-ref:Kernel@Procs]
4157 * - {Tracing}[rdoc-ref:Kernel@Tracing]
4158 * - {Subprocesses}[rdoc-ref:Kernel@Subprocesses]
4159 * - {Loading}[rdoc-ref:Kernel@Loading]
4160 * - {Yielding}[rdoc-ref:Kernel@Yielding]
4161 * - {Random Values}[rdoc-ref:Kernel@Random+Values]
4162 * - {Other}[rdoc-ref:Kernel@Other]
4163 *
4164 * === Converting
4165 *
4166 * - #Array: Returns an Array based on the given argument.
4167 * - #Complex: Returns a Complex based on the given arguments.
4168 * - #Float: Returns a Float based on the given arguments.
4169 * - #Hash: Returns a Hash based on the given argument.
4170 * - #Integer: Returns an Integer based on the given arguments.
4171 * - #Rational: Returns a Rational based on the given arguments.
4172 * - #String: Returns a String based on the given argument.
4173 *
4174 * === Querying
4175 *
4176 * - #__callee__: Returns the called name of the current method as a symbol.
4177 * - #__dir__: Returns the path to the directory from which the current
4178 * method is called.
4179 * - #__method__: Returns the name of the current method as a symbol.
4180 * - #autoload?: Returns the file to be loaded when the given module is referenced.
4181 * - #binding: Returns a Binding for the context at the point of call.
4182 * - #block_given?: Returns +true+ if a block was passed to the calling method.
4183 * - #caller: Returns the current execution stack as an array of strings.
4184 * - #caller_locations: Returns the current execution stack as an array
4185 * of Thread::Backtrace::Location objects.
4186 * - #class: Returns the class of +self+.
4187 * - #frozen?: Returns whether +self+ is frozen.
4188 * - #global_variables: Returns an array of global variables as symbols.
4189 * - #local_variables: Returns an array of local variables as symbols.
4190 * - #test: Performs specified tests on the given single file or pair of files.
4191 *
4192 * === Exiting
4193 *
4194 * - #abort: Exits the current process after printing the given arguments.
4195 * - #at_exit: Executes the given block when the process exits.
4196 * - #exit: Exits the current process after calling any registered
4197 * +at_exit+ handlers.
4198 * - #exit!: Exits the current process without calling any registered
4199 * +at_exit+ handlers.
4200 *
4201 * === Exceptions
4202 *
4203 * - #catch: Executes the given block, possibly catching a thrown object.
4204 * - #raise (aliased as #fail): Raises an exception based on the given arguments.
4205 * - #throw: Returns from the active catch block waiting for the given tag.
4206 *
4207 *
4208 * === \IO
4209 *
4210 * - ::pp: Prints the given objects in pretty form.
4211 * - #gets: Returns and assigns to <tt>$_</tt> the next line from the current input.
4212 * - #open: Creates an IO object connected to the given stream, file, or subprocess.
4213 * - #p: Prints the given objects' inspect output to the standard output.
4214 * - #print: Prints the given objects to standard output without a newline.
4215 * - #printf: Prints the string resulting from applying the given format string
4216 * to any additional arguments.
4217 * - #putc: Equivalent to <tt.$stdout.putc(object)</tt> for the given object.
4218 * - #puts: Equivalent to <tt>$stdout.puts(*objects)</tt> for the given objects.
4219 * - #readline: Similar to #gets, but raises an exception at the end of file.
4220 * - #readlines: Returns an array of the remaining lines from the current input.
4221 * - #select: Same as IO.select.
4222 *
4223 * === Procs
4224 *
4225 * - #lambda: Returns a lambda proc for the given block.
4226 * - #proc: Returns a new Proc; equivalent to Proc.new.
4227 *
4228 * === Tracing
4229 *
4230 * - #set_trace_func: Sets the given proc as the handler for tracing,
4231 * or disables tracing if given +nil+.
4232 * - #trace_var: Starts tracing assignments to the given global variable.
4233 * - #untrace_var: Disables tracing of assignments to the given global variable.
4234 *
4235 * === Subprocesses
4236 *
4237 * - {\`command`}[rdoc-ref:Kernel#`]: Returns the standard output of running
4238 * +command+ in a subshell.
4239 * - #exec: Replaces current process with a new process.
4240 * - #fork: Forks the current process into two processes.
4241 * - #spawn: Executes the given command and returns its pid without waiting
4242 * for completion.
4243 * - #system: Executes the given command in a subshell.
4244 *
4245 * === Loading
4246 *
4247 * - #autoload: Registers the given file to be loaded when the given constant
4248 * is first referenced.
4249 * - #load: Loads the given Ruby file.
4250 * - #require: Loads the given Ruby file unless it has already been loaded.
4251 * - #require_relative: Loads the Ruby file path relative to the calling file,
4252 * unless it has already been loaded.
4253 *
4254 * === Yielding
4255 *
4256 * - #tap: Yields +self+ to the given block; returns +self+.
4257 * - #then (aliased as #yield_self): Yields +self+ to the block
4258 * and returns the result of the block.
4259 *
4260 * === \Random Values
4261 *
4262 * - #rand: Returns a pseudo-random floating point number
4263 * strictly between 0.0 and 1.0.
4264 * - #srand: Seeds the pseudo-random number generator with the given number.
4265 *
4266 * === Other
4267 *
4268 * - #eval: Evaluates the given string as Ruby code.
4269 * - #loop: Repeatedly executes the given block.
4270 * - #sleep: Suspends the current thread for the given number of seconds.
4271 * - #sprintf (aliased as #format): Returns the string resulting from applying
4272 * the given format string to any additional arguments.
4273 * - #syscall: Runs an operating system call.
4274 * - #trap: Specifies the handling of system signals.
4275 * - #warn: Issue a warning based on the given messages and options.
4276 *
4277 */
4278 rb_mKernel = rb_define_module("Kernel");
4280 rb_define_private_method(rb_cClass, "inherited", rb_obj_class_inherited, 1);
4281 rb_define_private_method(rb_cModule, "included", rb_obj_mod_included, 1);
4282 rb_define_private_method(rb_cModule, "extended", rb_obj_mod_extended, 1);
4283 rb_define_private_method(rb_cModule, "prepended", rb_obj_mod_prepended, 1);
4284 rb_define_private_method(rb_cModule, "method_added", rb_obj_mod_method_added, 1);
4285 rb_define_private_method(rb_cModule, "const_added", rb_obj_mod_const_added, 1);
4286 rb_define_private_method(rb_cModule, "method_removed", rb_obj_mod_method_removed, 1);
4287 rb_define_private_method(rb_cModule, "method_undefined", rb_obj_mod_method_undefined, 1);
4288
4289 rb_define_method(rb_mKernel, "nil?", rb_false, 0);
4291 rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
4292 rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
4293 rb_define_method(rb_mKernel, "hash", rb_obj_hash, 0); /* in hash.c */
4294 rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
4295
4296 rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
4298 rb_define_method(rb_mKernel, "itself", rb_obj_itself, 0);
4299 rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
4300 rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
4301 rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_clone, -1);
4302
4304
4306 rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
4307 rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */
4308 rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
4309 rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
4310 rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */
4311 rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */
4312 rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
4313 rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
4314 rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set_m, 2);
4315 rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
4316 rb_define_method(rb_mKernel, "remove_instance_variable",
4317 rb_obj_remove_instance_variable, 1); /* in variable.c */
4318
4322
4323 rb_define_global_function("sprintf", f_sprintf, -1);
4324 rb_define_global_function("format", f_sprintf, -1);
4325
4326 rb_define_global_function("Integer", rb_f_integer, -1);
4327
4328 rb_define_global_function("String", rb_f_string, 1);
4329 rb_define_global_function("Array", rb_f_array, 1);
4330 rb_define_global_function("Hash", rb_f_hash, 1);
4331
4333 rb_cNilClass_to_s = rb_fstring_enc_lit("", rb_usascii_encoding());
4334 rb_gc_register_mark_object(rb_cNilClass_to_s);
4335 rb_define_method(rb_cNilClass, "to_s", rb_nil_to_s, 0);
4336 rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
4337 rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0);
4338 rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
4339 rb_define_method(rb_cNilClass, "=~", nil_match, 1);
4340 rb_define_method(rb_cNilClass, "&", false_and, 1);
4341 rb_define_method(rb_cNilClass, "|", false_or, 1);
4342 rb_define_method(rb_cNilClass, "^", false_xor, 1);
4344
4345 rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
4348
4349 rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
4350 rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
4351 rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
4352 rb_define_method(rb_cModule, "<=>", rb_mod_cmp, 1);
4353 rb_define_method(rb_cModule, "<", rb_mod_lt, 1);
4355 rb_define_method(rb_cModule, ">", rb_mod_gt, 1);
4356 rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
4357 rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
4358 rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
4359 rb_define_alias(rb_cModule, "inspect", "to_s");
4360 rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
4361 rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
4362 rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */
4363 rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
4364
4365 rb_define_method(rb_cModule, "attr", rb_mod_attr, -1);
4366 rb_define_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
4367 rb_define_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
4368 rb_define_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
4369
4370 rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
4372 rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
4373 rb_define_method(rb_cModule, "initialize_clone", rb_mod_initialize_clone, -1);
4374 rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
4375 rb_define_method(rb_cModule, "public_instance_methods",
4376 rb_class_public_instance_methods, -1); /* in class.c */
4377 rb_define_method(rb_cModule, "protected_instance_methods",
4378 rb_class_protected_instance_methods, -1); /* in class.c */
4379 rb_define_method(rb_cModule, "private_instance_methods",
4380 rb_class_private_instance_methods, -1); /* in class.c */
4381 rb_define_method(rb_cModule, "undefined_instance_methods",
4382 rb_class_undefined_instance_methods, 0); /* in class.c */
4383
4384 rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
4385 rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
4386 rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
4387 rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
4388 rb_define_method(rb_cModule, "const_source_location", rb_mod_const_source_location, -1);
4389 rb_define_private_method(rb_cModule, "remove_const",
4390 rb_mod_remove_const, 1); /* in variable.c */
4391 rb_define_method(rb_cModule, "const_missing",
4392 rb_mod_const_missing, 1); /* in variable.c */
4393 rb_define_method(rb_cModule, "class_variables",
4394 rb_mod_class_variables, -1); /* in variable.c */
4395 rb_define_method(rb_cModule, "remove_class_variable",
4396 rb_mod_remove_cvar, 1); /* in variable.c */
4397 rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
4398 rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
4399 rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
4400 rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); /* in variable.c */
4401 rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); /* in variable.c */
4402 rb_define_method(rb_cModule, "deprecate_constant", rb_mod_deprecate_constant, -1); /* in variable.c */
4403 rb_define_method(rb_cModule, "singleton_class?", rb_mod_singleton_p, 0);
4404
4405 rb_define_method(rb_singleton_class(rb_cClass), "allocate", rb_class_alloc_m, 0);
4406 rb_define_method(rb_cClass, "allocate", rb_class_alloc_m, 0);
4408 rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
4410 rb_define_method(rb_cClass, "subclasses", rb_class_subclasses, 0); /* in class.c */
4411 rb_define_method(rb_cClass, "attached_object", rb_class_attached_object, 0); /* in class.c */
4412 rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
4413 rb_undef_method(rb_cClass, "extend_object");
4414 rb_undef_method(rb_cClass, "append_features");
4415 rb_undef_method(rb_cClass, "prepend_features");
4416
4418 rb_cTrueClass_to_s = rb_fstring_enc_lit("true", rb_usascii_encoding());
4419 rb_gc_register_mark_object(rb_cTrueClass_to_s);
4420 rb_define_method(rb_cTrueClass, "to_s", rb_true_to_s, 0);
4421 rb_define_alias(rb_cTrueClass, "inspect", "to_s");
4422 rb_define_method(rb_cTrueClass, "&", true_and, 1);
4423 rb_define_method(rb_cTrueClass, "|", true_or, 1);
4424 rb_define_method(rb_cTrueClass, "^", true_xor, 1);
4428
4429 rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
4430 rb_cFalseClass_to_s = rb_fstring_enc_lit("false", rb_usascii_encoding());
4431 rb_gc_register_mark_object(rb_cFalseClass_to_s);
4432 rb_define_method(rb_cFalseClass, "to_s", rb_false_to_s, 0);
4433 rb_define_alias(rb_cFalseClass, "inspect", "to_s");
4434 rb_define_method(rb_cFalseClass, "&", false_and, 1);
4435 rb_define_method(rb_cFalseClass, "|", false_or, 1);
4436 rb_define_method(rb_cFalseClass, "^", false_xor, 1);
4440}
4441
4442#include "kernel.rbinc"
4443#include "nilclass.rbinc"
4444
4445void
4446Init_Object(void)
4447{
4448 id_dig = rb_intern_const("dig");
4449 InitVM(Object);
4450}
4451
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:177
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
Definition fl_type.h:921
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are protected only.
Definition class.c:1854
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1125
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:923
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition class.c:1634
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2236
void Init_class_hierarchy(void)
Internal header aggregating init functions.
Definition class.c:842
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
Definition class.c:1657
VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
Identical to rb_class_instance_methods(), except it returns names of singleton methods instead of ins...
Definition class.c:2031
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
Generates an array of symbols, which are the list of method names defined in the passed class.
Definition class.c:1839
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
Definition class.c:310
VALUE rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are public only.
Definition class.c:1892
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1033
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:672
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition class.c:1452
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition class.c:1520
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:914
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition class.c:1488
VALUE rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are private only.
Definition class.c:1877
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition class.c:498
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2284
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition class.c:2345
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2108
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:2574
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:868
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2363
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition value_type.h:59
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:107
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
Definition value_type.h:87
#define FL_EXIVAR
Old name of RUBY_FL_EXIVAR.
Definition fl_type.h:67
#define ALLOCV
Old name of RB_ALLOCV.
Definition memory.h:398
#define ISSPACE
Old name of rb_isspace.
Definition ctype.h:88
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define T_MASK
Old name of RUBY_T_MASK.
Definition value_type.h:68
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
Definition fl_type.h:145
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1683
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:143
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
#define T_NONE
Old name of RUBY_T_NONE.
Definition value_type.h:74
#define FIXABLE
Old name of RB_FIXABLE.
Definition fixnum.h:25
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define ISDIGIT
Old name of rb_isdigit.
Definition ctype.h:93
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition value_type.h:76
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:652
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
Definition string.h:1680
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:139
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
Definition fl_type.h:68
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:651
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition memory.h:400
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition error.c:3150
void rb_category_warning(rb_warning_category_t category, const char *fmt,...)
Identical to rb_warning(), except it takes additional "category" parameter.
Definition error.c:453
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition error.c:794
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
Definition eval.c:1880
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1091
void rb_invalid_str(const char *str, const char *type)
Honestly I don't understand the name, but it raises an instance of rb_eArgError.
Definition error.c:2182
VALUE rb_eArgError
ArgumentError exception.
Definition error.c:1092
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:442
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
Definition error.h:48
VALUE rb_cClass
Class class.
Definition object.c:54
VALUE rb_cRational
Rational class.
Definition rational.c:47
VALUE rb_class_superclass(VALUE klass)
Returns the superclass of klass.
Definition object.c:1995
VALUE rb_class_get_superclass(VALUE klass)
Returns the superclass of a class.
Definition object.c:2017
VALUE rb_convert_type(VALUE val, int type, const char *tname, const char *method)
Converts an object into another type.
Definition object.c:2934
#define case_equal
call-seq: obj === other -> true or false
Definition object.c:117
VALUE rb_Float(VALUE val)
This is the logic behind Kernel#Float.
Definition object.c:3532
VALUE rb_mKernel
Kernel module.
Definition object.c:51
VALUE rb_check_to_int(VALUE val)
Identical to rb_check_to_integer(), except it uses #to_int for conversion.
Definition object.c:3032
VALUE rb_obj_reveal(VALUE obj, VALUE klass)
Make a hidden object visible again.
Definition object.c:93
VALUE rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
Identical to rb_convert_type(), except it returns RUBY_Qnil instead of raising exceptions,...
Definition object.c:2961
VALUE rb_cObject
Documented in include/ruby/internal/globals.h.
Definition object.c:52
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
Definition object.c:589
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:1939
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition object.c:1980
VALUE rb_class_new_instance_kw(int argc, const VALUE *argv, VALUE klass, int kw_splat)
Identical to rb_class_new_instance(), except you can specify how to handle the last element of the gi...
Definition object.c:1968
VALUE rb_cRefinement
Refinement class.
Definition object.c:55
VALUE rb_cInteger
Module class.
Definition numeric.c:192
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:84
VALUE rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
Identical to rb_class_new_instance(), except it passes the passed keywords if any to the #initialize ...
Definition object.c:1957
VALUE rb_check_to_float(VALUE val)
This is complicated.
Definition object.c:3571
static VALUE rb_obj_init_clone(int argc, VALUE *argv, VALUE obj)
Default implementation of #initialize_clone.
Definition object.c:567
VALUE rb_cNilClass
NilClass class.
Definition object.c:57
VALUE rb_Hash(VALUE val)
Equivalent to Kernel#Hash in Ruby.
Definition object.c:3730
VALUE rb_obj_frozen_p(VALUE obj)
Just calls RB_OBJ_FROZEN() inside.
Definition object.c:1194
VALUE rb_obj_init_copy(VALUE obj, VALUE orig)
Default implementation of #initialize_copy.
Definition object.c:536
int rb_eql(VALUE obj1, VALUE obj2)
Checks for equality of the passed objects, in terms of Object#eql?.
Definition object.c:135
double rb_str_to_dbl(VALUE str, int badcheck)
Identical to rb_cstr_to_dbl(), except it accepts a Ruby's string instead of C's.
Definition object.c:3416
VALUE rb_Integer(VALUE val)
This is the logic behind Kernel#Integer.
Definition object.c:3101
VALUE rb_cFalseClass
FalseClass class.
Definition object.c:59
VALUE rb_cNumeric
Numeric class.
Definition numeric.c:190
VALUE rb_Array(VALUE val)
This is the logic behind Kernel#Array.
Definition object.c:3687
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:190
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
Definition object.c:487
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:600
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:50
VALUE rb_cModule
Module class.
Definition object.c:53
VALUE rb_class_inherited_p(VALUE mod, VALUE arg)
Determines if the given two modules are relatives.
Definition object.c:1610
VALUE rb_obj_is_instance_of(VALUE obj, VALUE c)
Queries if the given object is a direct instance of the given class.
Definition object.c:731
VALUE rb_class_real(VALUE cl)
Finds a "real" class.
Definition object.c:180
VALUE rb_obj_init_dup_clone(VALUE obj, VALUE orig)
Default implementation of #initialize_dup.
Definition object.c:553
VALUE rb_to_float(VALUE val)
Identical to rb_check_to_float(), except it raises on error.
Definition object.c:3561
double rb_num2dbl(VALUE val)
Converts an instance of rb_cNumeric into C's double.
Definition object.c:3623
VALUE rb_equal(VALUE obj1, VALUE obj2)
This function is an optimised version of calling #==.
Definition object.c:122
VALUE rb_obj_clone(VALUE obj)
Produces a shallow copy of the given object.
Definition object.c:441
VALUE rb_obj_is_kind_of(VALUE obj, VALUE c)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition object.c:787
double rb_cstr_to_dbl(const char *p, int badcheck)
Converts a textual representation of a real number into a numeric, which is the nearest value that th...
Definition object.c:3375
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition object.c:1182
VALUE rb_check_to_integer(VALUE val, const char *method)
Identical to rb_check_convert_type(), except the return value type is fixed to rb_cInteger.
Definition object.c:3013
VALUE rb_class_search_ancestor(VALUE klass, VALUE super)
Internal header for Object.
Definition object.c:845
VALUE rb_String(VALUE val)
This is the logic behind Kernel#String.
Definition object.c:3655
VALUE rb_cTrueClass
TrueClass class.
Definition object.c:58
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3026
VALUE rb_obj_setup(VALUE obj, VALUE klass, VALUE type)
Fills common fields in the object.
Definition object.c:102
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition rgengc.h:232
Encoding relates APIs.
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
Definition encoding.h:784
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
Definition string.c:833
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Identical to rb_check_id(), except it takes a pointer to a memory region instead of Ruby's string.
Definition symbol.c:1180
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1102
VALUE rb_funcallv_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_funcallv(), except you can specify how to handle the last element of the given array.
Definition vm_eval.c:1069
#define rb_check_frozen
Just another name of rb_check_frozen.
Definition error.h:264
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:280
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
Definition symbol.c:1048
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
Definition symbol.c:1030
ID rb_id_attrset(ID id)
Calculates an ID of attribute writer.
Definition symbol.c:114
int rb_is_local_id(ID id)
Classifies the given ID, then sees if it is a local variable.
Definition symbol.c:1060
VALUE rb_rational_num(VALUE rat)
Queries the numerator of the passed Rational.
Definition rational.c:1984
VALUE rb_rational_den(VALUE rat)
Queries the denominator of the passed Rational.
Definition rational.c:1990
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3353
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
Definition string.c:2826
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
Definition string.c:3319
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
Definition string.c:3453
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2640
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:844
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
Definition string.c:1682
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
VALUE rb_mod_remove_cvar(VALUE mod, VALUE name)
Resembles Module#remove_class_variable.
Definition variable.c:3913
VALUE rb_obj_instance_variables(VALUE obj)
Resembles Object#instance_variables.
Definition variable.c:1922
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:2896
VALUE rb_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
Definition variable.c:1226
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1606
VALUE rb_mod_remove_const(VALUE space, VALUE name)
Resembles Module#remove_const.
Definition variable.c:2988
void rb_cvar_set(VALUE klass, ID name, VALUE val)
Assigns a value to a class variable.
Definition variable.c:3677
VALUE rb_cvar_get(VALUE klass, ID name)
Obtains a value from a class variable.
Definition variable.c:3747
VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE recv)
Resembles Module#constants.
Definition variable.c:3148
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1218
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:3346
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
Definition variable.c:137
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
Definition variable.c:310
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition variable.c:2902
VALUE rb_obj_remove_instance_variable(VALUE obj, VALUE name)
Resembles Object#remove_instance_variable.
Definition variable.c:1977
st_index_t rb_ivar_count(VALUE obj)
Number of instance variables defined on an object.
Definition variable.c:1838
VALUE rb_const_get_from(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition variable.c:2890
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
Definition variable.c:1623
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
Definition variable.c:3210
VALUE rb_mod_class_variables(int argc, const VALUE *argv, VALUE recv)
Resembles Module#class_variables.
Definition variable.c:3878
VALUE rb_cvar_defined(VALUE klass, ID name)
Queries if the given class has the given class variable.
Definition variable.c:3754
int rb_const_defined_from(VALUE space, ID name)
Identical to rb_const_defined(), except it returns false for private constants.
Definition variable.c:3198
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3204
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
Definition vm.h:216
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1142
void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility)
This function resembles now-deprecated Module#attr.
Definition vm_method.c:1720
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition vm_eval.c:664
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
Definition vm_method.c:1148
VALUE rb_mod_module_exec(int argc, const VALUE *argv, VALUE mod)
Identical to rb_obj_instance_exec(), except it evaluates within the context of module.
Definition vm_eval.c:2185
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
Definition vm_method.c:2789
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:276
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1084
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition symbol.c:789
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
Definition symbol.c:959
ID rb_intern_str(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
Definition symbol.c:795
#define strtod(s, e)
Just another name of ruby_strtod.
Definition util.h:212
VALUE rb_f_sprintf(int argc, const VALUE *argv)
Identical to rb_str_format(), except how the arguments are arranged.
Definition sprintf.c:208
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
Definition sprintf.c:1219
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
Definition sprintf.c:1242
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:366
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_ivar_foreach(VALUE q, int_type *w, VALUE e)
Iteration over each instance variable of the object.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
Definition variable.c:1740
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:68
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:152
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
#define RCLASS(obj)
Convenient casting macro.
Definition rclass.h:38
#define ROBJECT(obj)
Convenient casting macro.
Definition robject.h:43
static VALUE * ROBJECT_IVPTR(VALUE obj)
Queries the instance variables.
Definition robject.h:162
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:72
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
Definition rstring.h:82
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition rstring.h:484
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition rstring.h:498
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
Definition variable.c:316
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:325
#define InitVM(ext)
This macro is for internal use.
Definition ruby.h:230
#define RB_PASS_KEYWORDS
Pass keywords, final argument should be a hash of keywords.
Definition scan_args.h:72
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
Definition scan_args.h:78
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
Definition st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
Definition value_type.h:263
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:432
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:375