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