Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
object.c
Go to the documentation of this file.
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
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/symbol.h"
34#include "internal/variable.h"
35#include "probes.h"
36#include "ruby/encoding.h"
37#include "ruby/st.h"
38#include "ruby/util.h"
39#include "ruby/assert.h"
40#include "builtin.h"
41
57static VALUE rb_cNilClass_to_s;
58static VALUE rb_cTrueClass_to_s;
59static VALUE rb_cFalseClass_to_s;
60
63#define id_eq idEq
64#define id_eql idEqlP
65#define id_match idEqTilde
66#define id_inspect idInspect
67#define id_init_copy idInitialize_copy
68#define id_init_clone idInitialize_clone
69#define id_init_dup idInitialize_dup
70#define id_const_missing idConst_missing
71#define id_to_f idTo_f
72
73#define CLASS_OR_MODULE_P(obj) \
74 (!SPECIAL_CONST_P(obj) && \
75 (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
76
93{
94 if (!SPECIAL_CONST_P(obj)) {
95 RBASIC_CLEAR_CLASS(obj);
96 }
97 return obj;
98}
99
108VALUE
110{
111 if (!SPECIAL_CONST_P(obj)) {
112 RBASIC_SET_CLASS(obj, klass);
113 }
114 return obj;
115}
116
125VALUE
127{
128 RBASIC(obj)->flags = type;
129 RBASIC_SET_CLASS(obj, klass);
130 return obj;
131}
132
141#define case_equal rb_equal
142 /* The default implementation of #=== is
143 * to call #== with the rb_equal() optimization. */
144
156VALUE
158{
159 VALUE result;
160
161 if (obj1 == obj2) return Qtrue;
162 result = rb_equal_opt(obj1, obj2);
163 if (result == Qundef) {
164 result = rb_funcall(obj1, id_eq, 1, obj2);
165 }
166 if (RTEST(result)) return Qtrue;
167 return Qfalse;
168}
169
179int
180rb_eql(VALUE obj1, VALUE obj2)
181{
182 VALUE result;
183
184 if (obj1 == obj2) return Qtrue;
185 result = rb_eql_opt(obj1, obj2);
186 if (result == Qundef) {
187 result = rb_funcall(obj1, id_eql, 1, obj2);
188 }
189 if (RTEST(result)) return Qtrue;
190 return Qfalse;
191}
192
198{
199 if (obj1 == obj2) return Qtrue;
200 return Qfalse;
201}
202
204
211{
212 return RTEST(obj) ? Qfalse : Qtrue;
213}
214
221{
222 VALUE result = rb_funcall(obj1, id_eq, 1, obj2);
223 return RTEST(result) ? Qfalse : Qtrue;
224}
225
234VALUE
236{
237 while (cl &&
238 ((RBASIC(cl)->flags & FL_SINGLETON) || BUILTIN_TYPE(cl) == T_ICLASS)) {
239 cl = RCLASS_SUPER(cl);
240 }
241 return cl;
242}
243
244VALUE
246{
247 return rb_class_real(CLASS_OF(obj));
248}
249
250/*
251 * call-seq:
252 * obj.singleton_class -> class
253 *
254 * Returns the singleton class of <i>obj</i>. This method creates
255 * a new singleton class if <i>obj</i> does not have one.
256 *
257 * If <i>obj</i> is <code>nil</code>, <code>true</code>, or
258 * <code>false</code>, it returns NilClass, TrueClass, or FalseClass,
259 * respectively.
260 * If <i>obj</i> is an Integer, a Float or a Symbol, it raises a TypeError.
261 *
262 * Object.new.singleton_class #=> #<Class:#<Object:0xb7ce1e24>>
263 * String.singleton_class #=> #<Class:String>
264 * nil.singleton_class #=> NilClass
265 */
266
267static VALUE
268rb_obj_singleton_class(VALUE obj)
269{
270 return rb_singleton_class(obj);
271}
272
276{
277 RUBY_ASSERT(RBASIC(dest)->flags & ROBJECT_EMBED);
278
279 if (RBASIC(obj)->flags & ROBJECT_EMBED) {
280 MEMCPY(ROBJECT(dest)->as.ary, ROBJECT(obj)->as.ary, VALUE, ROBJECT_EMBED_LEN_MAX);
281 RBASIC(dest)->flags |= ROBJECT_EMBED;
282 }
283 else {
284 uint32_t len = ROBJECT(obj)->as.heap.numiv;
285 VALUE *ptr = 0;
286 if (len > 0) {
287 ptr = ALLOC_N(VALUE, len);
288 MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
289 }
290 ROBJECT(dest)->as.heap.ivptr = ptr;
291 ROBJECT(dest)->as.heap.numiv = len;
292 ROBJECT(dest)->as.heap.iv_index_tbl = ROBJECT(obj)->as.heap.iv_index_tbl;
293 RBASIC(dest)->flags &= ~ROBJECT_EMBED;
294 }
295}
296
297static void
298init_copy(VALUE dest, VALUE obj)
299{
300 if (OBJ_FROZEN(dest)) {
301 rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
302 }
303 RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
304 RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR);
306 rb_copy_generic_ivar(dest, obj);
307 rb_gc_copy_finalizer(dest, obj);
308 if (RB_TYPE_P(obj, T_OBJECT)) {
309 rb_obj_copy_ivar(dest, obj);
310 }
311}
312
313static VALUE freeze_opt(int argc, VALUE *argv);
314static VALUE immutable_obj_clone(VALUE obj, VALUE kwfreeze);
315static VALUE mutable_obj_clone(VALUE obj, VALUE kwfreeze);
316PUREFUNC(static inline int special_object_p(VALUE obj));
317static inline int
318special_object_p(VALUE obj)
319{
320 if (SPECIAL_CONST_P(obj)) return TRUE;
321 switch (BUILTIN_TYPE(obj)) {
322 case T_BIGNUM:
323 case T_FLOAT:
324 case T_SYMBOL:
325 case T_RATIONAL:
326 case T_COMPLEX:
327 /* not a comprehensive list */
328 return TRUE;
329 default:
330 return FALSE;
331 }
332}
333
334static VALUE
335obj_freeze_opt(VALUE freeze)
336{
337 switch(freeze) {
338 case Qfalse:
339 case Qtrue:
340 case Qnil:
341 break;
342 default:
343 rb_raise(rb_eArgError, "unexpected value for freeze: %"PRIsVALUE, rb_obj_class(freeze));
344 }
345
346 return freeze;
347}
348
349static VALUE
350rb_obj_clone2(rb_execution_context_t *ec, VALUE obj, VALUE freeze)
351{
352 VALUE kwfreeze = obj_freeze_opt(freeze);
353 if (!special_object_p(obj))
354 return mutable_obj_clone(obj, kwfreeze);
355 return immutable_obj_clone(obj, kwfreeze);
356}
357
359VALUE
361{
362 VALUE kwfreeze = freeze_opt(argc, argv);
363 return immutable_obj_clone(obj, kwfreeze);
364}
365
366static VALUE
367freeze_opt(int argc, VALUE *argv)
368{
369 static ID keyword_ids[1];
370 VALUE opt;
371 VALUE kwfreeze = Qnil;
372
373 if (!keyword_ids[0]) {
374 CONST_ID(keyword_ids[0], "freeze");
375 }
376 rb_scan_args(argc, argv, "0:", &opt);
377 if (!NIL_P(opt)) {
378 rb_get_kwargs(opt, keyword_ids, 0, 1, &kwfreeze);
379 if (kwfreeze != Qundef)
380 kwfreeze = obj_freeze_opt(kwfreeze);
381 }
382 return kwfreeze;
383}
384
385static VALUE
386immutable_obj_clone(VALUE obj, VALUE kwfreeze)
387{
388 if (kwfreeze == Qfalse)
389 rb_raise(rb_eArgError, "can't unfreeze %"PRIsVALUE,
390 rb_obj_class(obj));
391 return obj;
392}
393
394static VALUE
395mutable_obj_clone(VALUE obj, VALUE kwfreeze)
396{
397 VALUE clone, singleton;
398 VALUE argv[2];
399
400 clone = rb_obj_alloc(rb_obj_class(obj));
401
402 singleton = rb_singleton_class_clone_and_attach(obj, clone);
403 RBASIC_SET_CLASS(clone, singleton);
404 if (FL_TEST(singleton, FL_SINGLETON)) {
405 rb_singleton_class_attached(singleton, clone);
406 }
407
408 init_copy(clone, obj);
409
410 switch (kwfreeze) {
411 case Qnil:
412 rb_funcall(clone, id_init_clone, 1, obj);
413 RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
414 break;
415 case Qtrue:
416 {
417 static VALUE freeze_true_hash;
418 if (!freeze_true_hash) {
419 freeze_true_hash = rb_hash_new();
420 rb_gc_register_mark_object(freeze_true_hash);
421 rb_hash_aset(freeze_true_hash, ID2SYM(rb_intern("freeze")), Qtrue);
422 rb_obj_freeze(freeze_true_hash);
423 }
424
425 argv[0] = obj;
426 argv[1] = freeze_true_hash;
427 rb_funcallv_kw(clone, id_init_clone, 2, argv, RB_PASS_KEYWORDS);
428 RBASIC(clone)->flags |= FL_FREEZE;
429 break;
430 }
431 case Qfalse:
432 {
433 static VALUE freeze_false_hash;
434 if (!freeze_false_hash) {
435 freeze_false_hash = rb_hash_new();
436 rb_gc_register_mark_object(freeze_false_hash);
437 rb_hash_aset(freeze_false_hash, ID2SYM(rb_intern("freeze")), Qfalse);
438 rb_obj_freeze(freeze_false_hash);
439 }
440
441 argv[0] = obj;
442 argv[1] = freeze_false_hash;
443 rb_funcallv_kw(clone, id_init_clone, 2, argv, RB_PASS_KEYWORDS);
444 break;
445 }
446 default:
447 rb_bug("invalid kwfreeze passed to mutable_obj_clone");
448 }
449
450 return clone;
451}
452
456VALUE
458{
459 if (special_object_p(obj)) return obj;
460 return mutable_obj_clone(obj, Qnil);
461}
462
466VALUE
468{
469 VALUE dup;
470
471 if (special_object_p(obj)) {
472 return obj;
473 }
474 dup = rb_obj_alloc(rb_obj_class(obj));
475 init_copy(dup, obj);
476 rb_funcall(dup, id_init_dup, 1, obj);
477
478 return dup;
479}
480
481/*
482 * call-seq:
483 * obj.itself -> obj
484 *
485 * Returns the receiver.
486 *
487 * string = "my string"
488 * string.itself.object_id == string.object_id #=> true
489 *
490 */
491
492static VALUE
493rb_obj_itself(VALUE obj)
494{
495 return obj;
496}
497
498VALUE
499rb_obj_size(VALUE self, VALUE args, VALUE obj)
500{
501 return LONG2FIX(1);
502}
503
504static VALUE
505block_given_p(rb_execution_context_t *ec, VALUE self)
506{
507 return rb_block_given_p() ? Qtrue : Qfalse;
508}
509
515VALUE
517{
518 if (obj == orig) return obj;
519 rb_check_frozen(obj);
520 if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
521 rb_raise(rb_eTypeError, "initialize_copy should take same class object");
522 }
523 return obj;
524}
525
532VALUE
534{
535 rb_funcall(obj, id_init_copy, 1, orig);
536 return obj;
537}
538
546static VALUE
547rb_obj_init_clone(int argc, VALUE *argv, VALUE obj)
548{
549 VALUE orig, opts;
550 rb_scan_args(argc, argv, "1:", &orig, &opts);
551 /* Ignore a freeze keyword */
552 if (argc == 2) (void)freeze_opt(1, &opts);
553 rb_funcall(obj, id_init_copy, 1, orig);
554 return obj;
555}
556
560VALUE
562{
563 VALUE str;
564 VALUE cname = rb_class_name(CLASS_OF(obj));
565
566 str = rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)obj);
567
568 return str;
569}
570
584VALUE
586{
587 VALUE str = rb_obj_as_string(rb_funcallv(obj, id_inspect, 0, 0));
588
590 if (enc == NULL) enc = rb_default_external_encoding();
591 if (!rb_enc_asciicompat(enc)) {
593 return rb_str_escape(str);
594 return str;
595 }
596 if (rb_enc_get(str) != enc && !rb_enc_str_asciionly_p(str))
597 return rb_str_escape(str);
598 return str;
599}
600
601static int
602inspect_i(st_data_t k, st_data_t v, st_data_t a)
603{
604 ID id = (ID)k;
605 VALUE value = (VALUE)v;
606 VALUE str = (VALUE)a;
607
608 /* need not to show internal data */
609 if (CLASS_OF(value) == 0) return ST_CONTINUE;
610 if (!rb_is_instance_id(id)) return ST_CONTINUE;
611 if (RSTRING_PTR(str)[0] == '-') { /* first element */
612 RSTRING_PTR(str)[0] = '#';
613 rb_str_cat2(str, " ");
614 }
615 else {
616 rb_str_cat2(str, ", ");
617 }
619 rb_id2str(id), value);
620
621 return ST_CONTINUE;
622}
623
624static VALUE
625inspect_obj(VALUE obj, VALUE str, int recur)
626{
627 if (recur) {
628 rb_str_cat2(str, " ...");
629 }
630 else {
631 rb_ivar_foreach(obj, inspect_i, str);
632 }
633 rb_str_cat2(str, ">");
634 RSTRING_PTR(str)[0] = '#';
635
636 return str;
637}
638
639/*
640 * call-seq:
641 * obj.inspect -> string
642 *
643 * Returns a string containing a human-readable representation of <i>obj</i>.
644 * The default #inspect shows the object's class name, an encoding of
645 * its memory address, and a list of the instance variables and their
646 * values (by calling #inspect on each of them). User defined classes
647 * should override this method to provide a better representation of
648 * <i>obj</i>. When overriding this method, it should return a string
649 * whose encoding is compatible with the default external encoding.
650 *
651 * [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
652 * Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
653 *
654 * class Foo
655 * end
656 * Foo.new.inspect #=> "#<Foo:0x0300c868>"
657 *
658 * class Bar
659 * def initialize
660 * @bar = 1
661 * end
662 * end
663 * Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
664 */
665
666static VALUE
667rb_obj_inspect(VALUE obj)
668{
669 if (rb_ivar_count(obj) > 0) {
670 VALUE str;
671 VALUE c = rb_class_name(CLASS_OF(obj));
672
673 str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void*)obj);
674 return rb_exec_recursive(inspect_obj, obj, str);
675 }
676 else {
677 return rb_any_to_s(obj);
678 }
679}
680
681static VALUE
682class_or_module_required(VALUE c)
683{
684 switch (OBJ_BUILTIN_TYPE(c)) {
685 case T_MODULE:
686 case T_CLASS:
687 case T_ICLASS:
688 break;
689
690 default:
691 rb_raise(rb_eTypeError, "class or module required");
692 }
693 return c;
694}
695
696static VALUE class_search_ancestor(VALUE cl, VALUE c);
697
706VALUE
708{
709 c = class_or_module_required(c);
710 if (rb_obj_class(obj) == c) return Qtrue;
711 return Qfalse;
712}
713
714
723VALUE
725{
726 VALUE cl = CLASS_OF(obj);
727
728 c = class_or_module_required(c);
729 return class_search_ancestor(cl, RCLASS_ORIGIN(c)) ? Qtrue : Qfalse;
730}
731
732static VALUE
733class_search_ancestor(VALUE cl, VALUE c)
734{
735 while (cl) {
736 if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
737 return cl;
738 cl = RCLASS_SUPER(cl);
739 }
740 return 0;
741}
742
744VALUE
746{
747 cl = class_or_module_required(cl);
748 c = class_or_module_required(c);
749 return class_search_ancestor(cl, RCLASS_ORIGIN(c));
750}
751
752
753/*
754 * Document-method: inherited
755 *
756 * call-seq:
757 * inherited(subclass)
758 *
759 * Callback invoked whenever a subclass of the current class is created.
760 *
761 * Example:
762 *
763 * class Foo
764 * def self.inherited(subclass)
765 * puts "New subclass: #{subclass}"
766 * end
767 * end
768 *
769 * class Bar < Foo
770 * end
771 *
772 * class Baz < Bar
773 * end
774 *
775 * <em>produces:</em>
776 *
777 * New subclass: Bar
778 * New subclass: Baz
779 */
780
781/* Document-method: method_added
782 *
783 * call-seq:
784 * method_added(method_name)
785 *
786 * Invoked as a callback whenever an instance method is added to the
787 * receiver.
788 *
789 * module Chatty
790 * def self.method_added(method_name)
791 * puts "Adding #{method_name.inspect}"
792 * end
793 * def self.some_class_method() end
794 * def some_instance_method() end
795 * end
796 *
797 * <em>produces:</em>
798 *
799 * Adding :some_instance_method
800 *
801 */
802
803/* Document-method: method_removed
804 *
805 * call-seq:
806 * method_removed(method_name)
807 *
808 * Invoked as a callback whenever an instance method is removed from the
809 * receiver.
810 *
811 * module Chatty
812 * def self.method_removed(method_name)
813 * puts "Removing #{method_name.inspect}"
814 * end
815 * def self.some_class_method() end
816 * def some_instance_method() end
817 * class << self
818 * remove_method :some_class_method
819 * end
820 * remove_method :some_instance_method
821 * end
822 *
823 * <em>produces:</em>
824 *
825 * Removing :some_instance_method
826 *
827 */
828
829/*
830 * Document-method: singleton_method_added
831 *
832 * call-seq:
833 * singleton_method_added(symbol)
834 *
835 * Invoked as a callback whenever a singleton method is added to the
836 * receiver.
837 *
838 * module Chatty
839 * def Chatty.singleton_method_added(id)
840 * puts "Adding #{id.id2name}"
841 * end
842 * def self.one() end
843 * def two() end
844 * def Chatty.three() end
845 * end
846 *
847 * <em>produces:</em>
848 *
849 * Adding singleton_method_added
850 * Adding one
851 * Adding three
852 *
853 */
854
855/*
856 * Document-method: singleton_method_removed
857 *
858 * call-seq:
859 * singleton_method_removed(symbol)
860 *
861 * Invoked as a callback whenever a singleton method is removed from
862 * the receiver.
863 *
864 * module Chatty
865 * def Chatty.singleton_method_removed(id)
866 * puts "Removing #{id.id2name}"
867 * end
868 * def self.one() end
869 * def two() end
870 * def Chatty.three() end
871 * class << self
872 * remove_method :three
873 * remove_method :one
874 * end
875 * end
876 *
877 * <em>produces:</em>
878 *
879 * Removing three
880 * Removing one
881 */
882
883/*
884 * Document-method: singleton_method_undefined
885 *
886 * call-seq:
887 * singleton_method_undefined(symbol)
888 *
889 * Invoked as a callback whenever a singleton method is undefined in
890 * the receiver.
891 *
892 * module Chatty
893 * def Chatty.singleton_method_undefined(id)
894 * puts "Undefining #{id.id2name}"
895 * end
896 * def Chatty.one() end
897 * class << self
898 * undef_method(:one)
899 * end
900 * end
901 *
902 * <em>produces:</em>
903 *
904 * Undefining one
905 */
906
907/*
908 * Document-method: extended
909 *
910 * call-seq:
911 * extended(othermod)
912 *
913 * The equivalent of <tt>included</tt>, but for extended modules.
914 *
915 * module A
916 * def self.extended(mod)
917 * puts "#{self} extended in #{mod}"
918 * end
919 * end
920 * module Enumerable
921 * extend A
922 * end
923 * # => prints "A extended in Enumerable"
924 */
925
926/*
927 * Document-method: included
928 *
929 * call-seq:
930 * included(othermod)
931 *
932 * Callback invoked whenever the receiver is included in another
933 * module or class. This should be used in preference to
934 * <tt>Module.append_features</tt> if your code wants to perform some
935 * action when a module is included in another.
936 *
937 * module A
938 * def A.included(mod)
939 * puts "#{self} included in #{mod}"
940 * end
941 * end
942 * module Enumerable
943 * include A
944 * end
945 * # => prints "A included in Enumerable"
946 */
947
948/*
949 * Document-method: prepended
950 *
951 * call-seq:
952 * prepended(othermod)
953 *
954 * The equivalent of <tt>included</tt>, but for prepended modules.
955 *
956 * module A
957 * def self.prepended(mod)
958 * puts "#{self} prepended to #{mod}"
959 * end
960 * end
961 * module Enumerable
962 * prepend A
963 * end
964 * # => prints "A prepended to Enumerable"
965 */
966
967/*
968 * Document-method: initialize
969 *
970 * call-seq:
971 * BasicObject.new
972 *
973 * Returns a new BasicObject.
974 */
975
976/*
977 * Not documented
978 */
979
980static VALUE
981rb_obj_dummy(void)
982{
983 return Qnil;
984}
985
986static VALUE
987rb_obj_dummy0(VALUE _)
988{
989 return rb_obj_dummy();
990}
991
992static VALUE
993rb_obj_dummy1(VALUE _x, VALUE _y)
994{
995 return rb_obj_dummy();
996}
997
1005VALUE
1007{
1008 rb_warn_deprecated_to_remove("Object#tainted?", "3.2");
1009 return Qfalse;
1010}
1011
1019VALUE
1021{
1022 rb_warn_deprecated_to_remove("Object#taint", "3.2");
1023 return obj;
1024}
1025
1026
1034VALUE
1036{
1037 rb_warn_deprecated_to_remove("Object#untaint", "3.2");
1038 return obj;
1039}
1040
1048VALUE
1050{
1051 rb_warn_deprecated_to_remove("Object#untrusted?", "3.2");
1052 return Qfalse;
1053}
1054
1062VALUE
1064{
1065 rb_warn_deprecated_to_remove("Object#untrust", "3.2");
1066 return obj;
1067}
1068
1069
1077VALUE
1079{
1080 rb_warn_deprecated_to_remove("Object#trust", "3.2");
1081 return obj;
1082}
1083
1088void
1089rb_obj_infect(VALUE victim, VALUE carrier)
1090{
1091 rb_warn_deprecated_to_remove("rb_obj_infect", "3.2");
1092}
1093
1100VALUE
1102{
1103 if (!OBJ_FROZEN(obj)) {
1104 OBJ_FREEZE(obj);
1105 if (SPECIAL_CONST_P(obj)) {
1106 rb_bug("special consts should be frozen.");
1107 }
1108 }
1109 return obj;
1110}
1111
1112VALUE
1114{
1115 return OBJ_FROZEN(obj) ? Qtrue : Qfalse;
1116}
1117
1118
1119/*
1120 * Document-class: NilClass
1121 *
1122 * The class of the singleton object <code>nil</code>.
1123 */
1124
1125/*
1126 * call-seq:
1127 * nil.to_i -> 0
1128 *
1129 * Always returns zero.
1130 *
1131 * nil.to_i #=> 0
1132 */
1133
1134
1135static VALUE
1136nil_to_i(VALUE obj)
1137{
1138 return INT2FIX(0);
1139}
1140
1141/*
1142 * call-seq:
1143 * nil.to_f -> 0.0
1144 *
1145 * Always returns zero.
1146 *
1147 * nil.to_f #=> 0.0
1148 */
1149
1150static VALUE
1151nil_to_f(VALUE obj)
1152{
1153 return DBL2NUM(0.0);
1154}
1155
1156/*
1157 * call-seq:
1158 * nil.to_s -> ""
1159 *
1160 * Always returns the empty string.
1161 */
1162
1163static VALUE
1164nil_to_s(VALUE obj)
1165{
1166 return rb_cNilClass_to_s;
1167}
1168
1169/*
1170 * Document-method: to_a
1171 *
1172 * call-seq:
1173 * nil.to_a -> []
1174 *
1175 * Always returns an empty array.
1176 *
1177 * nil.to_a #=> []
1178 */
1179
1180static VALUE
1181nil_to_a(VALUE obj)
1182{
1183 return rb_ary_new2(0);
1184}
1185
1186/*
1187 * Document-method: to_h
1188 *
1189 * call-seq:
1190 * nil.to_h -> {}
1191 *
1192 * Always returns an empty hash.
1193 *
1194 * nil.to_h #=> {}
1195 */
1196
1197static VALUE
1198nil_to_h(VALUE obj)
1199{
1200 return rb_hash_new();
1201}
1202
1203/*
1204 * call-seq:
1205 * nil.inspect -> "nil"
1206 *
1207 * Always returns the string "nil".
1208 */
1209
1210static VALUE
1211nil_inspect(VALUE obj)
1212{
1213 return rb_usascii_str_new2("nil");
1214}
1215
1216/*
1217 * call-seq:
1218 * nil =~ other -> nil
1219 *
1220 * Dummy pattern matching -- always returns nil.
1221 */
1222
1223static VALUE
1224nil_match(VALUE obj1, VALUE obj2)
1225{
1226 return Qnil;
1227}
1228
1229/***********************************************************************
1230 * Document-class: TrueClass
1231 *
1232 * The global value <code>true</code> is the only instance of class
1233 * TrueClass and represents a logically true value in
1234 * boolean expressions. The class provides operators allowing
1235 * <code>true</code> to be used in logical expressions.
1236 */
1237
1238
1239/*
1240 * call-seq:
1241 * true.to_s -> "true"
1242 *
1243 * The string representation of <code>true</code> is "true".
1244 */
1245
1246static VALUE
1247true_to_s(VALUE obj)
1248{
1249 return rb_cTrueClass_to_s;
1250}
1251
1252
1253/*
1254 * call-seq:
1255 * true & obj -> true or false
1256 *
1257 * And---Returns <code>false</code> if <i>obj</i> is
1258 * <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
1259 */
1260
1261static VALUE
1262true_and(VALUE obj, VALUE obj2)
1263{
1264 return RTEST(obj2)?Qtrue:Qfalse;
1265}
1266
1267/*
1268 * call-seq:
1269 * true | obj -> true
1270 *
1271 * Or---Returns <code>true</code>. As <i>obj</i> is an argument to
1272 * a method call, it is always evaluated; there is no short-circuit
1273 * evaluation in this case.
1274 *
1275 * true | puts("or")
1276 * true || puts("logical or")
1277 *
1278 * <em>produces:</em>
1279 *
1280 * or
1281 */
1282
1283static VALUE
1284true_or(VALUE obj, VALUE obj2)
1285{
1286 return Qtrue;
1287}
1288
1289
1290/*
1291 * call-seq:
1292 * true ^ obj -> !obj
1293 *
1294 * Exclusive Or---Returns <code>true</code> if <i>obj</i> is
1295 * <code>nil</code> or <code>false</code>, <code>false</code>
1296 * otherwise.
1297 */
1298
1299static VALUE
1300true_xor(VALUE obj, VALUE obj2)
1301{
1302 return RTEST(obj2)?Qfalse:Qtrue;
1303}
1304
1305
1306/*
1307 * Document-class: FalseClass
1308 *
1309 * The global value <code>false</code> is the only instance of class
1310 * FalseClass and represents a logically false value in
1311 * boolean expressions. The class provides operators allowing
1312 * <code>false</code> to participate correctly in logical expressions.
1313 *
1314 */
1315
1316/*
1317 * call-seq:
1318 * false.to_s -> "false"
1319 *
1320 * The string representation of <code>false</code> is "false".
1321 */
1322
1323static VALUE
1324false_to_s(VALUE obj)
1325{
1326 return rb_cFalseClass_to_s;
1327}
1328
1329/*
1330 * call-seq:
1331 * false & obj -> false
1332 * nil & obj -> false
1333 *
1334 * And---Returns <code>false</code>. <i>obj</i> is always
1335 * evaluated as it is the argument to a method call---there is no
1336 * short-circuit evaluation in this case.
1337 */
1338
1339static VALUE
1340false_and(VALUE obj, VALUE obj2)
1341{
1342 return Qfalse;
1343}
1344
1345
1346/*
1347 * call-seq:
1348 * false | obj -> true or false
1349 * nil | obj -> true or false
1350 *
1351 * Or---Returns <code>false</code> if <i>obj</i> is
1352 * <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
1353 */
1354
1355#define false_or true_and
1356
1357/*
1358 * call-seq:
1359 * false ^ obj -> true or false
1360 * nil ^ obj -> true or false
1361 *
1362 * Exclusive Or---If <i>obj</i> is <code>nil</code> or
1363 * <code>false</code>, returns <code>false</code>; otherwise, returns
1364 * <code>true</code>.
1365 *
1366 */
1367
1368#define false_xor true_and
1369
1370/*
1371 * call-seq:
1372 * nil.nil? -> true
1373 *
1374 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1375 */
1376
1377static VALUE
1378rb_true(VALUE obj)
1379{
1380 return Qtrue;
1381}
1382
1383/*
1384 * call-seq:
1385 * obj.nil? -> true or false
1386 *
1387 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1388 *
1389 * Object.new.nil? #=> false
1390 * nil.nil? #=> true
1391 */
1392
1393
1396{
1397 return Qfalse;
1398}
1399
1400
1401/*
1402 * call-seq:
1403 * obj =~ other -> nil
1404 *
1405 * This method is deprecated.
1406 *
1407 * This is not only useless but also troublesome because it may hide a
1408 * type error.
1409 */
1410
1411static VALUE
1412rb_obj_match(VALUE obj1, VALUE obj2)
1413{
1415 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "deprecated Object#=~ is called on %"PRIsVALUE
1416 "; it always returns nil", rb_obj_class(obj1));
1417 }
1418 return Qnil;
1419}
1420
1421/*
1422 * call-seq:
1423 * obj !~ other -> true or false
1424 *
1425 * Returns true if two objects do not match (using the <i>=~</i>
1426 * method), otherwise false.
1427 */
1428
1429static VALUE
1430rb_obj_not_match(VALUE obj1, VALUE obj2)
1431{
1432 VALUE result = rb_funcall(obj1, id_match, 1, obj2);
1433 return RTEST(result) ? Qfalse : Qtrue;
1434}
1435
1436
1437/*
1438 * call-seq:
1439 * obj <=> other -> 0 or nil
1440 *
1441 * Returns 0 if +obj+ and +other+ are the same object
1442 * or <code>obj == other</code>, otherwise nil.
1443 *
1444 * The #<=> is used by various methods to compare objects, for example
1445 * Enumerable#sort, Enumerable#max etc.
1446 *
1447 * Your implementation of #<=> should return one of the following values: -1, 0,
1448 * 1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
1449 * 1 means self is bigger than other. Nil means the two values could not be
1450 * compared.
1451 *
1452 * When you define #<=>, you can include Comparable to gain the
1453 * methods #<=, #<, #==, #>=, #> and #between?.
1454 */
1455static VALUE
1456rb_obj_cmp(VALUE obj1, VALUE obj2)
1457{
1458 if (rb_equal(obj1, obj2))
1459 return INT2FIX(0);
1460 return Qnil;
1461}
1462
1463/***********************************************************************
1464 *
1465 * Document-class: Module
1466 *
1467 * A Module is a collection of methods and constants. The
1468 * methods in a module may be instance methods or module methods.
1469 * Instance methods appear as methods in a class when the module is
1470 * included, module methods do not. Conversely, module methods may be
1471 * called without creating an encapsulating object, while instance
1472 * methods may not. (See Module#module_function.)
1473 *
1474 * In the descriptions that follow, the parameter <i>sym</i> refers
1475 * to a symbol, which is either a quoted string or a
1476 * Symbol (such as <code>:name</code>).
1477 *
1478 * module Mod
1479 * include Math
1480 * CONST = 1
1481 * def meth
1482 * # ...
1483 * end
1484 * end
1485 * Mod.class #=> Module
1486 * Mod.constants #=> [:CONST, :PI, :E]
1487 * Mod.instance_methods #=> [:meth]
1488 *
1489 */
1490
1491/*
1492 * call-seq:
1493 * mod.to_s -> string
1494 *
1495 * Returns a string representing this module or class. For basic
1496 * classes and modules, this is the name. For singletons, we
1497 * show information on the thing we're attached to as well.
1498 */
1499
1500static VALUE
1501rb_mod_to_s(VALUE klass)
1502{
1503 ID id_defined_at;
1504 VALUE refined_class, defined_at;
1505
1506 if (FL_TEST(klass, FL_SINGLETON)) {
1507 VALUE s = rb_usascii_str_new2("#<Class:");
1508 VALUE v = rb_ivar_get(klass, id__attached__);
1509
1510 if (CLASS_OR_MODULE_P(v)) {
1512 }
1513 else {
1515 }
1516 rb_str_cat2(s, ">");
1517
1518 return s;
1519 }
1520 refined_class = rb_refinement_module_get_refined_class(klass);
1521 if (!NIL_P(refined_class)) {
1522 VALUE s = rb_usascii_str_new2("#<refinement:");
1523
1524 rb_str_concat(s, rb_inspect(refined_class));
1525 rb_str_cat2(s, "@");
1526 CONST_ID(id_defined_at, "__defined_at__");
1527 defined_at = rb_attr_get(klass, id_defined_at);
1528 rb_str_concat(s, rb_inspect(defined_at));
1529 rb_str_cat2(s, ">");
1530 return s;
1531 }
1532 return rb_class_name(klass);
1533}
1534
1535/*
1536 * call-seq:
1537 * mod.freeze -> mod
1538 *
1539 * Prevents further modifications to <i>mod</i>.
1540 *
1541 * This method returns self.
1542 */
1543
1544static VALUE
1545rb_mod_freeze(VALUE mod)
1546{
1548 return rb_obj_freeze(mod);
1549}
1550
1551/*
1552 * call-seq:
1553 * mod === obj -> true or false
1554 *
1555 * Case Equality---Returns <code>true</code> if <i>obj</i> is an
1556 * instance of <i>mod</i> or an instance of one of <i>mod</i>'s descendants.
1557 * Of limited use for modules, but can be used in <code>case</code> statements
1558 * to classify objects by class.
1559 */
1560
1561static VALUE
1562rb_mod_eqq(VALUE mod, VALUE arg)
1563{
1564 return rb_obj_is_kind_of(arg, mod);
1565}
1566
1577VALUE
1579{
1580 if (mod == arg) return Qtrue;
1581 if (!CLASS_OR_MODULE_P(arg) && !RB_TYPE_P(arg, T_ICLASS)) {
1582 rb_raise(rb_eTypeError, "compared with non class/module");
1583 }
1584 if (class_search_ancestor(mod, RCLASS_ORIGIN(arg))) {
1585 return Qtrue;
1586 }
1587 /* not mod < arg; check if mod > arg */
1588 if (class_search_ancestor(arg, mod)) {
1589 return Qfalse;
1590 }
1591 return Qnil;
1592}
1593
1594/*
1595 * call-seq:
1596 * mod < other -> true, false, or nil
1597 *
1598 * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
1599 * <code>nil</code> if there's no relationship between the two.
1600 * (Think of the relationship in terms of the class definition:
1601 * "class A < B" implies "A < B".)
1602 *
1603 */
1604
1605static VALUE
1606rb_mod_lt(VALUE mod, VALUE arg)
1607{
1608 if (mod == arg) return Qfalse;
1609 return rb_class_inherited_p(mod, arg);
1610}
1611
1612
1613/*
1614 * call-seq:
1615 * mod >= other -> true, false, or nil
1616 *
1617 * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
1618 * two modules are the same. Returns
1619 * <code>nil</code> if there's no relationship between the two.
1620 * (Think of the relationship in terms of the class definition:
1621 * "class A < B" implies "B > A".)
1622 *
1623 */
1624
1625static VALUE
1626rb_mod_ge(VALUE mod, VALUE arg)
1627{
1628 if (!CLASS_OR_MODULE_P(arg)) {
1629 rb_raise(rb_eTypeError, "compared with non class/module");
1630 }
1631
1632 return rb_class_inherited_p(arg, mod);
1633}
1634
1635/*
1636 * call-seq:
1637 * mod > other -> true, false, or nil
1638 *
1639 * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
1640 * <code>nil</code> if there's no relationship between the two.
1641 * (Think of the relationship in terms of the class definition:
1642 * "class A < B" implies "B > A".)
1643 *
1644 */
1645
1646static VALUE
1647rb_mod_gt(VALUE mod, VALUE arg)
1648{
1649 if (mod == arg) return Qfalse;
1650 return rb_mod_ge(mod, arg);
1651}
1652
1653/*
1654 * call-seq:
1655 * module <=> other_module -> -1, 0, +1, or nil
1656 *
1657 * Comparison---Returns -1, 0, +1 or nil depending on whether +module+
1658 * includes +other_module+, they are the same, or if +module+ is included by
1659 * +other_module+.
1660 *
1661 * Returns +nil+ if +module+ has no relationship with +other_module+, if
1662 * +other_module+ is not a module, or if the two values are incomparable.
1663 */
1664
1665static VALUE
1666rb_mod_cmp(VALUE mod, VALUE arg)
1667{
1668 VALUE cmp;
1669
1670 if (mod == arg) return INT2FIX(0);
1671 if (!CLASS_OR_MODULE_P(arg)) {
1672 return Qnil;
1673 }
1674
1675 cmp = rb_class_inherited_p(mod, arg);
1676 if (NIL_P(cmp)) return Qnil;
1677 if (cmp) {
1678 return INT2FIX(-1);
1679 }
1680 return INT2FIX(1);
1681}
1682
1683static VALUE
1684rb_module_s_alloc(VALUE klass)
1685{
1687
1688 RBASIC_SET_CLASS(mod, klass);
1689 return mod;
1690}
1691
1692static VALUE
1693rb_class_s_alloc(VALUE klass)
1694{
1695 return rb_class_boot(0);
1696}
1697
1698/*
1699 * call-seq:
1700 * Module.new -> mod
1701 * Module.new {|mod| block } -> mod
1702 *
1703 * Creates a new anonymous module. If a block is given, it is passed
1704 * the module object, and the block is evaluated in the context of this
1705 * module like #module_eval.
1706 *
1707 * fred = Module.new do
1708 * def meth1
1709 * "hello"
1710 * end
1711 * def meth2
1712 * "bye"
1713 * end
1714 * end
1715 * a = "my string"
1716 * a.extend(fred) #=> "my string"
1717 * a.meth1 #=> "hello"
1718 * a.meth2 #=> "bye"
1719 *
1720 * Assign the module to a constant (name starting uppercase) if you
1721 * want to treat it like a regular module.
1722 */
1723
1724static VALUE
1725rb_mod_initialize(VALUE module)
1726{
1727 if (rb_block_given_p()) {
1728 rb_mod_module_exec(1, &module, module);
1729 }
1730 return Qnil;
1731}
1732
1733/* :nodoc: */
1734static VALUE
1735rb_mod_initialize_clone(int argc, VALUE* argv, VALUE clone)
1736{
1737 VALUE ret, orig, opts;
1738 rb_scan_args(argc, argv, "1:", &orig, &opts);
1739 ret = rb_obj_init_clone(argc, argv, clone);
1740 if (OBJ_FROZEN(orig))
1741 rb_class_name(clone);
1742 return ret;
1743}
1744
1745/*
1746 * call-seq:
1747 * Class.new(super_class=Object) -> a_class
1748 * Class.new(super_class=Object) { |mod| ... } -> a_class
1749 *
1750 * Creates a new anonymous (unnamed) class with the given superclass
1751 * (or Object if no parameter is given). You can give a
1752 * class a name by assigning the class object to a constant.
1753 *
1754 * If a block is given, it is passed the class object, and the block
1755 * is evaluated in the context of this class like
1756 * #class_eval.
1757 *
1758 * fred = Class.new do
1759 * def meth1
1760 * "hello"
1761 * end
1762 * def meth2
1763 * "bye"
1764 * end
1765 * end
1766 *
1767 * a = fred.new #=> #<#<Class:0x100381890>:0x100376b98>
1768 * a.meth1 #=> "hello"
1769 * a.meth2 #=> "bye"
1770 *
1771 * Assign the class to a constant (name starting uppercase) if you
1772 * want to treat it like a regular class.
1773 */
1774
1775static VALUE
1776rb_class_initialize(int argc, VALUE *argv, VALUE klass)
1777{
1778 VALUE super;
1779
1780 if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
1781 rb_raise(rb_eTypeError, "already initialized class");
1782 }
1783 if (rb_check_arity(argc, 0, 1) == 0) {
1784 super = rb_cObject;
1785 }
1786 else {
1787 super = argv[0];
1788 rb_check_inheritable(super);
1789 if (super != rb_cBasicObject && !RCLASS_SUPER(super)) {
1790 rb_raise(rb_eTypeError, "can't inherit uninitialized class");
1791 }
1792 }
1793 RCLASS_SET_SUPER(klass, super);
1794 rb_make_metaclass(klass, RBASIC(super)->klass);
1795 rb_class_inherited(super, klass);
1796 rb_mod_initialize(klass);
1797
1798 return klass;
1799}
1800
1802void
1804{
1805 rb_raise(rb_eTypeError, "allocator undefined for %"PRIsVALUE,
1806 klass);
1807}
1808
1809static rb_alloc_func_t class_get_alloc_func(VALUE klass);
1810static VALUE class_call_alloc_func(rb_alloc_func_t allocator, VALUE klass);
1811
1812/*
1813 * call-seq:
1814 * class.allocate() -> obj
1815 *
1816 * Allocates space for a new object of <i>class</i>'s class and does not
1817 * call initialize on the new instance. The returned object must be an
1818 * instance of <i>class</i>.
1819 *
1820 * klass = Class.new do
1821 * def initialize(*args)
1822 * @initialized = true
1823 * end
1824 *
1825 * def initialized?
1826 * @initialized || false
1827 * end
1828 * end
1829 *
1830 * klass.allocate.initialized? #=> false
1831 *
1832 */
1833
1834static VALUE
1835rb_class_alloc_m(VALUE klass)
1836{
1837 rb_alloc_func_t allocator = class_get_alloc_func(klass);
1838 if (!rb_obj_respond_to(klass, rb_intern("allocate"), 1)) {
1839 rb_raise(rb_eTypeError, "calling %"PRIsVALUE".allocate is prohibited",
1840 klass);
1841 }
1842 return class_call_alloc_func(allocator, klass);
1843}
1844
1845static VALUE
1846rb_class_alloc(VALUE klass)
1847{
1848 rb_alloc_func_t allocator = class_get_alloc_func(klass);
1849 return class_call_alloc_func(allocator, klass);
1850}
1851
1852static rb_alloc_func_t
1853class_get_alloc_func(VALUE klass)
1854{
1855 rb_alloc_func_t allocator;
1856
1857 if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
1858 rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
1859 }
1860 if (FL_TEST(klass, FL_SINGLETON)) {
1861 rb_raise(rb_eTypeError, "can't create instance of singleton class");
1862 }
1863 allocator = rb_get_alloc_func(klass);
1864 if (!allocator) {
1865 rb_undefined_alloc(klass);
1866 }
1867 return allocator;
1868}
1869
1870static VALUE
1871class_call_alloc_func(rb_alloc_func_t allocator, VALUE klass)
1872{
1873 VALUE obj;
1874
1875 RUBY_DTRACE_CREATE_HOOK(OBJECT, rb_class2name(klass));
1876
1877 obj = (*allocator)(klass);
1878
1879 if (rb_obj_class(obj) != rb_class_real(klass)) {
1880 rb_raise(rb_eTypeError, "wrong instance allocation");
1881 }
1882 return obj;
1883}
1884
1899VALUE
1901{
1902 Check_Type(klass, T_CLASS);
1903 return rb_class_alloc(klass);
1904}
1905
1906/*
1907 * call-seq:
1908 * class.new(args, ...) -> obj
1909 *
1910 * Calls #allocate to create a new object of <i>class</i>'s class,
1911 * then invokes that object's #initialize method, passing it
1912 * <i>args</i>. This is the method that ends up getting called
1913 * whenever an object is constructed using <code>.new</code>.
1914 *
1915 */
1916
1917VALUE
1919{
1920 VALUE obj;
1921
1922 obj = rb_class_alloc(klass);
1924
1925 return obj;
1926}
1927
1928VALUE
1929rb_class_new_instance_kw(int argc, const VALUE *argv, VALUE klass, int kw_splat)
1930{
1931 VALUE obj;
1932 Check_Type(klass, T_CLASS);
1933
1934 obj = rb_class_alloc(klass);
1935 rb_obj_call_init_kw(obj, argc, argv, kw_splat);
1936
1937 return obj;
1938}
1939
1952VALUE
1954{
1955 VALUE obj;
1956 Check_Type(klass, T_CLASS);
1957
1958 obj = rb_class_alloc(klass);
1960
1961 return obj;
1962}
1963
1973VALUE
1975{
1976 VALUE super = RCLASS_SUPER(klass);
1977
1978 if (!super) {
1979 if (klass == rb_cBasicObject) return Qnil;
1980 rb_raise(rb_eTypeError, "uninitialized class");
1981 }
1982 while (RB_TYPE_P(super, T_ICLASS)) {
1983 super = RCLASS_SUPER(super);
1984 }
1985 if (!super) {
1986 return Qnil;
1987 }
1988 return super;
1989}
1990
1998VALUE
2000{
2001 return RCLASS(klass)->super;
2002}
2003
2004static const char bad_instance_name[] = "`%1$s' is not allowed as an instance variable name";
2005static const char bad_class_name[] = "`%1$s' is not allowed as a class variable name";
2006static const char bad_const_name[] = "wrong constant name %1$s";
2007static const char bad_attr_name[] = "invalid attribute name `%1$s'";
2008#define wrong_constant_name bad_const_name
2009
2011#define id_for_var(obj, name, type) id_for_setter(obj, name, type, bad_##type##_name)
2013#define id_for_setter(obj, name, type, message) \
2014 check_setter_id(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2015static ID
2016check_setter_id(VALUE obj, VALUE *pname,
2017 int (*valid_id_p)(ID), int (*valid_name_p)(VALUE),
2018 const char *message, size_t message_len)
2019{
2020 ID id = rb_check_id(pname);
2021 VALUE name = *pname;
2022
2023 if (id ? !valid_id_p(id) : !valid_name_p(name)) {
2024 rb_name_err_raise_str(rb_fstring_new(message, message_len),
2025 obj, name);
2026 }
2027 return id;
2028}
2029
2030static int
2031rb_is_attr_name(VALUE name)
2032{
2034}
2035
2036static int
2037rb_is_attr_id(ID id)
2038{
2039 return rb_is_local_id(id) || rb_is_const_id(id);
2040}
2041
2042static ID
2043id_for_attr(VALUE obj, VALUE name)
2044{
2045 ID id = id_for_var(obj, name, attr);
2046 if (!id) id = rb_intern_str(name);
2047 return id;
2048}
2049
2050/*
2051 * call-seq:
2052 * attr_reader(symbol, ...) -> array
2053 * attr(symbol, ...) -> array
2054 * attr_reader(string, ...) -> array
2055 * attr(string, ...) -> array
2056 *
2057 * Creates instance variables and corresponding methods that return the
2058 * value of each instance variable. Equivalent to calling
2059 * ``<code>attr</code><i>:name</i>'' on each name in turn.
2060 * String arguments are converted to symbols.
2061 * Returns an array of defined method names as symbols.
2062 */
2063
2064static VALUE
2065rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
2066{
2067 int i;
2068 VALUE names = rb_ary_new2(argc);
2069
2070 for (i=0; i<argc; i++) {
2071 ID id = id_for_attr(klass, argv[i]);
2072 rb_attr(klass, id, TRUE, FALSE, TRUE);
2073 rb_ary_push(names, ID2SYM(id));
2074 }
2075 return names;
2076}
2077
2082VALUE
2083rb_mod_attr(int argc, VALUE *argv, VALUE klass)
2084{
2085 if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
2086 ID id = id_for_attr(klass, argv[0]);
2087 VALUE names = rb_ary_new();
2088
2089 rb_category_warning(RB_WARN_CATEGORY_DEPRECATED, "optional boolean argument is obsoleted");
2090 rb_attr(klass, id, 1, RTEST(argv[1]), TRUE);
2091 rb_ary_push(names, ID2SYM(id));
2092 if (argv[1] == Qtrue) rb_ary_push(names, ID2SYM(rb_id_attrset(id)));
2093 return names;
2094 }
2095 return rb_mod_attr_reader(argc, argv, klass);
2096}
2097
2098/*
2099 * call-seq:
2100 * attr_writer(symbol, ...) -> array
2101 * attr_writer(string, ...) -> array
2102 *
2103 * Creates an accessor method to allow assignment to the attribute
2104 * <i>symbol</i><code>.id2name</code>.
2105 * String arguments are converted to symbols.
2106 * Returns an array of defined method names as symbols.
2107 */
2108
2109static VALUE
2110rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
2111{
2112 int i;
2113 VALUE names = rb_ary_new2(argc);
2114
2115 for (i=0; i<argc; i++) {
2116 ID id = id_for_attr(klass, argv[i]);
2117 rb_attr(klass, id, FALSE, TRUE, TRUE);
2118 rb_ary_push(names, ID2SYM(rb_id_attrset(id)));
2119 }
2120 return names;
2121}
2122
2123/*
2124 * call-seq:
2125 * attr_accessor(symbol, ...) -> array
2126 * attr_accessor(string, ...) -> array
2127 *
2128 * Defines a named attribute for this module, where the name is
2129 * <i>symbol.</i><code>id2name</code>, creating an instance variable
2130 * (<code>@name</code>) and a corresponding access method to read it.
2131 * Also creates a method called <code>name=</code> to set the attribute.
2132 * String arguments are converted to symbols.
2133 * Returns an array of defined method names as symbols.
2134 *
2135 * module Mod
2136 * attr_accessor(:one, :two) #=> [:one, :one=, :two, :two=]
2137 * end
2138 * Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
2139 */
2140
2141static VALUE
2142rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
2143{
2144 int i;
2145 VALUE names = rb_ary_new2(argc * 2);
2146
2147 for (i=0; i<argc; i++) {
2148 ID id = id_for_attr(klass, argv[i]);
2149
2150 rb_attr(klass, id, TRUE, TRUE, TRUE);
2151 rb_ary_push(names, ID2SYM(id));
2152 rb_ary_push(names, ID2SYM(rb_id_attrset(id)));
2153 }
2154 return names;
2155}
2156
2157/*
2158 * call-seq:
2159 * mod.const_get(sym, inherit=true) -> obj
2160 * mod.const_get(str, inherit=true) -> obj
2161 *
2162 * Checks for a constant with the given name in <i>mod</i>.
2163 * If +inherit+ is set, the lookup will also search
2164 * the ancestors (and +Object+ if <i>mod</i> is a +Module+).
2165 *
2166 * The value of the constant is returned if a definition is found,
2167 * otherwise a +NameError+ is raised.
2168 *
2169 * Math.const_get(:PI) #=> 3.14159265358979
2170 *
2171 * This method will recursively look up constant names if a namespaced
2172 * class name is provided. For example:
2173 *
2174 * module Foo; class Bar; end end
2175 * Object.const_get 'Foo::Bar'
2176 *
2177 * The +inherit+ flag is respected on each lookup. For example:
2178 *
2179 * module Foo
2180 * class Bar
2181 * VAL = 10
2182 * end
2183 *
2184 * class Baz < Bar; end
2185 * end
2186 *
2187 * Object.const_get 'Foo::Baz::VAL' # => 10
2188 * Object.const_get 'Foo::Baz::VAL', false # => NameError
2189 *
2190 * If the argument is not a valid constant name a +NameError+ will be
2191 * raised with a warning "wrong constant name".
2192 *
2193 * Object.const_get 'foobar' #=> NameError: wrong constant name foobar
2194 *
2195 */
2196
2197static VALUE
2198rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
2199{
2200 VALUE name, recur;
2201 rb_encoding *enc;
2202 const char *pbeg, *p, *path, *pend;
2203 ID id;
2204
2205 rb_check_arity(argc, 1, 2);
2206 name = argv[0];
2207 recur = (argc == 1) ? Qtrue : argv[1];
2208
2209 if (SYMBOL_P(name)) {
2210 if (!rb_is_const_sym(name)) goto wrong_name;
2211 id = rb_check_id(&name);
2212 if (!id) return rb_const_missing(mod, name);
2213 return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
2214 }
2215
2216 path = StringValuePtr(name);
2217 enc = rb_enc_get(name);
2218
2219 if (!rb_enc_asciicompat(enc)) {
2220 rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
2221 }
2222
2223 pbeg = p = path;
2224 pend = path + RSTRING_LEN(name);
2225
2226 if (p >= pend || !*p) {
2227 goto wrong_name;
2228 }
2229
2230 if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
2231 mod = rb_cObject;
2232 p += 2;
2233 pbeg = p;
2234 }
2235
2236 while (p < pend) {
2237 VALUE part;
2238 long len, beglen;
2239
2240 while (p < pend && *p != ':') p++;
2241
2242 if (pbeg == p) goto wrong_name;
2243
2244 id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
2245 beglen = pbeg-path;
2246
2247 if (p < pend && p[0] == ':') {
2248 if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2249 p += 2;
2250 pbeg = p;
2251 }
2252
2253 if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2254 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2255 QUOTE(name));
2256 }
2257
2258 if (!id) {
2259 part = rb_str_subseq(name, beglen, len);
2260 OBJ_FREEZE(part);
2261 if (!rb_is_const_name(part)) {
2262 name = part;
2263 goto wrong_name;
2264 }
2265 else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
2266 part = rb_str_intern(part);
2267 mod = rb_const_missing(mod, part);
2268 continue;
2269 }
2270 else {
2272 }
2273 }
2274 if (!rb_is_const_id(id)) {
2275 name = ID2SYM(id);
2276 goto wrong_name;
2277 }
2278#if 0
2279 mod = rb_const_get_0(mod, id, beglen > 0 || !RTEST(recur), RTEST(recur), FALSE);
2280#else
2281 if (!RTEST(recur)) {
2282 mod = rb_const_get_at(mod, id);
2283 }
2284 else if (beglen == 0) {
2285 mod = rb_const_get(mod, id);
2286 }
2287 else {
2288 mod = rb_const_get_from(mod, id);
2289 }
2290#endif
2291 }
2292
2293 return mod;
2294
2295 wrong_name:
2296 rb_name_err_raise(wrong_constant_name, mod, name);
2298}
2299
2300/*
2301 * call-seq:
2302 * mod.const_set(sym, obj) -> obj
2303 * mod.const_set(str, obj) -> obj
2304 *
2305 * Sets the named constant to the given object, returning that object.
2306 * Creates a new constant if no constant with the given name previously
2307 * existed.
2308 *
2309 * Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
2310 * Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
2311 *
2312 * If +sym+ or +str+ is not a valid constant name a +NameError+ will be
2313 * raised with a warning "wrong constant name".
2314 *
2315 * Object.const_set('foobar', 42) #=> NameError: wrong constant name foobar
2316 *
2317 */
2318
2319static VALUE
2320rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
2321{
2322 ID id = id_for_var(mod, name, const);
2323 if (!id) id = rb_intern_str(name);
2324 rb_const_set(mod, id, value);
2325
2326 return value;
2327}
2328
2329/*
2330 * call-seq:
2331 * mod.const_defined?(sym, inherit=true) -> true or false
2332 * mod.const_defined?(str, inherit=true) -> true or false
2333 *
2334 * Says whether _mod_ or its ancestors have a constant with the given name:
2335 *
2336 * Float.const_defined?(:EPSILON) #=> true, found in Float itself
2337 * Float.const_defined?("String") #=> true, found in Object (ancestor)
2338 * BasicObject.const_defined?(:Hash) #=> false
2339 *
2340 * If _mod_ is a +Module+, additionally +Object+ and its ancestors are checked:
2341 *
2342 * Math.const_defined?(:String) #=> true, found in Object
2343 *
2344 * In each of the checked classes or modules, if the constant is not present
2345 * but there is an autoload for it, +true+ is returned directly without
2346 * autoloading:
2347 *
2348 * module Admin
2349 * autoload :User, 'admin/user'
2350 * end
2351 * Admin.const_defined?(:User) #=> true
2352 *
2353 * If the constant is not found the callback +const_missing+ is *not* called
2354 * and the method returns +false+.
2355 *
2356 * If +inherit+ is false, the lookup only checks the constants in the receiver:
2357 *
2358 * IO.const_defined?(:SYNC) #=> true, found in File::Constants (ancestor)
2359 * IO.const_defined?(:SYNC, false) #=> false, not found in IO itself
2360 *
2361 * In this case, the same logic for autoloading applies.
2362 *
2363 * If the argument is not a valid constant name a +NameError+ is raised with the
2364 * message "wrong constant name _name_":
2365 *
2366 * Hash.const_defined? 'foobar' #=> NameError: wrong constant name foobar
2367 *
2368 */
2369
2370static VALUE
2371rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
2372{
2373 VALUE name, recur;
2374 rb_encoding *enc;
2375 const char *pbeg, *p, *path, *pend;
2376 ID id;
2377
2378 rb_check_arity(argc, 1, 2);
2379 name = argv[0];
2380 recur = (argc == 1) ? Qtrue : argv[1];
2381
2382 if (SYMBOL_P(name)) {
2383 if (!rb_is_const_sym(name)) goto wrong_name;
2384 id = rb_check_id(&name);
2385 if (!id) return Qfalse;
2387 }
2388
2389 path = StringValuePtr(name);
2390 enc = rb_enc_get(name);
2391
2392 if (!rb_enc_asciicompat(enc)) {
2393 rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
2394 }
2395
2396 pbeg = p = path;
2397 pend = path + RSTRING_LEN(name);
2398
2399 if (p >= pend || !*p) {
2400 goto wrong_name;
2401 }
2402
2403 if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
2404 mod = rb_cObject;
2405 p += 2;
2406 pbeg = p;
2407 }
2408
2409 while (p < pend) {
2410 VALUE part;
2411 long len, beglen;
2412
2413 while (p < pend && *p != ':') p++;
2414
2415 if (pbeg == p) goto wrong_name;
2416
2417 id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
2418 beglen = pbeg-path;
2419
2420 if (p < pend && p[0] == ':') {
2421 if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2422 p += 2;
2423 pbeg = p;
2424 }
2425
2426 if (!id) {
2427 part = rb_str_subseq(name, beglen, len);
2428 OBJ_FREEZE(part);
2429 if (!rb_is_const_name(part)) {
2430 name = part;
2431 goto wrong_name;
2432 }
2433 else {
2434 return Qfalse;
2435 }
2436 }
2437 if (!rb_is_const_id(id)) {
2438 name = ID2SYM(id);
2439 goto wrong_name;
2440 }
2441
2442#if 0
2443 mod = rb_const_search(mod, id, beglen > 0 || !RTEST(recur), RTEST(recur), FALSE);
2444 if (mod == Qundef) return Qfalse;
2445#else
2446 if (!RTEST(recur)) {
2447 if (!rb_const_defined_at(mod, id))
2448 return Qfalse;
2449 if (p == pend) return Qtrue;
2450 mod = rb_const_get_at(mod, id);
2451 }
2452 else if (beglen == 0) {
2453 if (!rb_const_defined(mod, id))
2454 return Qfalse;
2455 if (p == pend) return Qtrue;
2456 mod = rb_const_get(mod, id);
2457 }
2458 else {
2459 if (!rb_const_defined_from(mod, id))
2460 return Qfalse;
2461 if (p == pend) return Qtrue;
2462 mod = rb_const_get_from(mod, id);
2463 }
2464#endif
2465
2466 if (p < pend && !RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2467 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2468 QUOTE(name));
2469 }
2470 }
2471
2472 return Qtrue;
2473
2474 wrong_name:
2475 rb_name_err_raise(wrong_constant_name, mod, name);
2477}
2478
2479/*
2480 * call-seq:
2481 * mod.const_source_location(sym, inherit=true) -> [String, Integer]
2482 * mod.const_source_location(str, inherit=true) -> [String, Integer]
2483 *
2484 * Returns the Ruby source filename and line number containing the definition
2485 * of the constant specified. If the named constant is not found, +nil+ is returned.
2486 * If the constant is found, but its source location can not be extracted
2487 * (constant is defined in C code), empty array is returned.
2488 *
2489 * _inherit_ specifies whether to lookup in <code>mod.ancestors</code> (+true+
2490 * by default).
2491 *
2492 * # test.rb:
2493 * class A # line 1
2494 * C1 = 1
2495 * C2 = 2
2496 * end
2497 *
2498 * module M # line 6
2499 * C3 = 3
2500 * end
2501 *
2502 * class B < A # line 10
2503 * include M
2504 * C4 = 4
2505 * end
2506 *
2507 * class A # continuation of A definition
2508 * C2 = 8 # constant redefinition; warned yet allowed
2509 * end
2510 *
2511 * p B.const_source_location('C4') # => ["test.rb", 12]
2512 * p B.const_source_location('C3') # => ["test.rb", 7]
2513 * p B.const_source_location('C1') # => ["test.rb", 2]
2514 *
2515 * p B.const_source_location('C3', false) # => nil -- don't lookup in ancestors
2516 *
2517 * p A.const_source_location('C2') # => ["test.rb", 16] -- actual (last) definition place
2518 *
2519 * p Object.const_source_location('B') # => ["test.rb", 10] -- top-level constant could be looked through Object
2520 * p Object.const_source_location('A') # => ["test.rb", 1] -- class reopening is NOT considered new definition
2521 *
2522 * p B.const_source_location('A') # => ["test.rb", 1] -- because Object is in ancestors
2523 * p M.const_source_location('A') # => ["test.rb", 1] -- Object is not ancestor, but additionally checked for modules
2524 *
2525 * p Object.const_source_location('A::C1') # => ["test.rb", 2] -- nesting is supported
2526 * p Object.const_source_location('String') # => [] -- constant is defined in C code
2527 *
2528 *
2529 */
2530static VALUE
2531rb_mod_const_source_location(int argc, VALUE *argv, VALUE mod)
2532{
2533 VALUE name, recur, loc = Qnil;
2534 rb_encoding *enc;
2535 const char *pbeg, *p, *path, *pend;
2536 ID id;
2537
2538 rb_check_arity(argc, 1, 2);
2539 name = argv[0];
2540 recur = (argc == 1) ? Qtrue : argv[1];
2541
2542 if (SYMBOL_P(name)) {
2543 if (!rb_is_const_sym(name)) goto wrong_name;
2544 id = rb_check_id(&name);
2545 if (!id) return Qnil;
2547 }
2548
2549 path = StringValuePtr(name);
2550 enc = rb_enc_get(name);
2551
2552 if (!rb_enc_asciicompat(enc)) {
2553 rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
2554 }
2555
2556 pbeg = p = path;
2557 pend = path + RSTRING_LEN(name);
2558
2559 if (p >= pend || !*p) {
2560 goto wrong_name;
2561 }
2562
2563 if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
2564 mod = rb_cObject;
2565 p += 2;
2566 pbeg = p;
2567 }
2568
2569 while (p < pend) {
2570 VALUE part;
2571 long len, beglen;
2572
2573 while (p < pend && *p != ':') p++;
2574
2575 if (pbeg == p) goto wrong_name;
2576
2577 id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
2578 beglen = pbeg-path;
2579
2580 if (p < pend && p[0] == ':') {
2581 if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2582 p += 2;
2583 pbeg = p;
2584 }
2585
2586 if (!id) {
2587 part = rb_str_subseq(name, beglen, len);
2588 OBJ_FREEZE(part);
2589 if (!rb_is_const_name(part)) {
2590 name = part;
2591 goto wrong_name;
2592 }
2593 else {
2594 return Qnil;
2595 }
2596 }
2597 if (!rb_is_const_id(id)) {
2598 name = ID2SYM(id);
2599 goto wrong_name;
2600 }
2601 if (p < pend) {
2602 if (RTEST(recur)) {
2603 mod = rb_const_get(mod, id);
2604 }
2605 else {
2606 mod = rb_const_get_at(mod, id);
2607 }
2608 if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2609 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2610 QUOTE(name));
2611 }
2612 }
2613 else {
2614 if (RTEST(recur)) {
2615 loc = rb_const_source_location(mod, id);
2616 }
2617 else {
2619 }
2620 break;
2621 }
2622 recur = Qfalse;
2623 }
2624
2625 return loc;
2626
2627 wrong_name:
2628 rb_name_err_raise(wrong_constant_name, mod, name);
2630}
2631
2632/*
2633 * call-seq:
2634 * obj.instance_variable_get(symbol) -> obj
2635 * obj.instance_variable_get(string) -> obj
2636 *
2637 * Returns the value of the given instance variable, or nil if the
2638 * instance variable is not set. The <code>@</code> part of the
2639 * variable name should be included for regular instance
2640 * variables. Throws a NameError exception if the
2641 * supplied symbol is not valid as an instance variable name.
2642 * String arguments are converted to symbols.
2643 *
2644 * class Fred
2645 * def initialize(p1, p2)
2646 * @a, @b = p1, p2
2647 * end
2648 * end
2649 * fred = Fred.new('cat', 99)
2650 * fred.instance_variable_get(:@a) #=> "cat"
2651 * fred.instance_variable_get("@b") #=> 99
2652 */
2653
2654static VALUE
2655rb_obj_ivar_get(VALUE obj, VALUE iv)
2656{
2657 ID id = id_for_var(obj, iv, instance);
2658
2659 if (!id) {
2660 return Qnil;
2661 }
2662 return rb_ivar_get(obj, id);
2663}
2664
2665/*
2666 * call-seq:
2667 * obj.instance_variable_set(symbol, obj) -> obj
2668 * obj.instance_variable_set(string, obj) -> obj
2669 *
2670 * Sets the instance variable named by <i>symbol</i> to the given
2671 * object. This may circumvent the encapsulation intended by
2672 * the author of the class, so it should be used with care.
2673 * The variable does not have to exist prior to this call.
2674 * If the instance variable name is passed as a string, that string
2675 * is converted to a symbol.
2676 *
2677 * class Fred
2678 * def initialize(p1, p2)
2679 * @a, @b = p1, p2
2680 * end
2681 * end
2682 * fred = Fred.new('cat', 99)
2683 * fred.instance_variable_set(:@a, 'dog') #=> "dog"
2684 * fred.instance_variable_set(:@c, 'cat') #=> "cat"
2685 * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
2686 */
2687
2688static VALUE
2689rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
2690{
2691 ID id = id_for_var(obj, iv, instance);
2692 if (!id) id = rb_intern_str(iv);
2693 return rb_ivar_set(obj, id, val);
2694}
2695
2696/*
2697 * call-seq:
2698 * obj.instance_variable_defined?(symbol) -> true or false
2699 * obj.instance_variable_defined?(string) -> true or false
2700 *
2701 * Returns <code>true</code> if the given instance variable is
2702 * defined in <i>obj</i>.
2703 * String arguments are converted to symbols.
2704 *
2705 * class Fred
2706 * def initialize(p1, p2)
2707 * @a, @b = p1, p2
2708 * end
2709 * end
2710 * fred = Fred.new('cat', 99)
2711 * fred.instance_variable_defined?(:@a) #=> true
2712 * fred.instance_variable_defined?("@b") #=> true
2713 * fred.instance_variable_defined?("@c") #=> false
2714 */
2715
2716static VALUE
2717rb_obj_ivar_defined(VALUE obj, VALUE iv)
2718{
2719 ID id = id_for_var(obj, iv, instance);
2720
2721 if (!id) {
2722 return Qfalse;
2723 }
2724 return rb_ivar_defined(obj, id);
2725}
2726
2727/*
2728 * call-seq:
2729 * mod.class_variable_get(symbol) -> obj
2730 * mod.class_variable_get(string) -> obj
2731 *
2732 * Returns the value of the given class variable (or throws a
2733 * NameError exception). The <code>@@</code> part of the
2734 * variable name should be included for regular class variables.
2735 * String arguments are converted to symbols.
2736 *
2737 * class Fred
2738 * @@foo = 99
2739 * end
2740 * Fred.class_variable_get(:@@foo) #=> 99
2741 */
2742
2743static VALUE
2744rb_mod_cvar_get(VALUE obj, VALUE iv)
2745{
2746 ID id = id_for_var(obj, iv, class);
2747
2748 if (!id) {
2749 rb_name_err_raise("uninitialized class variable %1$s in %2$s",
2750 obj, iv);
2751 }
2752 return rb_cvar_get(obj, id);
2753}
2754
2755/*
2756 * call-seq:
2757 * obj.class_variable_set(symbol, obj) -> obj
2758 * obj.class_variable_set(string, obj) -> obj
2759 *
2760 * Sets the class variable named by <i>symbol</i> to the given
2761 * object.
2762 * If the class variable name is passed as a string, that string
2763 * is converted to a symbol.
2764 *
2765 * class Fred
2766 * @@foo = 99
2767 * def foo
2768 * @@foo
2769 * end
2770 * end
2771 * Fred.class_variable_set(:@@foo, 101) #=> 101
2772 * Fred.new.foo #=> 101
2773 */
2774
2775static VALUE
2776rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
2777{
2778 ID id = id_for_var(obj, iv, class);
2779 if (!id) id = rb_intern_str(iv);
2780 rb_cvar_set(obj, id, val);
2781 return val;
2782}
2783
2784/*
2785 * call-seq:
2786 * obj.class_variable_defined?(symbol) -> true or false
2787 * obj.class_variable_defined?(string) -> true or false
2788 *
2789 * Returns <code>true</code> if the given class variable is defined
2790 * in <i>obj</i>.
2791 * String arguments are converted to symbols.
2792 *
2793 * class Fred
2794 * @@foo = 99
2795 * end
2796 * Fred.class_variable_defined?(:@@foo) #=> true
2797 * Fred.class_variable_defined?(:@@bar) #=> false
2798 */
2799
2800static VALUE
2801rb_mod_cvar_defined(VALUE obj, VALUE iv)
2802{
2803 ID id = id_for_var(obj, iv, class);
2804
2805 if (!id) {
2806 return Qfalse;
2807 }
2808 return rb_cvar_defined(obj, id);
2809}
2810
2811/*
2812 * call-seq:
2813 * mod.singleton_class? -> true or false
2814 *
2815 * Returns <code>true</code> if <i>mod</i> is a singleton class or
2816 * <code>false</code> if it is an ordinary class or module.
2817 *
2818 * class C
2819 * end
2820 * C.singleton_class? #=> false
2821 * C.singleton_class.singleton_class? #=> true
2822 */
2823
2824static VALUE
2825rb_mod_singleton_p(VALUE klass)
2826{
2827 if (RB_TYPE_P(klass, T_CLASS) && FL_TEST(klass, FL_SINGLETON))
2828 return Qtrue;
2829 return Qfalse;
2830}
2831
2833static const struct conv_method_tbl {
2834 const char method[6];
2835 unsigned short id;
2836} conv_method_names[] = {
2837#define M(n) {#n, (unsigned short)idTo_##n}
2838 M(int),
2839 M(ary),
2840 M(str),
2841 M(sym),
2842 M(hash),
2843 M(proc),
2844 M(io),
2845 M(a),
2846 M(s),
2847 M(i),
2848 M(r),
2849#undef M
2850};
2851#define IMPLICIT_CONVERSIONS 7
2852
2853static int
2854conv_method_index(const char *method)
2855{
2856 static const char prefix[] = "to_";
2857
2858 if (strncmp(prefix, method, sizeof(prefix)-1) == 0) {
2859 const char *const meth = &method[sizeof(prefix)-1];
2860 int i;
2861 for (i=0; i < numberof(conv_method_names); i++) {
2862 if (conv_method_names[i].method[0] == meth[0] &&
2863 strcmp(conv_method_names[i].method, meth) == 0) {
2864 return i;
2865 }
2866 }
2867 }
2868 return numberof(conv_method_names);
2869}
2870
2871static VALUE
2872convert_type_with_id(VALUE val, const char *tname, ID method, int raise, int index)
2873{
2874 VALUE r = rb_check_funcall(val, method, 0, 0);
2875 if (r == Qundef) {
2876 if (raise) {
2877 const char *msg =
2878 ((index < 0 ? conv_method_index(rb_id2name(method)) : index)
2880 "no implicit conversion of" : "can't convert";
2881 const char *cname = NIL_P(val) ? "nil" :
2882 val == Qtrue ? "true" :
2883 val == Qfalse ? "false" :
2884 NULL;
2885 if (cname)
2886 rb_raise(rb_eTypeError, "%s %s into %s", msg, cname, tname);
2887 rb_raise(rb_eTypeError, "%s %"PRIsVALUE" into %s", msg,
2888 rb_obj_class(val),
2889 tname);
2890 }
2891 return Qnil;
2892 }
2893 return r;
2894}
2895
2896static VALUE
2897convert_type(VALUE val, const char *tname, const char *method, int raise)
2898{
2899 int i = conv_method_index(method);
2900 ID m = i < numberof(conv_method_names) ?
2901 conv_method_names[i].id : rb_intern(method);
2902 return convert_type_with_id(val, tname, m, raise, i);
2903}
2904
2906NORETURN(static void conversion_mismatch(VALUE, const char *, const char *, VALUE));
2907static void
2908conversion_mismatch(VALUE val, const char *tname, const char *method, VALUE result)
2909{
2910 VALUE cname = rb_obj_class(val);
2912 "can't convert %"PRIsVALUE" to %s (%"PRIsVALUE"#%s gives %"PRIsVALUE")",
2913 cname, tname, cname, method, rb_obj_class(result));
2914}
2915
2929VALUE
2930rb_convert_type(VALUE val, int type, const char *tname, const char *method)
2931{
2932 VALUE v;
2933
2934 if (TYPE(val) == type) return val;
2935 v = convert_type(val, tname, method, TRUE);
2936 if (TYPE(v) != type) {
2937 conversion_mismatch(val, tname, method, v);
2938 }
2939 return v;
2940}
2941
2943VALUE
2944rb_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
2945{
2946 VALUE v;
2947
2948 if (TYPE(val) == type) return val;
2949 v = convert_type_with_id(val, tname, method, TRUE, -1);
2950 if (TYPE(v) != type) {
2951 conversion_mismatch(val, tname, RSTRING_PTR(rb_id2str(method)), v);
2952 }
2953 return v;
2954}
2955
2970VALUE
2971rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
2972{
2973 VALUE v;
2974
2975 /* always convert T_DATA */
2976 if (TYPE(val) == type && type != T_DATA) return val;
2977 v = convert_type(val, tname, method, FALSE);
2978 if (NIL_P(v)) return Qnil;
2979 if (TYPE(v) != type) {
2980 conversion_mismatch(val, tname, method, v);
2981 }
2982 return v;
2983}
2984
2987rb_check_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
2988{
2989 VALUE v;
2990
2991 /* always convert T_DATA */
2992 if (TYPE(val) == type && type != T_DATA) return val;
2993 v = convert_type_with_id(val, tname, method, FALSE, -1);
2994 if (NIL_P(v)) return Qnil;
2995 if (TYPE(v) != type) {
2996 conversion_mismatch(val, tname, RSTRING_PTR(rb_id2str(method)), v);
2997 }
2998 return v;
2999}
3000
3001#define try_to_int(val, mid, raise) \
3002 convert_type_with_id(val, "Integer", mid, raise, -1)
3003
3004ALWAYS_INLINE(static VALUE rb_to_integer(VALUE val, const char *method, ID mid));
3005static inline VALUE
3006rb_to_integer(VALUE val, const char *method, ID mid)
3007{
3008 VALUE v;
3009
3010 if (RB_INTEGER_TYPE_P(val)) return val;
3011 v = try_to_int(val, mid, TRUE);
3012 if (!RB_INTEGER_TYPE_P(v)) {
3013 conversion_mismatch(val, "Integer", method, v);
3014 }
3015 return v;
3016}
3017
3028VALUE
3029rb_check_to_integer(VALUE val, const char *method)
3030{
3031 VALUE v;
3032
3033 if (FIXNUM_P(val)) return val;
3034 if (RB_TYPE_P(val, T_BIGNUM)) return val;
3035 v = convert_type(val, "Integer", method, FALSE);
3036 if (!RB_INTEGER_TYPE_P(v)) {
3037 return Qnil;
3038 }
3039 return v;
3040}
3041
3050VALUE
3052{
3053 return rb_to_integer(val, "to_int", idTo_int);
3054}
3055
3065VALUE
3067{
3068 if (RB_INTEGER_TYPE_P(val)) return val;
3069 val = try_to_int(val, idTo_int, FALSE);
3070 if (RB_INTEGER_TYPE_P(val)) return val;
3071 return Qnil;
3072}
3073
3074static VALUE
3075rb_check_to_i(VALUE val)
3076{
3077 if (RB_INTEGER_TYPE_P(val)) return val;
3078 val = try_to_int(val, idTo_i, FALSE);
3079 if (RB_INTEGER_TYPE_P(val)) return val;
3080 return Qnil;
3081}
3082
3083static VALUE
3084rb_convert_to_integer(VALUE val, int base, int raise_exception)
3085{
3086 VALUE tmp;
3087
3088 if (base) {
3089 tmp = rb_check_string_type(val);
3090
3091 if (! NIL_P(tmp)) {
3092 val = tmp;
3093 }
3094 else if (! raise_exception) {
3095 return Qnil;
3096 }
3097 else {
3098 rb_raise(rb_eArgError, "base specified for non string value");
3099 }
3100 }
3101 if (RB_FLOAT_TYPE_P(val)) {
3102 double f = RFLOAT_VALUE(val);
3103 if (!raise_exception && !isfinite(f)) return Qnil;
3104 if (FIXABLE(f)) return LONG2FIX((long)f);
3105 return rb_dbl2big(f);
3106 }
3107 else if (RB_INTEGER_TYPE_P(val)) {
3108 return val;
3109 }
3110 else if (RB_TYPE_P(val, T_STRING)) {
3111 return rb_str_convert_to_inum(val, base, TRUE, raise_exception);
3112 }
3113 else if (NIL_P(val)) {
3114 if (!raise_exception) return Qnil;
3115 rb_raise(rb_eTypeError, "can't convert nil into Integer");
3116 }
3117
3118 tmp = rb_protect(rb_check_to_int, val, NULL);
3119 if (RB_INTEGER_TYPE_P(tmp)) return tmp;
3121
3122 if (!raise_exception) {
3123 VALUE result = rb_protect(rb_check_to_i, val, NULL);
3125 return result;
3126 }
3127
3128 return rb_to_integer(val, "to_i", idTo_i);
3129}
3130
3137VALUE
3139{
3140 return rb_convert_to_integer(val, 0, TRUE);
3141}
3142
3143int
3144rb_bool_expected(VALUE obj, const char *flagname)
3145{
3146 switch (obj) {
3147 case Qtrue: case Qfalse:
3148 break;
3149 default:
3150 rb_raise(rb_eArgError, "true or false is expected as %s: %+"PRIsVALUE,
3151 flagname, obj);
3152 }
3153 return obj != Qfalse;
3154}
3155
3156int
3157rb_opts_exception_p(VALUE opts, int default_value)
3158{
3159 static ID kwds[1] = {idException};
3160 VALUE exception;
3161 if (rb_get_kwargs(opts, kwds, 0, 1, &exception))
3162 return rb_bool_expected(exception, "exception");
3163 return default_value;
3164}
3165
3166#define opts_exception_p(opts) rb_opts_exception_p((opts), TRUE)
3167
3168/*
3169 * call-seq:
3170 * Integer(arg, base=0, exception: true) -> integer or nil
3171 *
3172 * Converts <i>arg</i> to an Integer.
3173 * Numeric types are converted directly (with floating point numbers
3174 * being truncated). <i>base</i> (0, or between 2 and 36) is a base for
3175 * integer string representation. If <i>arg</i> is a String,
3176 * when <i>base</i> is omitted or equals zero, radix indicators
3177 * (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
3178 * In any case, strings should consist only of one or more digits, except
3179 * for that a sign, one underscore between two digits, and leading/trailing
3180 * spaces are optional. This behavior is different from that of
3181 * String#to_i. Non string values will be converted by first
3182 * trying <code>to_int</code>, then <code>to_i</code>.
3183 *
3184 * Passing <code>nil</code> raises a TypeError, while passing a String that
3185 * does not conform with numeric representation raises an ArgumentError.
3186 * This behavior can be altered by passing <code>exception: false</code>,
3187 * in this case a not convertible value will return <code>nil</code>.
3188 *
3189 * Integer(123.999) #=> 123
3190 * Integer("0x1a") #=> 26
3191 * Integer(Time.new) #=> 1204973019
3192 * Integer("0930", 10) #=> 930
3193 * Integer("111", 2) #=> 7
3194 * Integer(" +1_0 ") #=> 10
3195 * Integer(nil) #=> TypeError: can't convert nil into Integer
3196 * Integer("x") #=> ArgumentError: invalid value for Integer(): "x"
3197 *
3198 * Integer("x", exception: false) #=> nil
3199 *
3200 */
3201
3202static VALUE
3203rb_f_integer(int argc, VALUE *argv, VALUE obj)
3204{
3205 VALUE arg = Qnil, opts = Qnil;
3206 int base = 0;
3207
3208 if (argc > 1) {
3209 int narg = 1;
3210 VALUE vbase = rb_check_to_int(argv[1]);
3211 if (!NIL_P(vbase)) {
3212 base = NUM2INT(vbase);
3213 narg = 2;
3214 }
3215 if (argc > narg) {
3216 VALUE hash = rb_check_hash_type(argv[argc-1]);
3217 if (!NIL_P(hash)) {
3218 opts = rb_extract_keywords(&hash);
3219 if (!hash) --argc;
3220 }
3221 }
3222 }
3223 rb_check_arity(argc, 1, 2);
3224 arg = argv[0];
3225
3226 return rb_convert_to_integer(arg, base, opts_exception_p(opts));
3227}
3228
3229static double
3230rb_cstr_to_dbl_raise(const char *p, int badcheck, int raise, int *error)
3231{
3232 const char *q;
3233 char *end;
3234 double d;
3235 const char *ellipsis = "";
3236 int w;
3237 enum {max_width = 20};
3238#define OutOfRange() ((end - p > max_width) ? \
3239 (w = max_width, ellipsis = "...") : \
3240 (w = (int)(end - p), ellipsis = ""))
3241
3242 if (!p) return 0.0;
3243 q = p;
3244 while (ISSPACE(*p)) p++;
3245
3246 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
3247 return 0.0;
3248 }
3249
3250 d = strtod(p, &end);
3251 if (errno == ERANGE) {
3252 OutOfRange();
3253 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
3254 errno = 0;
3255 }
3256 if (p == end) {
3257 if (badcheck) {
3258 goto bad;
3259 }
3260 return d;
3261 }
3262 if (*end) {
3263 char buf[DBL_DIG * 4 + 10];
3264 char *n = buf;
3265 char *const init_e = buf + DBL_DIG * 4;
3266 char *e = init_e;
3267 char prev = 0;
3268 int dot_seen = FALSE;
3269
3270 switch (*p) {case '+': case '-': prev = *n++ = *p++;}
3271 if (*p == '0') {
3272 prev = *n++ = '0';
3273 while (*++p == '0');
3274 }
3275 while (p < end && n < e) prev = *n++ = *p++;
3276 while (*p) {
3277 if (*p == '_') {
3278 /* remove an underscore between digits */
3279 if (n == buf || !ISDIGIT(prev) || (++p, !ISDIGIT(*p))) {
3280 if (badcheck) goto bad;
3281 break;
3282 }
3283 }
3284 prev = *p++;
3285 if (e == init_e && (prev == 'e' || prev == 'E' || prev == 'p' || prev == 'P')) {
3286 e = buf + sizeof(buf) - 1;
3287 *n++ = prev;
3288 switch (*p) {case '+': case '-': prev = *n++ = *p++;}
3289 if (*p == '0') {
3290 prev = *n++ = '0';
3291 while (*++p == '0');
3292 }
3293 continue;
3294 }
3295 else if (ISSPACE(prev)) {
3296 while (ISSPACE(*p)) ++p;
3297 if (*p) {
3298 if (badcheck) goto bad;
3299 break;
3300 }
3301 }
3302 else if (prev == '.' ? dot_seen++ : !ISDIGIT(prev)) {
3303 if (badcheck) goto bad;
3304 break;
3305 }
3306 if (n < e) *n++ = prev;
3307 }
3308 *n = '\0';
3309 p = buf;
3310
3311 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
3312 return 0.0;
3313 }
3314
3315 d = strtod(p, &end);
3316 if (errno == ERANGE) {
3317 OutOfRange();
3318 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
3319 errno = 0;
3320 }
3321 if (badcheck) {
3322 if (!end || p == end) goto bad;
3323 while (*end && ISSPACE(*end)) end++;
3324 if (*end) goto bad;
3325 }
3326 }
3327 if (errno == ERANGE) {
3328 errno = 0;
3329 OutOfRange();
3330 rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
3331 }
3332 return d;
3333
3334 bad:
3335 if (raise) {
3336 rb_invalid_str(q, "Float()");
3338 }
3339 else {
3340 if (error) *error = 1;
3341 return 0.0;
3342 }
3343}
3344
3356double
3357rb_cstr_to_dbl(const char *p, int badcheck)
3358{
3359 return rb_cstr_to_dbl_raise(p, badcheck, TRUE, NULL);
3360}
3361
3362static double
3363rb_str_to_dbl_raise(VALUE str, int badcheck, int raise, int *error)
3364{
3365 char *s;
3366 long len;
3367 double ret;
3368 VALUE v = 0;
3369
3371 s = RSTRING_PTR(str);
3372 len = RSTRING_LEN(str);
3373 if (s) {
3374 if (badcheck && memchr(s, '\0', len)) {
3375 if (raise)
3376 rb_raise(rb_eArgError, "string for Float contains null byte");
3377 else {
3378 if (error) *error = 1;
3379 return 0.0;
3380 }
3381 }
3382 if (s[len]) { /* no sentinel somehow */
3383 char *p = ALLOCV(v, (size_t)len + 1);
3384 MEMCPY(p, s, char, len);
3385 p[len] = '\0';
3386 s = p;
3387 }
3388 }
3389 ret = rb_cstr_to_dbl_raise(s, badcheck, raise, error);
3390 if (v)
3391 ALLOCV_END(v);
3392 return ret;
3393}
3394
3395FUNC_MINIMIZED(double rb_str_to_dbl(VALUE str, int badcheck));
3396
3408double
3410{
3411 return rb_str_to_dbl_raise(str, badcheck, TRUE, NULL);
3412}
3413
3415#define fix2dbl_without_to_f(x) (double)FIX2LONG(x)
3416#define big2dbl_without_to_f(x) rb_big2dbl(x)
3417#define int2dbl_without_to_f(x) \
3418 (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : big2dbl_without_to_f(x))
3419#define num2dbl_without_to_f(x) \
3420 (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : \
3421 RB_TYPE_P(x, T_BIGNUM) ? big2dbl_without_to_f(x) : \
3422 (Check_Type(x, T_FLOAT), RFLOAT_VALUE(x)))
3423static inline double
3424rat2dbl_without_to_f(VALUE x)
3425{
3427 VALUE den = rb_rational_den(x);
3428 return num2dbl_without_to_f(num) / num2dbl_without_to_f(den);
3429}
3430
3431#define special_const_to_float(val, pre, post) \
3432 switch (val) { \
3433 case Qnil: \
3434 rb_raise_static(rb_eTypeError, pre "nil" post); \
3435 case Qtrue: \
3436 rb_raise_static(rb_eTypeError, pre "true" post); \
3437 case Qfalse: \
3438 rb_raise_static(rb_eTypeError, pre "false" post); \
3439 }
3442static inline void
3443conversion_to_float(VALUE val)
3444{
3445 special_const_to_float(val, "can't convert ", " into Float");
3446}
3447
3448static inline void
3449implicit_conversion_to_float(VALUE val)
3450{
3451 special_const_to_float(val, "no implicit conversion to float from ", "");
3452}
3453
3454static int
3455to_float(VALUE *valp, int raise_exception)
3456{
3457 VALUE val = *valp;
3458 if (SPECIAL_CONST_P(val)) {
3459 if (FIXNUM_P(val)) {
3460 *valp = DBL2NUM(fix2dbl_without_to_f(val));
3461 return T_FLOAT;
3462 }
3463 else if (FLONUM_P(val)) {
3464 return T_FLOAT;
3465 }
3466 else if (raise_exception) {
3467 conversion_to_float(val);
3468 }
3469 }
3470 else {
3471 int type = BUILTIN_TYPE(val);
3472 switch (type) {
3473 case T_FLOAT:
3474 return T_FLOAT;
3475 case T_BIGNUM:
3476 *valp = DBL2NUM(big2dbl_without_to_f(val));
3477 return T_FLOAT;
3478 case T_RATIONAL:
3479 *valp = DBL2NUM(rat2dbl_without_to_f(val));
3480 return T_FLOAT;
3481 case T_STRING:
3482 return T_STRING;
3483 }
3484 }
3485 return T_NONE;
3486}
3487
3488static VALUE
3489convert_type_to_float_protected(VALUE val)
3490{
3491 return rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3492}
3493
3494static VALUE
3495rb_convert_to_float(VALUE val, int raise_exception)
3496{
3497 switch (to_float(&val, raise_exception)) {
3498 case T_FLOAT:
3499 return val;
3500 case T_STRING:
3501 if (!raise_exception) {
3502 int e = 0;
3503 double x = rb_str_to_dbl_raise(val, TRUE, raise_exception, &e);
3504 return e ? Qnil : DBL2NUM(x);
3505 }
3506 return DBL2NUM(rb_str_to_dbl(val, TRUE));
3507 case T_NONE:
3508 if (SPECIAL_CONST_P(val) && !raise_exception)
3509 return Qnil;
3510 }
3511
3512 if (!raise_exception) {
3513 int state;
3514 VALUE result = rb_protect(convert_type_to_float_protected, val, &state);
3516 return result;
3517 }
3518
3519 return rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3520}
3521
3523
3530VALUE
3532{
3533 return rb_convert_to_float(val, TRUE);
3534}
3535
3536static VALUE
3537rb_f_float(rb_execution_context_t *ec, VALUE obj, VALUE arg, VALUE opts)
3538{
3539 int exception = rb_bool_expected(opts, "exception");
3540 return rb_convert_to_float(arg, exception);
3541}
3542
3543static VALUE
3544numeric_to_float(VALUE val)
3545{
3546 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
3547 rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into Float",
3548 rb_obj_class(val));
3549 }
3550 return rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3551}
3552
3558VALUE
3560{
3561 switch (to_float(&val, TRUE)) {
3562 case T_FLOAT:
3563 return val;
3564 }
3565 return numeric_to_float(val);
3566}
3567
3575VALUE
3577{
3578 if (RB_TYPE_P(val, T_FLOAT)) return val;
3579 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
3580 return Qnil;
3581 }
3582 return rb_check_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3583}
3584
3585static inline int
3586basic_to_f_p(VALUE klass)
3587{
3589}
3590
3592double
3594{
3595 if (SPECIAL_CONST_P(val)) {
3596 if (FIXNUM_P(val)) {
3597 if (basic_to_f_p(rb_cInteger))
3598 return fix2dbl_without_to_f(val);
3599 }
3600 else if (FLONUM_P(val)) {
3601 return rb_float_flonum_value(val);
3602 }
3603 else {
3604 conversion_to_float(val);
3605 }
3606 }
3607 else {
3608 switch (BUILTIN_TYPE(val)) {
3609 case T_FLOAT:
3610 return rb_float_noflonum_value(val);
3611 case T_BIGNUM:
3612 if (basic_to_f_p(rb_cInteger))
3613 return big2dbl_without_to_f(val);
3614 break;
3615 case T_RATIONAL:
3616 if (basic_to_f_p(rb_cRational))
3617 return rat2dbl_without_to_f(val);
3618 break;
3619 default:
3620 break;
3621 }
3622 }
3623 val = numeric_to_float(val);
3624 return RFLOAT_VALUE(val);
3625}
3626
3634double
3636{
3637 if (SPECIAL_CONST_P(val)) {
3638 if (FIXNUM_P(val)) {
3639 return fix2dbl_without_to_f(val);
3640 }
3641 else if (FLONUM_P(val)) {
3642 return rb_float_flonum_value(val);
3643 }
3644 else {
3645 implicit_conversion_to_float(val);
3646 }
3647 }
3648 else {
3649 switch (BUILTIN_TYPE(val)) {
3650 case T_FLOAT:
3651 return rb_float_noflonum_value(val);
3652 case T_BIGNUM:
3653 return big2dbl_without_to_f(val);
3654 case T_RATIONAL:
3655 return rat2dbl_without_to_f(val);
3656 case T_STRING:
3657 rb_raise(rb_eTypeError, "no implicit conversion to float from string");
3658 default:
3659 break;
3660 }
3661 }
3662 val = rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3663 return RFLOAT_VALUE(val);
3664}
3665
3672VALUE
3674{
3675 VALUE tmp = rb_check_string_type(val);
3676 if (NIL_P(tmp))
3677 tmp = rb_convert_type_with_id(val, T_STRING, "String", idTo_s);
3678 return tmp;
3679}
3680
3681
3682/*
3683 * call-seq:
3684 * String(arg) -> string
3685 *
3686 * Returns <i>arg</i> as a String.
3687 *
3688 * First tries to call its <code>to_str</code> method, then its <code>to_s</code> method.
3689 *
3690 * String(self) #=> "main"
3691 * String(self.class) #=> "Object"
3692 * String(123456) #=> "123456"
3693 */
3694
3695static VALUE
3696rb_f_string(VALUE obj, VALUE arg)
3697{
3698 return rb_String(arg);
3699}
3700
3704VALUE
3706{
3707 VALUE tmp = rb_check_array_type(val);
3708
3709 if (NIL_P(tmp)) {
3710 tmp = rb_check_to_array(val);
3711 if (NIL_P(tmp)) {
3712 return rb_ary_new3(1, val);
3713 }
3714 }
3715 return tmp;
3716}
3717
3718/*
3719 * call-seq:
3720 * Array(arg) -> array
3721 *
3722 * Returns +arg+ as an Array.
3723 *
3724 * First tries to call <code>to_ary</code> on +arg+, then <code>to_a</code>.
3725 * If +arg+ does not respond to <code>to_ary</code> or <code>to_a</code>,
3726 * returns an Array of length 1 containing +arg+.
3727 *
3728 * If <code>to_ary</code> or <code>to_a</code> returns something other than
3729 * an Array, raises a TypeError.
3730 *
3731 * Array(["a", "b"]) #=> ["a", "b"]
3732 * Array(1..5) #=> [1, 2, 3, 4, 5]
3733 * Array(key: :value) #=> [[:key, :value]]
3734 * Array(nil) #=> []
3735 * Array(1) #=> [1]
3736 */
3737
3738static VALUE
3739rb_f_array(VALUE obj, VALUE arg)
3740{
3741 return rb_Array(arg);
3742}
3743
3747VALUE
3749{
3750 VALUE tmp;
3751
3752 if (NIL_P(val)) return rb_hash_new();
3753 tmp = rb_check_hash_type(val);
3754 if (NIL_P(tmp)) {
3755 if (RB_TYPE_P(val, T_ARRAY) && RARRAY_LEN(val) == 0)
3756 return rb_hash_new();
3757 rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
3758 }
3759 return tmp;
3760}
3761
3762/*
3763 * call-seq:
3764 * Hash(arg) -> hash
3765 *
3766 * Converts <i>arg</i> to a Hash by calling
3767 * <i>arg</i><code>.to_hash</code>. Returns an empty Hash when
3768 * <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>.
3769 *
3770 * Hash([]) #=> {}
3771 * Hash(nil) #=> {}
3772 * Hash(key: :value) #=> {:key => :value}
3773 * Hash([1, 2, 3]) #=> TypeError
3774 */
3775
3776static VALUE
3777rb_f_hash(VALUE obj, VALUE arg)
3778{
3779 return rb_Hash(arg);
3780}
3781
3783struct dig_method {
3784 VALUE klass;
3785 int basic;
3786};
3787
3788static ID id_dig;
3789
3790static int
3791dig_basic_p(VALUE obj, struct dig_method *cache)
3792{
3793 VALUE klass = RBASIC_CLASS(obj);
3794 if (klass != cache->klass) {
3795 cache->klass = klass;
3796 cache->basic = rb_method_basic_definition_p(klass, id_dig);
3797 }
3798 return cache->basic;
3799}
3800
3801static void
3802no_dig_method(int found, VALUE recv, ID mid, int argc, const VALUE *argv, VALUE data)
3803{
3804 if (!found) {
3805 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not have #dig method",
3806 CLASS_OF(data));
3807 }
3808}
3809
3811VALUE
3812rb_obj_dig(int argc, VALUE *argv, VALUE obj, VALUE notfound)
3813{
3814 struct dig_method hash = {Qnil}, ary = {Qnil}, strt = {Qnil};
3815
3816 for (; argc > 0; ++argv, --argc) {
3817 if (NIL_P(obj)) return notfound;
3818 if (!SPECIAL_CONST_P(obj)) {
3819 switch (BUILTIN_TYPE(obj)) {
3820 case T_HASH:
3821 if (dig_basic_p(obj, &hash)) {
3822 obj = rb_hash_aref(obj, *argv);
3823 continue;
3824 }
3825 break;
3826 case T_ARRAY:
3827 if (dig_basic_p(obj, &ary)) {
3828 obj = rb_ary_at(obj, *argv);
3829 continue;
3830 }
3831 break;
3832 case T_STRUCT:
3833 if (dig_basic_p(obj, &strt)) {
3834 obj = rb_struct_lookup(obj, *argv);
3835 continue;
3836 }
3837 break;
3838 default:
3839 break;
3840 }
3841 }
3842 return rb_check_funcall_with_hook_kw(obj, id_dig, argc, argv,
3843 no_dig_method, obj,
3845 }
3846 return obj;
3847}
3848
3849/*
3850 * call-seq:
3851 * format(format_string [, arguments...] ) -> string
3852 * sprintf(format_string [, arguments...] ) -> string
3853 *
3854 * Returns the string resulting from applying <i>format_string</i> to
3855 * any additional arguments. Within the format string, any characters
3856 * other than format sequences are copied to the result.
3857 *
3858 * The syntax of a format sequence is as follows.
3859 *
3860 * %[flags][width][.precision]type
3861 *
3862 * A format
3863 * sequence consists of a percent sign, followed by optional flags,
3864 * width, and precision indicators, then terminated with a field type
3865 * character. The field type controls how the corresponding
3866 * <code>sprintf</code> argument is to be interpreted, while the flags
3867 * modify that interpretation.
3868 *
3869 * The field type characters are:
3870 *
3871 * Field | Integer Format
3872 * ------+--------------------------------------------------------------
3873 * b | Convert argument as a binary number.
3874 * | Negative numbers will be displayed as a two's complement
3875 * | prefixed with `..1'.
3876 * B | Equivalent to `b', but uses an uppercase 0B for prefix
3877 * | in the alternative format by #.
3878 * d | Convert argument as a decimal number.
3879 * i | Identical to `d'.
3880 * o | Convert argument as an octal number.
3881 * | Negative numbers will be displayed as a two's complement
3882 * | prefixed with `..7'.
3883 * u | Identical to `d'.
3884 * x | Convert argument as a hexadecimal number.
3885 * | Negative numbers will be displayed as a two's complement
3886 * | prefixed with `..f' (representing an infinite string of
3887 * | leading 'ff's).
3888 * X | Equivalent to `x', but uses uppercase letters.
3889 *
3890 * Field | Float Format
3891 * ------+--------------------------------------------------------------
3892 * e | Convert floating point argument into exponential notation
3893 * | with one digit before the decimal point as [-]d.dddddde[+-]dd.
3894 * | The precision specifies the number of digits after the decimal
3895 * | point (defaulting to six).
3896 * E | Equivalent to `e', but uses an uppercase E to indicate
3897 * | the exponent.
3898 * f | Convert floating point argument as [-]ddd.dddddd,
3899 * | where the precision specifies the number of digits after
3900 * | the decimal point.
3901 * g | Convert a floating point number using exponential form
3902 * | if the exponent is less than -4 or greater than or
3903 * | equal to the precision, or in dd.dddd form otherwise.
3904 * | The precision specifies the number of significant digits.
3905 * G | Equivalent to `g', but use an uppercase `E' in exponent form.
3906 * a | Convert floating point argument as [-]0xh.hhhhp[+-]dd,
3907 * | which is consisted from optional sign, "0x", fraction part
3908 * | as hexadecimal, "p", and exponential part as decimal.
3909 * A | Equivalent to `a', but use uppercase `X' and `P'.
3910 *
3911 * Field | Other Format
3912 * ------+--------------------------------------------------------------
3913 * c | Argument is the numeric code for a single character or
3914 * | a single character string itself.
3915 * p | The valuing of argument.inspect.
3916 * s | Argument is a string to be substituted. If the format
3917 * | sequence contains a precision, at most that many characters
3918 * | will be copied.
3919 * % | A percent sign itself will be displayed. No argument taken.
3920 *
3921 * The flags modifies the behavior of the formats.
3922 * The flag characters are:
3923 *
3924 * Flag | Applies to | Meaning
3925 * ---------+---------------+-----------------------------------------
3926 * space | bBdiouxX | Leave a space at the start of
3927 * | aAeEfgG | non-negative numbers.
3928 * | (numeric fmt) | For `o', `x', `X', `b' and `B', use
3929 * | | a minus sign with absolute value for
3930 * | | negative values.
3931 * ---------+---------------+-----------------------------------------
3932 * (digit)$ | all | Specifies the absolute argument number
3933 * | | for this field. Absolute and relative
3934 * | | argument numbers cannot be mixed in a
3935 * | | sprintf string.
3936 * ---------+---------------+-----------------------------------------
3937 * # | bBoxX | Use an alternative format.
3938 * | aAeEfgG | For the conversions `o', increase the precision
3939 * | | until the first digit will be `0' if
3940 * | | it is not formatted as complements.
3941 * | | For the conversions `x', `X', `b' and `B'
3942 * | | on non-zero, prefix the result with ``0x'',
3943 * | | ``0X'', ``0b'' and ``0B'', respectively.
3944 * | | For `a', `A', `e', `E', `f', `g', and 'G',
3945 * | | force a decimal point to be added,
3946 * | | even if no digits follow.
3947 * | | For `g' and 'G', do not remove trailing zeros.
3948 * ---------+---------------+-----------------------------------------
3949 * + | bBdiouxX | Add a leading plus sign to non-negative
3950 * | aAeEfgG | numbers.
3951 * | (numeric fmt) | For `o', `x', `X', `b' and `B', use
3952 * | | a minus sign with absolute value for
3953 * | | negative values.
3954 * ---------+---------------+-----------------------------------------
3955 * - | all | Left-justify the result of this conversion.
3956 * ---------+---------------+-----------------------------------------
3957 * 0 (zero) | bBdiouxX | Pad with zeros, not spaces.
3958 * | aAeEfgG | For `o', `x', `X', `b' and `B', radix-1
3959 * | (numeric fmt) | is used for negative numbers formatted as
3960 * | | complements.
3961 * ---------+---------------+-----------------------------------------
3962 * * | all | Use the next argument as the field width.
3963 * | | If negative, left-justify the result. If the
3964 * | | asterisk is followed by a number and a dollar
3965 * | | sign, use the indicated argument as the width.
3966 *
3967 * Examples of flags:
3968 *
3969 * # `+' and space flag specifies the sign of non-negative numbers.
3970 * sprintf("%d", 123) #=> "123"
3971 * sprintf("%+d", 123) #=> "+123"
3972 * sprintf("% d", 123) #=> " 123"
3973 *
3974 * # `#' flag for `o' increases number of digits to show `0'.
3975 * # `+' and space flag changes format of negative numbers.
3976 * sprintf("%o", 123) #=> "173"
3977 * sprintf("%#o", 123) #=> "0173"
3978 * sprintf("%+o", -123) #=> "-173"
3979 * sprintf("%o", -123) #=> "..7605"
3980 * sprintf("%#o", -123) #=> "..7605"
3981 *
3982 * # `#' flag for `x' add a prefix `0x' for non-zero numbers.
3983 * # `+' and space flag disables complements for negative numbers.
3984 * sprintf("%x", 123) #=> "7b"
3985 * sprintf("%#x", 123) #=> "0x7b"
3986 * sprintf("%+x", -123) #=> "-7b"
3987 * sprintf("%x", -123) #=> "..f85"
3988 * sprintf("%#x", -123) #=> "0x..f85"
3989 * sprintf("%#x", 0) #=> "0"
3990 *
3991 * # `#' for `X' uses the prefix `0X'.
3992 * sprintf("%X", 123) #=> "7B"
3993 * sprintf("%#X", 123) #=> "0X7B"
3994 *
3995 * # `#' flag for `b' add a prefix `0b' for non-zero numbers.
3996 * # `+' and space flag disables complements for negative numbers.
3997 * sprintf("%b", 123) #=> "1111011"
3998 * sprintf("%#b", 123) #=> "0b1111011"
3999 * sprintf("%+b", -123) #=> "-1111011"
4000 * sprintf("%b", -123) #=> "..10000101"
4001 * sprintf("%#b", -123) #=> "0b..10000101"
4002 * sprintf("%#b", 0) #=> "0"
4003 *
4004 * # `#' for `B' uses the prefix `0B'.
4005 * sprintf("%B", 123) #=> "1111011"
4006 * sprintf("%#B", 123) #=> "0B1111011"
4007 *
4008 * # `#' for `e' forces to show the decimal point.
4009 * sprintf("%.0e", 1) #=> "1e+00"
4010 * sprintf("%#.0e", 1) #=> "1.e+00"
4011 *
4012 * # `#' for `f' forces to show the decimal point.
4013 * sprintf("%.0f", 1234) #=> "1234"
4014 * sprintf("%#.0f", 1234) #=> "1234."
4015 *
4016 * # `#' for `g' forces to show the decimal point.
4017 * # It also disables stripping lowest zeros.
4018 * sprintf("%g", 123.4) #=> "123.4"
4019 * sprintf("%#g", 123.4) #=> "123.400"
4020 * sprintf("%g", 123456) #=> "123456"
4021 * sprintf("%#g", 123456) #=> "123456."
4022 *
4023 * The field width is an optional integer, followed optionally by a
4024 * period and a precision. The width specifies the minimum number of
4025 * characters that will be written to the result for this field.
4026 *
4027 * Examples of width:
4028 *
4029 * # padding is done by spaces, width=20
4030 * # 0 or radix-1. <------------------>
4031 * sprintf("%20d", 123) #=> " 123"
4032 * sprintf("%+20d", 123) #=> " +123"
4033 * sprintf("%020d", 123) #=> "00000000000000000123"
4034 * sprintf("%+020d", 123) #=> "+0000000000000000123"
4035 * sprintf("% 020d", 123) #=> " 0000000000000000123"
4036 * sprintf("%-20d", 123) #=> "123 "
4037 * sprintf("%-+20d", 123) #=> "+123 "
4038 * sprintf("%- 20d", 123) #=> " 123 "
4039 * sprintf("%020x", -123) #=> "..ffffffffffffffff85"
4040 *
4041 * For
4042 * numeric fields, the precision controls the number of decimal places
4043 * displayed. For string fields, the precision determines the maximum
4044 * number of characters to be copied from the string. (Thus, the format
4045 * sequence <code>%10.10s</code> will always contribute exactly ten
4046 * characters to the result.)
4047 *
4048 * Examples of precisions:
4049 *
4050 * # precision for `d', 'o', 'x' and 'b' is
4051 * # minimum number of digits <------>
4052 * sprintf("%20.8d", 123) #=> " 00000123"
4053 * sprintf("%20.8o", 123) #=> " 00000173"
4054 * sprintf("%20.8x", 123) #=> " 0000007b"
4055 * sprintf("%20.8b", 123) #=> " 01111011"
4056 * sprintf("%20.8d", -123) #=> " -00000123"
4057 * sprintf("%20.8o", -123) #=> " ..777605"
4058 * sprintf("%20.8x", -123) #=> " ..ffff85"
4059 * sprintf("%20.8b", -11) #=> " ..110101"
4060 *
4061 * # "0x" and "0b" for `#x' and `#b' is not counted for
4062 * # precision but "0" for `#o' is counted. <------>
4063 * sprintf("%#20.8d", 123) #=> " 00000123"
4064 * sprintf("%#20.8o", 123) #=> " 00000173"
4065 * sprintf("%#20.8x", 123) #=> " 0x0000007b"
4066 * sprintf("%#20.8b", 123) #=> " 0b01111011"
4067 * sprintf("%#20.8d", -123) #=> " -00000123"
4068 * sprintf("%#20.8o", -123) #=> " ..777605"
4069 * sprintf("%#20.8x", -123) #=> " 0x..ffff85"
4070 * sprintf("%#20.8b", -11) #=> " 0b..110101"
4071 *
4072 * # precision for `e' is number of
4073 * # digits after the decimal point <------>
4074 * sprintf("%20.8e", 1234.56789) #=> " 1.23456789e+03"
4075 *
4076 * # precision for `f' is number of
4077 * # digits after the decimal point <------>
4078 * sprintf("%20.8f", 1234.56789) #=> " 1234.56789000"
4079 *
4080 * # precision for `g' is number of
4081 * # significant digits <------->
4082 * sprintf("%20.8g", 1234.56789) #=> " 1234.5679"
4083 *
4084 * # <------->
4085 * sprintf("%20.8g", 123456789) #=> " 1.2345679e+08"
4086 *
4087 * # precision for `s' is
4088 * # maximum number of characters <------>
4089 * sprintf("%20.8s", "string test") #=> " string t"
4090 *
4091 * Examples:
4092 *
4093 * sprintf("%d %04x", 123, 123) #=> "123 007b"
4094 * sprintf("%08b '%4s'", 123, 123) #=> "01111011 ' 123'"
4095 * sprintf("%1$*2$s %2$d %1$s", "hello", 8) #=> " hello 8 hello"
4096 * sprintf("%1$*2$s %2$d", "hello", -8) #=> "hello -8"
4097 * sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) #=> "+1.23: 1.23:1.23"
4098 * sprintf("%u", -123) #=> "-123"
4099 *
4100 * For more complex formatting, Ruby supports a reference by name.
4101 * %<name>s style uses format style, but %{name} style doesn't.
4102 *
4103 * Examples:
4104 * sprintf("%<foo>d : %<bar>f", { :foo => 1, :bar => 2 })
4105 * #=> 1 : 2.000000
4106 * sprintf("%{foo}f", { :foo => 1 })
4107 * # => "1f"
4108 */
4109
4110static VALUE
4111f_sprintf(int c, const VALUE *v, VALUE _)
4112{
4113 return rb_f_sprintf(c, v);
4114}
4115
4116/*
4117 * Document-class: Class
4118 *
4119 * Classes in Ruby are first-class objects---each is an instance of
4120 * class Class.
4121 *
4122 * Typically, you create a new class by using:
4123 *
4124 * class Name
4125 * # some code describing the class behavior
4126 * end
4127 *
4128 * When a new class is created, an object of type Class is initialized and
4129 * assigned to a global constant (Name in this case).
4130 *
4131 * When <code>Name.new</code> is called to create a new object, the
4132 * #new method in Class is run by default.
4133 * This can be demonstrated by overriding #new in Class:
4134 *
4135 * class Class
4136 * alias old_new new
4137 * def new(*args)
4138 * print "Creating a new ", self.name, "\n"
4139 * old_new(*args)
4140 * end
4141 * end
4142 *
4143 * class Name
4144 * end
4145 *
4146 * n = Name.new
4147 *
4148 * <em>produces:</em>
4149 *
4150 * Creating a new Name
4151 *
4152 * Classes, modules, and objects are interrelated. In the diagram
4153 * that follows, the vertical arrows represent inheritance, and the
4154 * parentheses metaclasses. All metaclasses are instances
4155 * of the class `Class'.
4156 * +---------+ +-...
4157 * | | |
4158 * BasicObject-----|-->(BasicObject)-------|-...
4159 * ^ | ^ |
4160 * | | | |
4161 * Object---------|----->(Object)---------|-...
4162 * ^ | ^ |
4163 * | | | |
4164 * +-------+ | +--------+ |
4165 * | | | | | |
4166 * | Module-|---------|--->(Module)-|-...
4167 * | ^ | | ^ |
4168 * | | | | | |
4169 * | Class-|---------|---->(Class)-|-...
4170 * | ^ | | ^ |
4171 * | +---+ | +----+
4172 * | |
4173 * obj--->OtherClass---------->(OtherClass)-----------...
4174 *
4175 */
4176
4177
4178/* Document-class: BasicObject
4179 *
4180 * BasicObject is the parent class of all classes in Ruby. It's an explicit
4181 * blank class.
4182 *
4183 * BasicObject can be used for creating object hierarchies independent of
4184 * Ruby's object hierarchy, proxy objects like the Delegator class, or other
4185 * uses where namespace pollution from Ruby's methods and classes must be
4186 * avoided.
4187 *
4188 * To avoid polluting BasicObject for other users an appropriately named
4189 * subclass of BasicObject should be created instead of directly modifying
4190 * BasicObject:
4191 *
4192 * class MyObjectSystem < BasicObject
4193 * end
4194 *
4195 * BasicObject does not include Kernel (for methods like +puts+) and
4196 * BasicObject is outside of the namespace of the standard library so common
4197 * classes will not be found without using a full class path.
4198 *
4199 * A variety of strategies can be used to provide useful portions of the
4200 * standard library to subclasses of BasicObject. A subclass could
4201 * <code>include Kernel</code> to obtain +puts+, +exit+, etc. A custom
4202 * Kernel-like module could be created and included or delegation can be used
4203 * via #method_missing:
4204 *
4205 * class MyObjectSystem < BasicObject
4206 * DELEGATE = [:puts, :p]
4207 *
4208 * def method_missing(name, *args, &block)
4209 * return super unless DELEGATE.include? name
4210 * ::Kernel.send(name, *args, &block)
4211 * end
4212 *
4213 * def respond_to_missing?(name, include_private = false)
4214 * DELEGATE.include?(name) or super
4215 * end
4216 * end
4217 *
4218 * Access to classes and modules from the Ruby standard library can be
4219 * obtained in a BasicObject subclass by referencing the desired constant
4220 * from the root like <code>::File</code> or <code>::Enumerator</code>.
4221 * Like #method_missing, #const_missing can be used to delegate constant
4222 * lookup to +Object+:
4223 *
4224 * class MyObjectSystem < BasicObject
4225 * def self.const_missing(name)
4226 * ::Object.const_get(name)
4227 * end
4228 * end
4229 */
4230
4231/* Document-class: Object
4232 *
4233 * Object is the default root of all Ruby objects. Object inherits from
4234 * BasicObject which allows creating alternate object hierarchies. Methods
4235 * on Object are available to all classes unless explicitly overridden.
4236 *
4237 * Object mixes in the Kernel module, making the built-in kernel functions
4238 * globally accessible. Although the instance methods of Object are defined
4239 * by the Kernel module, we have chosen to document them here for clarity.
4240 *
4241 * When referencing constants in classes inheriting from Object you do not
4242 * need to use the full namespace. For example, referencing +File+ inside
4243 * +YourClass+ will find the top-level File class.
4244 *
4245 * In the descriptions of Object's methods, the parameter <i>symbol</i> refers
4246 * to a symbol, which is either a quoted string or a Symbol (such as
4247 * <code>:name</code>).
4248 */
4249
4269void
4270InitVM_Object(void)
4271{
4273
4274#if 0
4275 // teach RDoc about these classes
4276 rb_cBasicObject = rb_define_class("BasicObject", Qnil);
4280#endif
4281
4282 rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy0, 0);
4284 rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
4285 rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
4286 rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
4287 rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);
4288
4289 rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy1, 1);
4290 rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy1, 1);
4291 rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy1, 1);
4292
4293 /* Document-module: Kernel
4294 *
4295 * The Kernel module is included by class Object, so its methods are
4296 * available in every Ruby object.
4297 *
4298 * The Kernel instance methods are documented in class Object while the
4299 * module methods are documented here. These methods are called without a
4300 * receiver and thus can be called in functional form:
4301 *
4302 * sprintf "%.1f", 1.234 #=> "1.2"
4303 *
4304 */
4305 rb_mKernel = rb_define_module("Kernel");
4307 rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy1, 1);
4308 rb_define_private_method(rb_cModule, "included", rb_obj_dummy1, 1);
4309 rb_define_private_method(rb_cModule, "extended", rb_obj_dummy1, 1);
4310 rb_define_private_method(rb_cModule, "prepended", rb_obj_dummy1, 1);
4311 rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy1, 1);
4312 rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy1, 1);
4313 rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy1, 1);
4314
4317 rb_define_method(rb_mKernel, "=~", rb_obj_match, 1);
4318 rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
4319 rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
4320 rb_define_method(rb_mKernel, "hash", rb_obj_hash, 0); /* in hash.c */
4321 rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
4322
4323 rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
4325 rb_define_method(rb_mKernel, "itself", rb_obj_itself, 0);
4326 rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
4327 rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
4328 rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_clone, -1);
4329
4334 rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0);
4337
4339 rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
4340 rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */
4341 rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
4342 rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
4343 rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */
4344 rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */
4345 rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
4346 rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
4347 rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
4348 rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
4349 rb_define_method(rb_mKernel, "remove_instance_variable",
4350 rb_obj_remove_instance_variable, 1); /* in variable.c */
4351
4355
4356 rb_define_global_function("sprintf", f_sprintf, -1);
4357 rb_define_global_function("format", f_sprintf, -1);
4358
4359 rb_define_global_function("Integer", rb_f_integer, -1);
4360
4361 rb_define_global_function("String", rb_f_string, 1);
4362 rb_define_global_function("Array", rb_f_array, 1);
4363 rb_define_global_function("Hash", rb_f_hash, 1);
4364
4366 rb_cNilClass_to_s = rb_fstring_enc_lit("", rb_usascii_encoding());
4367 rb_gc_register_mark_object(rb_cNilClass_to_s);
4368 rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
4369 rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
4370 rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
4371 rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
4372 rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0);
4373 rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
4374 rb_define_method(rb_cNilClass, "=~", nil_match, 1);
4375 rb_define_method(rb_cNilClass, "&", false_and, 1);
4379
4380 rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
4383
4384 rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
4385 rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
4386 rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
4387 rb_define_method(rb_cModule, "<=>", rb_mod_cmp, 1);
4388 rb_define_method(rb_cModule, "<", rb_mod_lt, 1);
4390 rb_define_method(rb_cModule, ">", rb_mod_gt, 1);
4391 rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
4392 rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
4393 rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
4394 rb_define_alias(rb_cModule, "inspect", "to_s");
4395 rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
4396 rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
4397 rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */
4398 rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
4399
4400 rb_define_method(rb_cModule, "attr", rb_mod_attr, -1);
4401 rb_define_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
4402 rb_define_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
4403 rb_define_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
4404
4405 rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
4406 rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
4407 rb_define_method(rb_cModule, "initialize_clone", rb_mod_initialize_clone, -1);
4408 rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
4409 rb_define_method(rb_cModule, "public_instance_methods",
4410 rb_class_public_instance_methods, -1); /* in class.c */
4411 rb_define_method(rb_cModule, "protected_instance_methods",
4412 rb_class_protected_instance_methods, -1); /* in class.c */
4413 rb_define_method(rb_cModule, "private_instance_methods",
4414 rb_class_private_instance_methods, -1); /* in class.c */
4415
4416 rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
4417 rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
4418 rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
4419 rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
4420 rb_define_method(rb_cModule, "const_source_location", rb_mod_const_source_location, -1);
4421 rb_define_private_method(rb_cModule, "remove_const",
4422 rb_mod_remove_const, 1); /* in variable.c */
4423 rb_define_method(rb_cModule, "const_missing",
4424 rb_mod_const_missing, 1); /* in variable.c */
4425 rb_define_method(rb_cModule, "class_variables",
4426 rb_mod_class_variables, -1); /* in variable.c */
4427 rb_define_method(rb_cModule, "remove_class_variable",
4428 rb_mod_remove_cvar, 1); /* in variable.c */
4429 rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
4430 rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
4431 rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
4432 rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); /* in variable.c */
4433 rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); /* in variable.c */
4434 rb_define_method(rb_cModule, "deprecate_constant", rb_mod_deprecate_constant, -1); /* in variable.c */
4435 rb_define_method(rb_cModule, "singleton_class?", rb_mod_singleton_p, 0);
4436
4437 rb_define_method(rb_cClass, "allocate", rb_class_alloc_m, 0);
4439 rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
4441 rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
4442 rb_undef_method(rb_cClass, "extend_object");
4443 rb_undef_method(rb_cClass, "append_features");
4444 rb_undef_method(rb_cClass, "prepend_features");
4445
4447 rb_cTrueClass_to_s = rb_fstring_enc_lit("true", rb_usascii_encoding());
4448 rb_gc_register_mark_object(rb_cTrueClass_to_s);
4449 rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
4450 rb_define_alias(rb_cTrueClass, "inspect", "to_s");
4451 rb_define_method(rb_cTrueClass, "&", true_and, 1);
4452 rb_define_method(rb_cTrueClass, "|", true_or, 1);
4453 rb_define_method(rb_cTrueClass, "^", true_xor, 1);
4457
4458 rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
4459 rb_cFalseClass_to_s = rb_fstring_enc_lit("false", rb_usascii_encoding());
4460 rb_gc_register_mark_object(rb_cFalseClass_to_s);
4461 rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
4462 rb_define_alias(rb_cFalseClass, "inspect", "to_s");
4463 rb_define_method(rb_cFalseClass, "&", false_and, 1);
4469}
4470
4471#include "kernel.rbinc"
4472
4473void
4475{
4476 id_dig = rb_intern_const("dig");
4477 InitVM(Object);
4478}
4479
VALUE rb_check_to_array(VALUE ary)
Definition: array.c:994
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:1301
VALUE rb_ary_new(void)
Definition: array.c:749
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:988
VALUE rb_ary_at(VALUE ary, VALUE pos)
Definition: array.c:1855
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy iff RUBY_DEBUG is truthy.
Definition: assert.h:177
#define ALWAYS_INLINE(x)
Definition: attributes.h:86
#define PUREFUNC(x)
Definition: attributes.h:54
#define NORETURN(x)
Definition: attributes.h:152
#define FUNC_MINIMIZED(x)
Definition: attributes.h:126
#define UNREACHABLE_RETURN
Definition: assume.h:31
VALUE rb_mod_const_missing(VALUE, VALUE)
Definition: variable.c:1960
#define rb_category_warn(category,...)
Definition: bigdecimal.h:163
VALUE rb_dbl2big(double d)
Definition: bignum.c:5248
VALUE rb_str_convert_to_inum(VALUE str, int base, int badcheck, int raise_exception)
Definition: bignum.c:4252
#define OBJ_BUILTIN_TYPE(obj)
Definition: compilers.h:68
#define id_to_f
Definition: complex.c:55
VALUE rb_mod_public_constant(int argc, const VALUE *argv, VALUE obj)
Definition: variable.c:3255
VALUE rb_const_source_location(VALUE, ID)
Definition: variable.c:2694
VALUE rb_mod_private_constant(int argc, const VALUE *argv, VALUE obj)
Definition: variable.c:3241
VALUE rb_const_source_location_at(VALUE, ID)
Definition: variable.c:2700
VALUE rb_mod_deprecate_constant(int argc, const VALUE *argv, VALUE obj)
Definition: variable.c:3281
Our own, locale independent, character handling routines.
#define ISSPACE
Definition: ctype.h:38
#define ISDIGIT
Definition: ctype.h:43
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:653
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
Definition: cxxanyargs.hpp:660
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
Definition: cxxanyargs.hpp:678
#define mod(x, y)
Definition: date_strftime.c:28
#define recur(fmt)
struct RIMemo * ptr
Definition: debug.c:88
#define MJIT_FUNC_EXPORTED
Definition: dllexport.h:55
#define RFLOAT_VALUE
Definition: double.h:28
#define DBL2NUM
Definition: double.h:29
rb_encoding * rb_default_internal_encoding(void)
Definition: encoding.c:1734
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:1070
rb_encoding * rb_default_external_encoding(void)
Definition: encoding.c:1647
rb_encoding * rb_usascii_encoding(void)
Definition: encoding.c:1549
big_t * num
Definition: enough.c:232
#define sym(name)
Definition: enumerator.c:4007
uint8_t len
Definition: escape.c:17
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
#define numberof(array)
Definition: etc.c:649
void rb_obj_call_init_kw(VALUE obj, int argc, const VALUE *argv, int kw_splat)
Definition: eval.c:1717
#define CLASS_OR_MODULE_P(obj)
Definition: eval.c:60
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
#define FL_SINGLETON
Definition: fl_type.h:49
#define FL_EXIVAR
Definition: fl_type.h:58
#define FL_FREEZE
Definition: fl_type.h:59
#define PRIsVALUE
Definition: function.c:10
void rb_copy_wb_protected_attribute(VALUE dest, VALUE obj)
Definition: gc.c:7876
VALUE rb_class_allocate_instance(VALUE klass)
Definition: gc.c:2482
void rb_gc_copy_finalizer(VALUE dest, VALUE obj)
Definition: gc.c:3612
void rb_gc_register_mark_object(VALUE obj)
Inform the garbage collector that object is a live Ruby object that should not be moved.
Definition: gc.c:8022
#define rb_obj_instance_variables(object)
Definition: generator.h:20
#define rb_intern_str(string)
Definition: generator.h:16
VALUE rb_cRational
Definition: rational.c:43
VALUE rb_cInteger
Definition: numeric.c:191
#define CLASS_OF
Definition: globals.h:153
VALUE rb_cNumeric
Definition: numeric.c:189
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1489
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:962
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:748
VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1597
VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1563
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1924
void Init_class_hierarchy(void)
Definition: class.c:648
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
Definition: class.c:466
VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1582
VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1651
VALUE rb_module_new(void)
Definition: class.c:856
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1474
void rb_check_inheritable(VALUE super)
Ensures a class can be derived from super.
Definition: class.c:231
VALUE rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1527
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition: class.c:213
VALUE rb_define_module(const char *name)
Definition: class.c:871
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attach a object to a singleton class.
Definition: class.c:530
VALUE rb_mod_included_modules(VALUE mod)
Definition: class.c:1233
VALUE rb_mod_ancestors(VALUE mod)
Definition: class.c:1301
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition: class.c:722
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Definition: class.c:1269
VALUE rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1512
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
Definition: class.c:359
VALUE rb_make_metaclass(VALUE obj, VALUE unused)
Definition: class.c:679
VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1612
VALUE rb_extract_keywords(VALUE *orighash)
Definition: class.c:2067
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1777
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1999
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:2296
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:935
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:2085
#define OBJ_FROZEN
Definition: fl_type.h:136
#define OBJ_FREEZE
Definition: fl_type.h:134
#define FL_TEST
Definition: fl_type.h:130
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
void rb_warn_deprecated_to_remove(const char *fmt, const char *removal,...)
Definition: error.c:496
void rb_bug(const char *fmt,...)
Definition: error.c:768
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
Definition: eval.c:1925
VALUE rb_eTypeError
Definition: error.c:1057
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
Definition: eval.c:1105
void rb_invalid_str(const char *str, const char *type)
Definition: error.c:2045
void rb_category_warning(rb_warning_category_t category, const char *fmt,...)
Definition: error.c:450
VALUE rb_eArgError
Definition: error.c:1058
bool rb_warning_category_enabled_p(rb_warning_category_t category)
Definition: error.c:182
void rb_warning(const char *fmt,...)
Definition: error.c:439
VALUE rb_cClass
Class class.
Definition: object.c:51
VALUE rb_class_superclass(VALUE klass)
Returns the superclass of klass.
Definition: object.c:1974
VALUE rb_obj_taint(VALUE obj)
call-seq: obj.taint -> obj
Definition: object.c:1020
VALUE rb_obj_trust(VALUE obj)
call-seq: obj.trust -> obj
Definition: object.c:1078
VALUE rb_class_get_superclass(VALUE klass)
Returns the superclass of klass The return value might be an iclass of a module, unlike rb_class_supe...
Definition: object.c:1999
VALUE rb_convert_type(VALUE val, int type, const char *tname, const char *method)
Converts an object into another type.
Definition: object.c:2930
VALUE rb_obj_not_equal(VALUE obj1, VALUE obj2)
Definition: object.c:220
#define case_equal
call-seq: obj === other -> true or false
Definition: object.c:141
VALUE rb_Float(VALUE val)
Equivalent to Kernel#Float in Ruby.
Definition: object.c:3531
VALUE rb_mKernel
Kernel module.
Definition: object.c:48
VALUE rb_check_to_int(VALUE val)
Tries to convert val into Integer.
Definition: object.c:3066
VALUE rb_obj_reveal(VALUE obj, VALUE klass)
Make a hidden object visible again.
Definition: object.c:109
#define opts_exception_p(opts)
Definition: object.c:3166
VALUE rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
Tries to convert an object into another type.
Definition: object.c:2971
VALUE rb_obj_dig(int argc, VALUE *argv, VALUE self, VALUE notfound)
Definition: object.c:3812
#define false_or
Definition: object.c:1355
VALUE rb_cObject
Object class.
Definition: object.c:49
VALUE rb_any_to_s(VALUE obj)
Default implementation of #to_s.
Definition: object.c:561
#define id_for_var(obj, name, type)
Definition: object.c:2011
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of klass.
Definition: object.c:1900
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates and initializes an instance of klass.
Definition: object.c:1953
#define false_xor
Definition: object.c:1368
VALUE rb_class_new_instance_kw(int argc, const VALUE *argv, VALUE klass, int kw_splat)
Definition: object.c:1929
int rb_opts_exception_p(VALUE opts, int default_value)
Definition: object.c:3157
#define try_to_int(val, mid, raise)
Definition: object.c:3001
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition: object.c:92
double rb_num_to_dbl(VALUE val)
Definition: object.c:3593
int rb_bool_expected(VALUE obj, const char *flagname)
Definition: object.c:3144
VALUE rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
Definition: object.c:1918
#define IMPLICIT_CONVERSIONS
Definition: object.c:2851
VALUE rb_check_to_float(VALUE val)
Tries to convert an object into Float.
Definition: object.c:3576
VALUE rb_cNilClass
NilClass class.
Definition: object.c:53
VALUE rb_Hash(VALUE val)
Equivalent to Kernel#Hash in Ruby.
Definition: object.c:3748
void rb_obj_infect(VALUE victim, VALUE carrier)
Does nothing.
Definition: object.c:1089
VALUE rb_obj_frozen_p(VALUE obj)
Definition: object.c:1113
VALUE rb_obj_init_copy(VALUE obj, VALUE orig)
Default implementation of #initialize_copy.
Definition: object.c:516
VALUE rb_obj_equal(VALUE obj1, VALUE obj2)
Definition: object.c:197
int rb_eql(VALUE obj1, VALUE obj2)
Determines if obj1 and obj2 are equal in terms of Object::eql?.
Definition: object.c:180
double rb_str_to_dbl(VALUE str, int badcheck)
Parses a string representation of a floating point number.
Definition: object.c:3409
VALUE rb_Integer(VALUE val)
Equivalent to Kernel#Integer in Ruby.
Definition: object.c:3138
VALUE rb_cFalseClass
FalseClass class.
Definition: object.c:55
VALUE rb_Array(VALUE val)
Equivalent to Kernel#Array in Ruby.
Definition: object.c:3705
void rb_undefined_alloc(VALUE klass)
Definition: object.c:1803
VALUE rb_obj_class(VALUE obj)
Definition: object.c:245
#define wrong_constant_name
Definition: object.c:2008
VALUE rb_obj_dup(VALUE obj)
Equivalent to Object#dup in Ruby.
Definition: object.c:467
VALUE rb_inspect(VALUE obj)
Convenient wrapper of Object::inspect.
Definition: object.c:585
VALUE rb_obj_size(VALUE self, VALUE args, VALUE obj)
Definition: object.c:499
VALUE rb_check_convert_type_with_id(VALUE, int, const char *, ID)
Definition: object.c:2987
VALUE rb_cBasicObject
BasicObject class.
Definition: object.c:47
VALUE rb_cModule
Module class.
Definition: object.c:50
VALUE rb_obj_untrust(VALUE obj)
call-seq: obj.untrust -> obj
Definition: object.c:1063
VALUE rb_class_inherited_p(VALUE mod, VALUE arg)
Determines if mod inherits arg.
Definition: object.c:1578
VALUE rb_obj_is_instance_of(VALUE obj, VALUE c)
Determines if obj is an instance of c.
Definition: object.c:707
VALUE rb_convert_type_with_id(VALUE v, int t, const char *nam, ID mid)
Definition: object.c:2944
VALUE rb_class_real(VALUE cl)
Looks up the nearest ancestor of cl, skipping singleton classes or module inclusions.
Definition: object.c:235
VALUE rb_obj_init_dup_clone(VALUE obj, VALUE orig)
Default implementation of #initialize_dup.
Definition: object.c:533
VALUE rb_to_float(VALUE val)
Converts a Numeric object into Float.
Definition: object.c:3559
double rb_num2dbl(VALUE val)
Converts a Numeric object to double.
Definition: object.c:3635
VALUE rb_equal(VALUE obj1, VALUE obj2)
This function is an optimized version of calling #==.
Definition: object.c:157
VALUE rb_obj_clone(VALUE obj)
Almost same as Object::clone.
Definition: object.c:457
VALUE rb_obj_is_kind_of(VALUE obj, VALUE c)
Determines if obj is a kind of c.
Definition: object.c:724
double rb_cstr_to_dbl(const char *p, int badcheck)
Parses a string representation of a floating point number.
Definition: object.c:3357
VALUE rb_obj_not(VALUE obj)
Definition: object.c:210
VALUE rb_obj_freeze(VALUE obj)
Make the object unmodifiable.
Definition: object.c:1101
VALUE rb_check_to_integer(VALUE val, const char *method)
Tries to convert val into Integer.
Definition: object.c:3029
VALUE rb_immutable_obj_clone(int, VALUE *, VALUE)
Definition: object.c:360
VALUE rb_class_search_ancestor(VALUE klass, VALUE super)
Definition: object.c:745
VALUE rb_obj_untrusted(VALUE obj)
call-seq: obj.untrusted? -> false
Definition: object.c:1049
VALUE rb_false(VALUE obj)
Definition: object.c:1395
VALUE rb_String(VALUE val)
Equivalent to Kernel#String in Ruby.
Definition: object.c:3673
VALUE rb_str_escape(VALUE str)
Definition: string.c:6135
VALUE rb_obj_hash(VALUE obj)
Definition: hash.c:305
VALUE rb_cTrueClass
TrueClass class.
Definition: object.c:54
VALUE rb_obj_untaint(VALUE obj)
call-seq: obj.untaint -> obj
Definition: object.c:1035
VALUE rb_obj_tainted(VALUE obj)
call-seq: obj.tainted? -> false
Definition: object.c:1006
void rb_obj_copy_ivar(VALUE dest, VALUE obj)
Definition: object.c:275
VALUE rb_to_int(VALUE val)
Converts val into Integer.
Definition: object.c:3051
void Init_Object(void)
Definition: object.c:4474
VALUE rb_obj_setup(VALUE obj, VALUE klass, VALUE type)
Fills common (RBasic) fields in obj.
Definition: object.c:126
unsigned short prefix[65536]
Definition: gun.c:163
VALUE rb_check_hash_type(VALUE hash)
Definition: hash.c:1860
VALUE rb_hash_aref(VALUE hash, VALUE key)
Definition: hash.c:2046
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:2901
VALUE rb_hash_new(void)
Definition: hash.c:1538
#define rb_enc_asciicompat(enc)
Definition: encoding.h:236
int rb_enc_str_asciionly_p(VALUE)
Definition: string.c:739
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Definition: symbol.c:1155
#define FIXABLE
Definition: fixnum.h:25
Thin wrapper to ruby/config.h.
@ RB_WARN_CATEGORY_DEPRECATED
Definition: error.h:34
VALUE rb_funcallv_kw(VALUE, ID, int, const VALUE *, int)
Definition: vm_eval.c:1030
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:1077
Defines RBIMPL_HAS_BUILTIN.
#define rb_ary_new3
Definition: array.h:73
#define rb_ary_new2
Definition: array.h:72
#define rb_check_frozen
Definition: error.h:72
#define rb_check_arity
Definition: error.h:34
int rb_is_instance_id(ID)
Definition: symbol.c:1022
int rb_is_local_id(ID)
Definition: symbol.c:1034
int rb_is_const_id(ID)
Definition: symbol.c:1004
ID rb_id_attrset(ID)
Definition: symbol.c:113
VALUE rb_rational_num(VALUE rat)
Definition: rational.c:1977
VALUE rb_rational_den(VALUE rat)
Definition: rational.c:1983
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:3217
#define rb_str_cat2
Definition: string.h:285
VALUE rb_check_string_type(VALUE)
Definition: string.c:2462
#define rb_usascii_str_new2
Definition: string.h:282
VALUE rb_str_subseq(VALUE, long, long)
Definition: string.c:2624
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:3118
VALUE rb_str_intern(VALUE)
Definition: symbol.c:840
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1529
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:5360
VALUE rb_class_name(VALUE)
Definition: variable.c:293
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:2624
VALUE rb_mod_class_variables(int, const VALUE *, VALUE)
Definition: variable.c:3512
void rb_copy_generic_ivar(VALUE, VALUE)
Definition: variable.c:1638
VALUE rb_cvar_defined(VALUE, ID)
Definition: variable.c:3387
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1234
st_index_t rb_ivar_count(VALUE)
Definition: variable.c:1733
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:2934
void rb_cvar_set(VALUE, ID, VALUE)
Definition: variable.c:3346
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:2630
VALUE rb_const_get_from(VALUE, ID)
Definition: variable.c:2618
VALUE rb_ivar_defined(VALUE, ID)
Definition: variable.c:1510
VALUE rb_obj_remove_instance_variable(VALUE, VALUE)
Definition: variable.c:1856
VALUE rb_cvar_get(VALUE, ID)
Definition: variable.c:3371
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:3003
VALUE rb_mod_remove_const(VALUE, VALUE)
Definition: variable.c:2716
VALUE rb_mod_constants(int, const VALUE *, VALUE)
Definition: variable.c:2872
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1242
int rb_const_defined_from(VALUE, ID)
Definition: variable.c:2922
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1493
VALUE rb_mod_remove_cvar(VALUE, VALUE)
Definition: variable.c:3547
VALUE rb_mod_name(VALUE)
Definition: variable.c:118
int rb_const_defined(VALUE, ID)
Definition: variable.c:2928
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:1508
int rb_obj_respond_to(VALUE, ID, int)
Definition: vm_method.c:2545
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:619
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_mod_module_exec(int, const VALUE *, VALUE)
Definition: vm_eval.c:2198
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:954
rb_alloc_func_t rb_get_alloc_func(VALUE)
Definition: vm_method.c:960
VALUE(* rb_alloc_func_t)(VALUE)
Definition: vm.h:50
#define ID2SYM
Definition: symbol.h:44
const char * rb_id2name(ID)
Definition: symbol.c:944
ID rb_intern(const char *)
Definition: symbol.c:785
ID rb_check_id(volatile VALUE *)
Returns ID for the given name if it is interned already, or 0.
Definition: symbol.c:1069
#define CONST_ID
Definition: symbol.h:47
#define isfinite(x)
Definition: missing.h:187
double nan(const char *)
Definition: nan.c:7
#define strtod(s, e)
Definition: util.h:45
Internal header aggregating init functions.
#define NUM2INT
Definition: int.h:44
Internal header for Array.
Internal header for Class.
#define RCLASS_ORIGIN(c)
Definition: class.h:87
#define RCLASS_M_TBL(c)
Definition: class.h:80
Internal header for Numeric.
Internal header for Object.
#define rb_fstring_enc_lit(str, enc)
Definition: string.h:80
VALUE rb_fstring_new(const char *ptr, long len)
Definition: string.c:446
Internal header for Struct.
VALUE rb_struct_lookup(VALUE s, VALUE idx)
Definition: struct.c:1136
int rb_is_const_name(VALUE name)
Definition: symbol.c:1217
int rb_is_const_sym(VALUE sym)
Definition: symbol.c:1046
int rb_is_local_name(VALUE name)
Definition: symbol.c:1235
VALUE rb_const_missing(VALUE klass, VALUE name)
Definition: variable.c:1915
VALUE rb_eql_opt(VALUE obj1, VALUE obj2)
VALUE rb_equal_opt(VALUE obj1, VALUE obj2)
#define RUBY_DTRACE_CREATE_HOOK(name, arg)
Definition: vm.h:120
VALUE rb_check_funcall_with_hook_kw(VALUE recv, ID mid, int argc, const VALUE *argv, rb_check_funcall_hook *hook, VALUE arg, int kw_splat)
Definition: vm_eval.c:653
#define rb_funcallv(...)
Definition: internal.h:77
#define rb_method_basic_definition_p(...)
Definition: internal.h:78
voidpf void * buf
Definition: ioapi.h:138
#define INT2FIX
Definition: long.h:48
#define LONG2FIX
Definition: long.h:49
Internal header for Math.
#define T_MASK
Definition: md5.c:131
#define MEMCPY(p1, p2, type, n)
Definition: memory.h:129
#define ALLOCV
Definition: memory.h:138
#define ALLOC_N
Definition: memory.h:133
#define ALLOCV_END
Definition: memory.h:140
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
void rb_ivar_foreach(VALUE q, int_type *w, VALUE e)
Iteration over each instance variable of the object.
Definition: cxxanyargs.hpp:496
const int id
Definition: nkf.c:209
const char * name
Definition: nkf.c:208
#define TRUE
Definition: nkf.h:175
#define FALSE
Definition: nkf.h:174
#define id_eq
Definition: numeric.c:186
#define DBL_DIG
Definition: numeric.c:67
#define OutOfRange()
#define M(n)
#define RARRAY_LEN
Definition: rarray.h:52
#define RBASIC(obj)
Definition: rbasic.h:34
#define RBASIC_CLASS
Definition: rbasic.h:35
#define RCLASS_SUPER
Definition: rclass.h:33
#define RCLASS(obj)
Definition: rclass.h:31
#define NULL
Definition: regenc.h:69
#define ROBJECT(obj)
Definition: robject.h:37
#define ROBJECT_EMBED_LEN_MAX
Definition: robject.h:38
#define ROBJECT_EMBED
Definition: robject.h:39
#define StringValue(v)
Definition: rstring.h:50
#define StringValuePtr(v)
Definition: rstring.h:51
const char * rb_obj_classname(VALUE)
Definition: variable.c:308
#define InitVM(ext)
Definition: ruby.h:112
const char * rb_class2name(VALUE)
Definition: variable.c:299
int argc
Definition: ruby.c:240
char ** argv
Definition: ruby.c:241
#define RB_INTEGER_TYPE_P(obj)
Definition: ruby_missing.h:15
#define RB_PASS_KEYWORDS
Definition: scan_args.h:47
#define RB_PASS_CALLED_KEYWORDS
Definition: scan_args.h:48
#define RB_NO_KEYWORDS
Definition: scan_args.h:46
unsigned int uint32_t
Definition: sha2.h:101
#define Qundef
#define SPECIAL_CONST_P
#define Qtrue
#define RTEST
#define Qnil
#define Qfalse
#define NIL_P
#define FIXNUM_P
#define f
VALUE rb_str_catf(VALUE, const char *,...)
Definition: sprintf.c:1243
VALUE rb_f_sprintf(int, const VALUE *)
Definition: sprintf.c:208
VALUE rb_sprintf(const char *,...)
Definition: sprintf.c:1203
@ ST_CONTINUE
Definition: st.h:99
unsigned long st_data_t
Definition: st.h:22
#define _(args)
Definition: stdarg.h:31
Definition: blast.c:41
void error(const char *msg)
Definition: untgz.c:593
unsigned long VALUE
Definition: value.h:38
unsigned long ID
Definition: value.h:39
#define T_COMPLEX
Definition: value_type.h:58
#define TYPE(_)
Definition: value_type.h:105
#define T_STRING
Definition: value_type.h:77
#define T_FLOAT
Definition: value_type.h:63
#define T_BIGNUM
Definition: value_type.h:56
#define T_STRUCT
Definition: value_type.h:78
#define T_DATA
Definition: value_type.h:59
#define T_NONE
Definition: value_type.h:73
#define T_MODULE
Definition: value_type.h:69
#define T_RATIONAL
Definition: value_type.h:75
#define T_ICLASS
Definition: value_type.h:65
#define T_HASH
Definition: value_type.h:64
#define T_ARRAY
Definition: value_type.h:55
#define T_OBJECT
Definition: value_type.h:74
#define T_SYMBOL
Definition: value_type.h:79
#define T_CLASS
Definition: value_type.h:57
#define BUILTIN_TYPE
Definition: value_type.h:84
#define SYMBOL_P
Definition: value_type.h:87
#define rb_id2str(id)
Definition: vm_backtrace.c:30