Ruby  2.5.0dev(2017-10-22revision60238)
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 "internal.h"
15 #include "ruby/st.h"
16 #include "ruby/util.h"
17 #include <stdio.h>
18 #include <errno.h>
19 #include <ctype.h>
20 #include <math.h>
21 #include <float.h>
22 #include "constant.h"
23 #include "id.h"
24 #include "probes.h"
25 
44 #define id_eq idEq
45 #define id_eql idEqlP
46 #define id_match idEqTilde
47 #define id_inspect idInspect
48 #define id_init_copy idInitialize_copy
49 #define id_init_clone idInitialize_clone
50 #define id_init_dup idInitialize_dup
51 #define id_const_missing idConst_missing
52 
53 #define CLASS_OR_MODULE_P(obj) \
54  (!SPECIAL_CONST_P(obj) && \
55  (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
56 
71 VALUE
73 {
74  if (!SPECIAL_CONST_P(obj)) {
75  RBASIC_CLEAR_CLASS(obj);
76  }
77  return obj;
78 }
79 
88 VALUE
90 {
91  if (!SPECIAL_CONST_P(obj)) {
92  RBASIC_SET_CLASS(obj, klass);
93  }
94  return obj;
95 }
96 
105 VALUE
106 rb_obj_setup(VALUE obj, VALUE klass, VALUE type)
107 {
108  RBASIC(obj)->flags = type;
109  RBASIC_SET_CLASS(obj, klass);
110  return obj;
111 }
112 
125 VALUE
126 rb_equal(VALUE obj1, VALUE obj2)
127 {
128  VALUE result;
129 
130  if (obj1 == obj2) return Qtrue;
131  result = rb_equal_opt(obj1, obj2);
132  if (result == Qundef) {
133  result = rb_funcall(obj1, id_eq, 1, obj2);
134  }
135  if (RTEST(result)) return Qtrue;
136  return Qfalse;
137 }
138 
148 int
149 rb_eql(VALUE obj1, VALUE obj2)
150 {
151  VALUE result;
152 
153  if (obj1 == obj2) return Qtrue;
154  result = rb_eql_opt(obj1, obj2);
155  if (result == Qundef) {
156  result = rb_funcall(obj1, id_eql, 1, obj2);
157  }
158  if (RTEST(result)) return Qtrue;
159  return Qfalse;
160 }
161 
200 VALUE
201 rb_obj_equal(VALUE obj1, VALUE obj2)
202 {
203  if (obj1 == obj2) return Qtrue;
204  return Qfalse;
205 }
206 
207 VALUE rb_obj_hash(VALUE obj);
208 
219 VALUE
220 rb_obj_not(VALUE obj)
221 {
222  return RTEST(obj) ? Qfalse : Qtrue;
223 }
224 
235 VALUE
237 {
238  VALUE result = rb_funcall(obj1, id_eq, 1, obj2);
239  return RTEST(result) ? Qfalse : Qtrue;
240 }
241 
250 VALUE
252 {
253  while (cl &&
254  ((RBASIC(cl)->flags & FL_SINGLETON) || BUILTIN_TYPE(cl) == T_ICLASS)) {
255  cl = RCLASS_SUPER(cl);
256  }
257  return cl;
258 }
259 
276 VALUE
278 {
279  return rb_class_real(CLASS_OF(obj));
280 }
281 
282 /*
283  * call-seq:
284  * obj.singleton_class -> class
285  *
286  * Returns the singleton class of <i>obj</i>. This method creates
287  * a new singleton class if <i>obj</i> does not have one.
288  *
289  * If <i>obj</i> is <code>nil</code>, <code>true</code>, or
290  * <code>false</code>, it returns NilClass, TrueClass, or FalseClass,
291  * respectively.
292  * If <i>obj</i> is an Integer, a Float or a Symbol, it raises a TypeError.
293  *
294  * Object.new.singleton_class #=> #<Class:#<Object:0xb7ce1e24>>
295  * String.singleton_class #=> #<Class:String>
296  * nil.singleton_class #=> NilClass
297  */
298 
299 static VALUE
300 rb_obj_singleton_class(VALUE obj)
301 {
302  return rb_singleton_class(obj);
303 }
304 
306 void
308 {
309  if (!(RBASIC(dest)->flags & ROBJECT_EMBED) && ROBJECT_IVPTR(dest)) {
310  xfree(ROBJECT_IVPTR(dest));
311  ROBJECT(dest)->as.heap.ivptr = 0;
312  ROBJECT(dest)->as.heap.numiv = 0;
313  ROBJECT(dest)->as.heap.iv_index_tbl = 0;
314  }
315  if (RBASIC(obj)->flags & ROBJECT_EMBED) {
316  MEMCPY(ROBJECT(dest)->as.ary, ROBJECT(obj)->as.ary, VALUE, ROBJECT_EMBED_LEN_MAX);
317  RBASIC(dest)->flags |= ROBJECT_EMBED;
318  }
319  else {
320  uint32_t len = ROBJECT(obj)->as.heap.numiv;
321  VALUE *ptr = 0;
322  if (len > 0) {
323  ptr = ALLOC_N(VALUE, len);
324  MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
325  }
326  ROBJECT(dest)->as.heap.ivptr = ptr;
327  ROBJECT(dest)->as.heap.numiv = len;
328  ROBJECT(dest)->as.heap.iv_index_tbl = ROBJECT(obj)->as.heap.iv_index_tbl;
329  RBASIC(dest)->flags &= ~ROBJECT_EMBED;
330  }
331 }
332 
333 static void
334 init_copy(VALUE dest, VALUE obj)
335 {
336  if (OBJ_FROZEN(dest)) {
337  rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
338  }
339  RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
340  RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR|FL_TAINT);
342  rb_copy_generic_ivar(dest, obj);
343  rb_gc_copy_finalizer(dest, obj);
344  if (RB_TYPE_P(obj, T_OBJECT)) {
345  rb_obj_copy_ivar(dest, obj);
346  }
347 }
348 
349 static int freeze_opt(int argc, VALUE *argv);
350 static VALUE immutable_obj_clone(VALUE obj, int kwfreeze);
351 static VALUE mutable_obj_clone(VALUE obj, int kwfreeze);
352 PUREFUNC(static inline int special_object_p(VALUE obj));
353 static inline int
354 special_object_p(VALUE obj)
355 {
356  if (SPECIAL_CONST_P(obj)) return TRUE;
357  switch (BUILTIN_TYPE(obj)) {
358  case T_BIGNUM:
359  case T_FLOAT:
360  case T_SYMBOL:
361  case T_RATIONAL:
362  case T_COMPLEX:
363  /* not a comprehensive list */
364  return TRUE;
365  default:
366  return FALSE;
367  }
368 }
369 
370 /*
371  * call-seq:
372  * obj.clone(freeze: true) -> an_object
373  *
374  * Produces a shallow copy of <i>obj</i>---the instance variables of
375  * <i>obj</i> are copied, but not the objects they reference.
376  * <code>clone</code> copies the frozen (unless :freeze keyword argument
377  * is given with a false value) and tainted state of <i>obj</i>.
378  * See also the discussion under <code>Object#dup</code>.
379  *
380  * class Klass
381  * attr_accessor :str
382  * end
383  * s1 = Klass.new #=> #<Klass:0x401b3a38>
384  * s1.str = "Hello" #=> "Hello"
385  * s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello">
386  * s2.str[1,4] = "i" #=> "i"
387  * s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
388  * s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
389  *
390  * This method may have class-specific behavior. If so, that
391  * behavior will be documented under the #+initialize_copy+ method of
392  * the class.
393  */
394 
395 static VALUE
396 rb_obj_clone2(int argc, VALUE *argv, VALUE obj)
397 {
398  int kwfreeze = freeze_opt(argc, argv);
399  if (!special_object_p(obj))
400  return mutable_obj_clone(obj, kwfreeze);
401  return immutable_obj_clone(obj, kwfreeze);
402 }
403 
405 VALUE
407 {
408  int kwfreeze = freeze_opt(argc, argv);
409  return immutable_obj_clone(obj, kwfreeze);
410 }
411 
412 static int
413 freeze_opt(int argc, VALUE *argv)
414 {
415  static ID keyword_ids[1];
416  VALUE opt;
417  VALUE kwfreeze;
418 
419  if (!keyword_ids[0]) {
420  CONST_ID(keyword_ids[0], "freeze");
421  }
422  rb_scan_args(argc, argv, "0:", &opt);
423  if (!NIL_P(opt)) {
424  rb_get_kwargs(opt, keyword_ids, 0, 1, &kwfreeze);
425  if (kwfreeze == Qfalse) return FALSE;
426  if (kwfreeze != Qundef && kwfreeze != Qtrue) {
427  rb_raise(rb_eArgError, "unexpected value for freeze: %"PRIsVALUE,
428  rb_obj_class(kwfreeze));
429  }
430  }
431  return TRUE;
432 }
433 
434 static VALUE
435 immutable_obj_clone(VALUE obj, int kwfreeze)
436 {
437  if (!kwfreeze)
438  rb_raise(rb_eArgError, "can't unfreeze %"PRIsVALUE,
439  rb_obj_class(obj));
440  return obj;
441 }
442 
443 static VALUE
444 mutable_obj_clone(VALUE obj, int kwfreeze)
445 {
446  VALUE clone, singleton;
447 
448  clone = rb_obj_alloc(rb_obj_class(obj));
449  RBASIC(clone)->flags &= (FL_TAINT|FL_PROMOTED0|FL_PROMOTED1);
450  RBASIC(clone)->flags |= RBASIC(obj)->flags & ~(FL_PROMOTED0|FL_PROMOTED1|FL_FREEZE|FL_FINALIZE);
451 
452  singleton = rb_singleton_class_clone_and_attach(obj, clone);
453  RBASIC_SET_CLASS(clone, singleton);
454  if (FL_TEST(singleton, FL_SINGLETON)) {
455  rb_singleton_class_attached(singleton, clone);
456  }
457 
458  init_copy(clone, obj);
459  rb_funcall(clone, id_init_clone, 1, obj);
460 
461  if (kwfreeze) {
462  RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
463  }
464 
465  return clone;
466 }
467 
474 VALUE
476 {
477  if (special_object_p(obj)) return obj;
478  return mutable_obj_clone(obj, Qtrue);
479 }
480 
525 VALUE
527 {
528  VALUE dup;
529 
530  if (special_object_p(obj)) {
531  return obj;
532  }
533  dup = rb_obj_alloc(rb_obj_class(obj));
534  init_copy(dup, obj);
535  rb_funcall(dup, id_init_dup, 1, obj);
536 
537  return dup;
538 }
539 
540 /*
541  * call-seq:
542  * obj.itself -> obj
543  *
544  * Returns the receiver.
545  *
546  * string = "my string"
547  * string.itself.object_id == string.object_id #=> true
548  *
549  */
550 
551 static VALUE
552 rb_obj_itself(VALUE obj)
553 {
554  return obj;
555 }
556 
557 static VALUE
558 rb_obj_size(VALUE self, VALUE args, VALUE obj)
559 {
560  return LONG2FIX(1);
561 }
562 
563 /*
564  * call-seq:
565  * obj.yield_self {|x| block } -> an_object
566  *
567  * Yields self to the block and returns the result of the block.
568  *
569  * "my string".yield_self {|s| s.upcase } #=> "MY STRING"
570  * 3.next.yield_self {|x| x**x }.to_s #=> "256"
571  *
572  */
573 
574 static VALUE
575 rb_obj_yield_self(VALUE obj)
576 {
577  RETURN_SIZED_ENUMERATOR(obj, 0, 0, rb_obj_size);
578  return rb_yield_values2(1, &obj);
579 }
580 
589 VALUE
591 {
592  if (obj == orig) return obj;
593  rb_check_frozen(obj);
594  rb_check_trusted(obj);
595  if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
596  rb_raise(rb_eTypeError, "initialize_copy should take same class object");
597  }
598  return obj;
599 }
600 
610 VALUE
612 {
613  rb_funcall(obj, id_init_copy, 1, orig);
614  return obj;
615 }
616 
630 VALUE
632 {
633  VALUE str;
634  VALUE cname = rb_class_name(CLASS_OF(obj));
635 
636  str = rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)obj);
637  OBJ_INFECT(str, obj);
638 
639  return str;
640 }
641 
655 VALUE
657 {
658  VALUE str = rb_obj_as_string(rb_funcallv(obj, id_inspect, 0, 0));
659 
661  if (enc == NULL) enc = rb_default_external_encoding();
662  if (!rb_enc_asciicompat(enc)) {
663  if (!rb_enc_str_asciionly_p(str))
664  return rb_str_escape(str);
665  return str;
666  }
667  if (rb_enc_get(str) != enc && !rb_enc_str_asciionly_p(str))
668  return rb_str_escape(str);
669  return str;
670 }
671 
672 static int
673 inspect_i(st_data_t k, st_data_t v, st_data_t a)
674 {
675  ID id = (ID)k;
676  VALUE value = (VALUE)v;
677  VALUE str = (VALUE)a;
678 
679  /* need not to show internal data */
680  if (CLASS_OF(value) == 0) return ST_CONTINUE;
681  if (!rb_is_instance_id(id)) return ST_CONTINUE;
682  if (RSTRING_PTR(str)[0] == '-') { /* first element */
683  RSTRING_PTR(str)[0] = '#';
684  rb_str_cat2(str, " ");
685  }
686  else {
687  rb_str_cat2(str, ", ");
688  }
689  rb_str_catf(str, "%"PRIsVALUE"=%+"PRIsVALUE,
690  rb_id2str(id), value);
691 
692  return ST_CONTINUE;
693 }
694 
695 static VALUE
696 inspect_obj(VALUE obj, VALUE str, int recur)
697 {
698  if (recur) {
699  rb_str_cat2(str, " ...");
700  }
701  else {
702  rb_ivar_foreach(obj, inspect_i, str);
703  }
704  rb_str_cat2(str, ">");
705  RSTRING_PTR(str)[0] = '#';
706  OBJ_INFECT(str, obj);
707 
708  return str;
709 }
710 
711 /*
712  * call-seq:
713  * obj.inspect -> string
714  *
715  * Returns a string containing a human-readable representation of <i>obj</i>.
716  * The default <code>inspect</code> shows the object's class name,
717  * an encoding of the object id, and a list of the instance variables and
718  * their values (by calling #inspect on each of them).
719  * User defined classes should override this method to provide a better
720  * representation of <i>obj</i>. When overriding this method, it should
721  * return a string whose encoding is compatible with the default external
722  * encoding.
723  *
724  * [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
725  * Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
726  *
727  * class Foo
728  * end
729  * Foo.new.inspect #=> "#<Foo:0x0300c868>"
730  *
731  * class Bar
732  * def initialize
733  * @bar = 1
734  * end
735  * end
736  * Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
737  */
738 
739 static VALUE
740 rb_obj_inspect(VALUE obj)
741 {
742  if (rb_ivar_count(obj) > 0) {
743  VALUE str;
744  VALUE c = rb_class_name(CLASS_OF(obj));
745 
746  str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void*)obj);
747  return rb_exec_recursive(inspect_obj, obj, str);
748  }
749  else {
750  return rb_any_to_s(obj);
751  }
752 }
753 
754 static VALUE
755 class_or_module_required(VALUE c)
756 {
757  if (SPECIAL_CONST_P(c)) goto not_class;
758  switch (BUILTIN_TYPE(c)) {
759  case T_MODULE:
760  case T_CLASS:
761  case T_ICLASS:
762  break;
763 
764  default:
765  not_class:
766  rb_raise(rb_eTypeError, "class or module required");
767  }
768  return c;
769 }
770 
771 static VALUE class_search_ancestor(VALUE cl, VALUE c);
772 
797 VALUE
799 {
800  c = class_or_module_required(c);
801  if (rb_obj_class(obj) == c) return Qtrue;
802  return Qfalse;
803 }
804 
805 
841 VALUE
843 {
844  VALUE cl = CLASS_OF(obj);
845 
846  c = class_or_module_required(c);
847  return class_search_ancestor(cl, RCLASS_ORIGIN(c)) ? Qtrue : Qfalse;
848 }
849 
850 static VALUE
851 class_search_ancestor(VALUE cl, VALUE c)
852 {
853  while (cl) {
854  if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
855  return cl;
856  cl = RCLASS_SUPER(cl);
857  }
858  return 0;
859 }
860 
862 VALUE
864 {
865  cl = class_or_module_required(cl);
866  c = class_or_module_required(c);
867  return class_search_ancestor(cl, RCLASS_ORIGIN(c));
868 }
869 
888 VALUE
889 rb_obj_tap(VALUE obj)
890 {
891  rb_yield(obj);
892  return obj;
893 }
894 
895 
896 /*
897  * Document-method: inherited
898  *
899  * call-seq:
900  * inherited(subclass)
901  *
902  * Callback invoked whenever a subclass of the current class is created.
903  *
904  * Example:
905  *
906  * class Foo
907  * def self.inherited(subclass)
908  * puts "New subclass: #{subclass}"
909  * end
910  * end
911  *
912  * class Bar < Foo
913  * end
914  *
915  * class Baz < Bar
916  * end
917  *
918  * <em>produces:</em>
919  *
920  * New subclass: Bar
921  * New subclass: Baz
922  */
923 
924 /* Document-method: method_added
925  *
926  * call-seq:
927  * method_added(method_name)
928  *
929  * Invoked as a callback whenever an instance method is added to the
930  * receiver.
931  *
932  * module Chatty
933  * def self.method_added(method_name)
934  * puts "Adding #{method_name.inspect}"
935  * end
936  * def self.some_class_method() end
937  * def some_instance_method() end
938  * end
939  *
940  * <em>produces:</em>
941  *
942  * Adding :some_instance_method
943  *
944  */
945 
946 /* Document-method: method_removed
947  *
948  * call-seq:
949  * method_removed(method_name)
950  *
951  * Invoked as a callback whenever an instance method is removed from the
952  * receiver.
953  *
954  * module Chatty
955  * def self.method_removed(method_name)
956  * puts "Removing #{method_name.inspect}"
957  * end
958  * def self.some_class_method() end
959  * def some_instance_method() end
960  * class << self
961  * remove_method :some_class_method
962  * end
963  * remove_method :some_instance_method
964  * end
965  *
966  * <em>produces:</em>
967  *
968  * Removing :some_instance_method
969  *
970  */
971 
972 /*
973  * Document-method: singleton_method_added
974  *
975  * call-seq:
976  * singleton_method_added(symbol)
977  *
978  * Invoked as a callback whenever a singleton method is added to the
979  * receiver.
980  *
981  * module Chatty
982  * def Chatty.singleton_method_added(id)
983  * puts "Adding #{id.id2name}"
984  * end
985  * def self.one() end
986  * def two() end
987  * def Chatty.three() end
988  * end
989  *
990  * <em>produces:</em>
991  *
992  * Adding singleton_method_added
993  * Adding one
994  * Adding three
995  *
996  */
997 
998 /*
999  * Document-method: singleton_method_removed
1000  *
1001  * call-seq:
1002  * singleton_method_removed(symbol)
1003  *
1004  * Invoked as a callback whenever a singleton method is removed from
1005  * the receiver.
1006  *
1007  * module Chatty
1008  * def Chatty.singleton_method_removed(id)
1009  * puts "Removing #{id.id2name}"
1010  * end
1011  * def self.one() end
1012  * def two() end
1013  * def Chatty.three() end
1014  * class << self
1015  * remove_method :three
1016  * remove_method :one
1017  * end
1018  * end
1019  *
1020  * <em>produces:</em>
1021  *
1022  * Removing three
1023  * Removing one
1024  */
1025 
1026 /*
1027  * Document-method: singleton_method_undefined
1028  *
1029  * call-seq:
1030  * singleton_method_undefined(symbol)
1031  *
1032  * Invoked as a callback whenever a singleton method is undefined in
1033  * the receiver.
1034  *
1035  * module Chatty
1036  * def Chatty.singleton_method_undefined(id)
1037  * puts "Undefining #{id.id2name}"
1038  * end
1039  * def Chatty.one() end
1040  * class << self
1041  * undef_method(:one)
1042  * end
1043  * end
1044  *
1045  * <em>produces:</em>
1046  *
1047  * Undefining one
1048  */
1049 
1050 /*
1051  * Document-method: extended
1052  *
1053  * call-seq:
1054  * extended(othermod)
1055  *
1056  * The equivalent of <tt>included</tt>, but for extended modules.
1057  *
1058  * module A
1059  * def self.extended(mod)
1060  * puts "#{self} extended in #{mod}"
1061  * end
1062  * end
1063  * module Enumerable
1064  * extend A
1065  * end
1066  * # => prints "A extended in Enumerable"
1067  */
1068 
1069 /*
1070  * Document-method: included
1071  *
1072  * call-seq:
1073  * included(othermod)
1074  *
1075  * Callback invoked whenever the receiver is included in another
1076  * module or class. This should be used in preference to
1077  * <tt>Module.append_features</tt> if your code wants to perform some
1078  * action when a module is included in another.
1079  *
1080  * module A
1081  * def A.included(mod)
1082  * puts "#{self} included in #{mod}"
1083  * end
1084  * end
1085  * module Enumerable
1086  * include A
1087  * end
1088  * # => prints "A included in Enumerable"
1089  */
1090 
1091 /*
1092  * Document-method: prepended
1093  *
1094  * call-seq:
1095  * prepended(othermod)
1096  *
1097  * The equivalent of <tt>included</tt>, but for prepended modules.
1098  *
1099  * module A
1100  * def self.prepended(mod)
1101  * puts "#{self} prepended to #{mod}"
1102  * end
1103  * end
1104  * module Enumerable
1105  * prepend A
1106  * end
1107  * # => prints "A prepended to Enumerable"
1108  */
1109 
1110 /*
1111  * Document-method: initialize
1112  *
1113  * call-seq:
1114  * BasicObject.new
1115  *
1116  * Returns a new BasicObject.
1117  */
1118 
1119 /*
1120  * Not documented
1121  */
1122 
1123 static VALUE
1124 rb_obj_dummy(void)
1125 {
1126  return Qnil;
1127 }
1128 
1146 VALUE
1148 {
1149  if (OBJ_TAINTED(obj))
1150  return Qtrue;
1151  return Qfalse;
1152 }
1153 
1178 VALUE
1180 {
1181  if (!OBJ_TAINTED(obj) && OBJ_TAINTABLE(obj)) {
1182  rb_check_frozen(obj);
1183  OBJ_TAINT(obj);
1184  }
1185  return obj;
1186 }
1187 
1188 
1207 VALUE
1209 {
1210  if (OBJ_TAINTED(obj)) {
1211  rb_check_frozen(obj);
1212  FL_UNSET(obj, FL_TAINT);
1213  }
1214  return obj;
1215 }
1216 
1233 VALUE
1235 {
1236  rb_warning("untrusted? is deprecated and its behavior is same as tainted?");
1237  return rb_obj_tainted(obj);
1238 }
1239 
1256 VALUE
1258 {
1259  rb_warning("untrust is deprecated and its behavior is same as taint");
1260  return rb_obj_taint(obj);
1261 }
1262 
1263 
1280 VALUE
1282 {
1283  rb_warning("trust is deprecated and its behavior is same as untaint");
1284  return rb_obj_untaint(obj);
1285 }
1286 
1295 void
1296 rb_obj_infect(VALUE victim, VALUE carrier)
1297 {
1298  OBJ_INFECT(victim, carrier);
1299 }
1300 
1330 VALUE
1332 {
1333  if (!OBJ_FROZEN(obj)) {
1334  OBJ_FREEZE(obj);
1335  if (SPECIAL_CONST_P(obj)) {
1336  rb_bug("special consts should be frozen.");
1337  }
1338  }
1339  return obj;
1340 }
1341 
1359 VALUE
1361 {
1362  return OBJ_FROZEN(obj) ? Qtrue : Qfalse;
1363 }
1364 
1365 
1366 /*
1367  * Document-class: NilClass
1368  *
1369  * The class of the singleton object <code>nil</code>.
1370  */
1371 
1372 /*
1373  * call-seq:
1374  * nil.to_i -> 0
1375  *
1376  * Always returns zero.
1377  *
1378  * nil.to_i #=> 0
1379  */
1380 
1381 
1382 static VALUE
1383 nil_to_i(VALUE obj)
1384 {
1385  return INT2FIX(0);
1386 }
1387 
1388 /*
1389  * call-seq:
1390  * nil.to_f -> 0.0
1391  *
1392  * Always returns zero.
1393  *
1394  * nil.to_f #=> 0.0
1395  */
1396 
1397 static VALUE
1398 nil_to_f(VALUE obj)
1399 {
1400  return DBL2NUM(0.0);
1401 }
1402 
1403 /*
1404  * call-seq:
1405  * nil.to_s -> ""
1406  *
1407  * Always returns the empty string.
1408  */
1409 
1410 static VALUE
1411 nil_to_s(VALUE obj)
1412 {
1413  return rb_usascii_str_new(0, 0);
1414 }
1415 
1416 /*
1417  * Document-method: to_a
1418  *
1419  * call-seq:
1420  * nil.to_a -> []
1421  *
1422  * Always returns an empty array.
1423  *
1424  * nil.to_a #=> []
1425  */
1426 
1427 static VALUE
1428 nil_to_a(VALUE obj)
1429 {
1430  return rb_ary_new2(0);
1431 }
1432 
1433 /*
1434  * Document-method: to_h
1435  *
1436  * call-seq:
1437  * nil.to_h -> {}
1438  *
1439  * Always returns an empty hash.
1440  *
1441  * nil.to_h #=> {}
1442  */
1443 
1444 static VALUE
1445 nil_to_h(VALUE obj)
1446 {
1447  return rb_hash_new();
1448 }
1449 
1450 /*
1451  * call-seq:
1452  * nil.inspect -> "nil"
1453  *
1454  * Always returns the string "nil".
1455  */
1456 
1457 static VALUE
1458 nil_inspect(VALUE obj)
1459 {
1460  return rb_usascii_str_new2("nil");
1461 }
1462 
1463 /***********************************************************************
1464  * Document-class: TrueClass
1465  *
1466  * The global value <code>true</code> is the only instance of class
1467  * <code>TrueClass</code> and represents a logically true value in
1468  * boolean expressions. The class provides operators allowing
1469  * <code>true</code> to be used in logical expressions.
1470  */
1471 
1472 
1473 /*
1474  * call-seq:
1475  * true.to_s -> "true"
1476  *
1477  * The string representation of <code>true</code> is "true".
1478  */
1479 
1480 static VALUE
1481 true_to_s(VALUE obj)
1482 {
1483  return rb_usascii_str_new2("true");
1484 }
1485 
1486 
1487 /*
1488  * call-seq:
1489  * true & obj -> true or false
1490  *
1491  * And---Returns <code>false</code> if <i>obj</i> is
1492  * <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
1493  */
1494 
1495 static VALUE
1496 true_and(VALUE obj, VALUE obj2)
1497 {
1498  return RTEST(obj2)?Qtrue:Qfalse;
1499 }
1500 
1501 /*
1502  * call-seq:
1503  * true | obj -> true
1504  *
1505  * Or---Returns <code>true</code>. As <i>obj</i> is an argument to
1506  * a method call, it is always evaluated; there is no short-circuit
1507  * evaluation in this case.
1508  *
1509  * true | puts("or")
1510  * true || puts("logical or")
1511  *
1512  * <em>produces:</em>
1513  *
1514  * or
1515  */
1516 
1517 static VALUE
1518 true_or(VALUE obj, VALUE obj2)
1519 {
1520  return Qtrue;
1521 }
1522 
1523 
1524 /*
1525  * call-seq:
1526  * true ^ obj -> !obj
1527  *
1528  * Exclusive Or---Returns <code>true</code> if <i>obj</i> is
1529  * <code>nil</code> or <code>false</code>, <code>false</code>
1530  * otherwise.
1531  */
1532 
1533 static VALUE
1534 true_xor(VALUE obj, VALUE obj2)
1535 {
1536  return RTEST(obj2)?Qfalse:Qtrue;
1537 }
1538 
1539 
1540 /*
1541  * Document-class: FalseClass
1542  *
1543  * The global value <code>false</code> is the only instance of class
1544  * <code>FalseClass</code> and represents a logically false value in
1545  * boolean expressions. The class provides operators allowing
1546  * <code>false</code> to participate correctly in logical expressions.
1547  *
1548  */
1549 
1550 /*
1551  * call-seq:
1552  * false.to_s -> "false"
1553  *
1554  * The string representation of <code>false</code> is "false".
1555  */
1556 
1557 static VALUE
1558 false_to_s(VALUE obj)
1559 {
1560  return rb_usascii_str_new2("false");
1561 }
1562 
1563 /*
1564  * call-seq:
1565  * false & obj -> false
1566  * nil & obj -> false
1567  *
1568  * And---Returns <code>false</code>. <i>obj</i> is always
1569  * evaluated as it is the argument to a method call---there is no
1570  * short-circuit evaluation in this case.
1571  */
1572 
1573 static VALUE
1574 false_and(VALUE obj, VALUE obj2)
1575 {
1576  return Qfalse;
1577 }
1578 
1579 
1580 /*
1581  * call-seq:
1582  * false | obj -> true or false
1583  * nil | obj -> true or false
1584  *
1585  * Or---Returns <code>false</code> if <i>obj</i> is
1586  * <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
1587  */
1588 
1589 static VALUE
1590 false_or(VALUE obj, VALUE obj2)
1591 {
1592  return RTEST(obj2)?Qtrue:Qfalse;
1593 }
1594 
1595 
1596 
1597 /*
1598  * call-seq:
1599  * false ^ obj -> true or false
1600  * nil ^ obj -> true or false
1601  *
1602  * Exclusive Or---If <i>obj</i> is <code>nil</code> or
1603  * <code>false</code>, returns <code>false</code>; otherwise, returns
1604  * <code>true</code>.
1605  *
1606  */
1607 
1608 static VALUE
1609 false_xor(VALUE obj, VALUE obj2)
1610 {
1611  return RTEST(obj2)?Qtrue:Qfalse;
1612 }
1613 
1614 /*
1615  * call-seq:
1616  * nil.nil? -> true
1617  *
1618  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1619  */
1620 
1621 static VALUE
1622 rb_true(VALUE obj)
1623 {
1624  return Qtrue;
1625 }
1626 
1627 /*
1628  * call-seq:
1629  * obj.nil? -> true or false
1630  *
1631  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1632  *
1633  * Object.new.nil? #=> false
1634  * nil.nil? #=> true
1635  */
1636 
1637 
1638 static VALUE
1639 rb_false(VALUE obj)
1640 {
1641  return Qfalse;
1642 }
1643 
1644 
1645 /*
1646  * call-seq:
1647  * obj =~ other -> nil
1648  *
1649  * Pattern Match---Overridden by descendants (notably
1650  * <code>Regexp</code> and <code>String</code>) to provide meaningful
1651  * pattern-match semantics.
1652  */
1653 
1654 static VALUE
1655 rb_obj_match(VALUE obj1, VALUE obj2)
1656 {
1657  return Qnil;
1658 }
1659 
1660 /*
1661  * call-seq:
1662  * obj !~ other -> true or false
1663  *
1664  * Returns true if two objects do not match (using the <i>=~</i>
1665  * method), otherwise false.
1666  */
1667 
1668 static VALUE
1669 rb_obj_not_match(VALUE obj1, VALUE obj2)
1670 {
1671  VALUE result = rb_funcall(obj1, id_match, 1, obj2);
1672  return RTEST(result) ? Qfalse : Qtrue;
1673 }
1674 
1675 
1676 /*
1677  * call-seq:
1678  * obj <=> other -> 0 or nil
1679  *
1680  * Returns 0 if +obj+ and +other+ are the same object
1681  * or <code>obj == other</code>, otherwise nil.
1682  *
1683  * The <code><=></code> is used by various methods to compare objects, for example
1684  * Enumerable#sort, Enumerable#max etc.
1685  *
1686  * Your implementation of <code><=></code> should return one of the following values: -1, 0,
1687  * 1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
1688  * 1 means self is bigger than other. Nil means the two values could not be
1689  * compared.
1690  *
1691  * When you define <code><=></code>, you can include Comparable to gain the methods
1692  * <code><=</code>, <code><</code>, <code>==</code>, <code>>=</code>, <code>></code> and <code>between?</code>.
1693  */
1694 static VALUE
1695 rb_obj_cmp(VALUE obj1, VALUE obj2)
1696 {
1697  if (obj1 == obj2 || rb_equal(obj1, obj2))
1698  return INT2FIX(0);
1699  return Qnil;
1700 }
1701 
1702 /***********************************************************************
1703  *
1704  * Document-class: Module
1705  *
1706  * A <code>Module</code> is a collection of methods and constants. The
1707  * methods in a module may be instance methods or module methods.
1708  * Instance methods appear as methods in a class when the module is
1709  * included, module methods do not. Conversely, module methods may be
1710  * called without creating an encapsulating object, while instance
1711  * methods may not. (See <code>Module#module_function</code>.)
1712  *
1713  * In the descriptions that follow, the parameter <i>sym</i> refers
1714  * to a symbol, which is either a quoted string or a
1715  * <code>Symbol</code> (such as <code>:name</code>).
1716  *
1717  * module Mod
1718  * include Math
1719  * CONST = 1
1720  * def meth
1721  * # ...
1722  * end
1723  * end
1724  * Mod.class #=> Module
1725  * Mod.constants #=> [:CONST, :PI, :E]
1726  * Mod.instance_methods #=> [:meth]
1727  *
1728  */
1729 
1730 /*
1731  * call-seq:
1732  * mod.to_s -> string
1733  *
1734  * Returns a string representing this module or class. For basic
1735  * classes and modules, this is the name. For singletons, we
1736  * show information on the thing we're attached to as well.
1737  */
1738 
1739 static VALUE
1740 rb_mod_to_s(VALUE klass)
1741 {
1742  ID id_defined_at;
1743  VALUE refined_class, defined_at;
1744 
1745  if (FL_TEST(klass, FL_SINGLETON)) {
1746  VALUE s = rb_usascii_str_new2("#<Class:");
1747  VALUE v = rb_ivar_get(klass, id__attached__);
1748 
1749  if (CLASS_OR_MODULE_P(v)) {
1750  rb_str_append(s, rb_inspect(v));
1751  }
1752  else {
1753  rb_str_append(s, rb_any_to_s(v));
1754  }
1755  rb_str_cat2(s, ">");
1756 
1757  return s;
1758  }
1759  refined_class = rb_refinement_module_get_refined_class(klass);
1760  if (!NIL_P(refined_class)) {
1761  VALUE s = rb_usascii_str_new2("#<refinement:");
1762 
1763  rb_str_concat(s, rb_inspect(refined_class));
1764  rb_str_cat2(s, "@");
1765  CONST_ID(id_defined_at, "__defined_at__");
1766  defined_at = rb_attr_get(klass, id_defined_at);
1767  rb_str_concat(s, rb_inspect(defined_at));
1768  rb_str_cat2(s, ">");
1769  return s;
1770  }
1771  return rb_str_dup(rb_class_name(klass));
1772 }
1773 
1774 /*
1775  * call-seq:
1776  * mod.freeze -> mod
1777  *
1778  * Prevents further modifications to <i>mod</i>.
1779  *
1780  * This method returns self.
1781  */
1782 
1783 static VALUE
1784 rb_mod_freeze(VALUE mod)
1785 {
1786  rb_class_name(mod);
1787  return rb_obj_freeze(mod);
1788 }
1789 
1790 /*
1791  * call-seq:
1792  * mod === obj -> true or false
1793  *
1794  * Case Equality---Returns <code>true</code> if <i>obj</i> is an
1795  * instance of <i>mod</i> or an instance of one of <i>mod</i>'s descendants.
1796  * Of limited use for modules, but can be used in <code>case</code> statements
1797  * to classify objects by class.
1798  */
1799 
1800 static VALUE
1801 rb_mod_eqq(VALUE mod, VALUE arg)
1802 {
1803  return rb_obj_is_kind_of(arg, mod);
1804 }
1805 
1826 VALUE
1828 {
1829  if (mod == arg) return Qtrue;
1830  if (!CLASS_OR_MODULE_P(arg) && !RB_TYPE_P(arg, T_ICLASS)) {
1831  rb_raise(rb_eTypeError, "compared with non class/module");
1832  }
1833  if (class_search_ancestor(mod, RCLASS_ORIGIN(arg))) {
1834  return Qtrue;
1835  }
1836  /* not mod < arg; check if mod > arg */
1837  if (class_search_ancestor(arg, mod)) {
1838  return Qfalse;
1839  }
1840  return Qnil;
1841 }
1842 
1843 /*
1844  * call-seq:
1845  * mod < other -> true, false, or nil
1846  *
1847  * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
1848  * <code>nil</code> if there's no relationship between the two.
1849  * (Think of the relationship in terms of the class definition:
1850  * "class A < B" implies "A < B".)
1851  *
1852  */
1853 
1854 static VALUE
1855 rb_mod_lt(VALUE mod, VALUE arg)
1856 {
1857  if (mod == arg) return Qfalse;
1858  return rb_class_inherited_p(mod, arg);
1859 }
1860 
1861 
1862 /*
1863  * call-seq:
1864  * mod >= other -> true, false, or nil
1865  *
1866  * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
1867  * two modules are the same. Returns
1868  * <code>nil</code> if there's no relationship between the two.
1869  * (Think of the relationship in terms of the class definition:
1870  * "class A < B" implies "B > A".)
1871  *
1872  */
1873 
1874 static VALUE
1875 rb_mod_ge(VALUE mod, VALUE arg)
1876 {
1877  if (!CLASS_OR_MODULE_P(arg)) {
1878  rb_raise(rb_eTypeError, "compared with non class/module");
1879  }
1880 
1881  return rb_class_inherited_p(arg, mod);
1882 }
1883 
1884 /*
1885  * call-seq:
1886  * mod > other -> true, false, or nil
1887  *
1888  * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
1889  * <code>nil</code> if there's no relationship between the two.
1890  * (Think of the relationship in terms of the class definition:
1891  * "class A < B" implies "B > A".)
1892  *
1893  */
1894 
1895 static VALUE
1896 rb_mod_gt(VALUE mod, VALUE arg)
1897 {
1898  if (mod == arg) return Qfalse;
1899  return rb_mod_ge(mod, arg);
1900 }
1901 
1902 /*
1903  * call-seq:
1904  * module <=> other_module -> -1, 0, +1, or nil
1905  *
1906  * Comparison---Returns -1, 0, +1 or nil depending on whether +module+
1907  * includes +other_module+, they are the same, or if +module+ is included by
1908  * +other_module+.
1909  *
1910  * Returns +nil+ if +module+ has no relationship with +other_module+, if
1911  * +other_module+ is not a module, or if the two values are incomparable.
1912  */
1913 
1914 static VALUE
1915 rb_mod_cmp(VALUE mod, VALUE arg)
1916 {
1917  VALUE cmp;
1918 
1919  if (mod == arg) return INT2FIX(0);
1920  if (!CLASS_OR_MODULE_P(arg)) {
1921  return Qnil;
1922  }
1923 
1924  cmp = rb_class_inherited_p(mod, arg);
1925  if (NIL_P(cmp)) return Qnil;
1926  if (cmp) {
1927  return INT2FIX(-1);
1928  }
1929  return INT2FIX(1);
1930 }
1931 
1932 static VALUE
1933 rb_module_s_alloc(VALUE klass)
1934 {
1935  VALUE mod = rb_module_new();
1936 
1937  RBASIC_SET_CLASS(mod, klass);
1938  return mod;
1939 }
1940 
1941 static VALUE
1942 rb_class_s_alloc(VALUE klass)
1943 {
1944  return rb_class_boot(0);
1945 }
1946 
1947 /*
1948  * call-seq:
1949  * Module.new -> mod
1950  * Module.new {|mod| block } -> mod
1951  *
1952  * Creates a new anonymous module. If a block is given, it is passed
1953  * the module object, and the block is evaluated in the context of this
1954  * module like <code>module_eval</code>.
1955  *
1956  * fred = Module.new do
1957  * def meth1
1958  * "hello"
1959  * end
1960  * def meth2
1961  * "bye"
1962  * end
1963  * end
1964  * a = "my string"
1965  * a.extend(fred) #=> "my string"
1966  * a.meth1 #=> "hello"
1967  * a.meth2 #=> "bye"
1968  *
1969  * Assign the module to a constant (name starting uppercase) if you
1970  * want to treat it like a regular module.
1971  */
1972 
1973 static VALUE
1974 rb_mod_initialize(VALUE module)
1975 {
1976  if (rb_block_given_p()) {
1977  rb_mod_module_exec(1, &module, module);
1978  }
1979  return Qnil;
1980 }
1981 
1982 /* :nodoc: */
1983 static VALUE
1984 rb_mod_initialize_clone(VALUE clone, VALUE orig)
1985 {
1986  VALUE ret;
1987  ret = rb_obj_init_dup_clone(clone, orig);
1988  if (OBJ_FROZEN(orig))
1989  rb_class_name(clone);
1990  return ret;
1991 }
1992 
1993 /*
1994  * call-seq:
1995  * Class.new(super_class=Object) -> a_class
1996  * Class.new(super_class=Object) { |mod| ... } -> a_class
1997  *
1998  * Creates a new anonymous (unnamed) class with the given superclass
1999  * (or <code>Object</code> if no parameter is given). You can give a
2000  * class a name by assigning the class object to a constant.
2001  *
2002  * If a block is given, it is passed the class object, and the block
2003  * is evaluated in the context of this class like
2004  * <code>class_eval</code>.
2005  *
2006  * fred = Class.new do
2007  * def meth1
2008  * "hello"
2009  * end
2010  * def meth2
2011  * "bye"
2012  * end
2013  * end
2014  *
2015  * a = fred.new #=> #<#<Class:0x100381890>:0x100376b98>
2016  * a.meth1 #=> "hello"
2017  * a.meth2 #=> "bye"
2018  *
2019  * Assign the class to a constant (name starting uppercase) if you
2020  * want to treat it like a regular class.
2021  */
2022 
2023 static VALUE
2024 rb_class_initialize(int argc, VALUE *argv, VALUE klass)
2025 {
2026  VALUE super;
2027 
2028  if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
2029  rb_raise(rb_eTypeError, "already initialized class");
2030  }
2031  if (argc == 0) {
2032  super = rb_cObject;
2033  }
2034  else {
2035  rb_scan_args(argc, argv, "01", &super);
2036  rb_check_inheritable(super);
2037  if (super != rb_cBasicObject && !RCLASS_SUPER(super)) {
2038  rb_raise(rb_eTypeError, "can't inherit uninitialized class");
2039  }
2040  }
2041  RCLASS_SET_SUPER(klass, super);
2042  rb_make_metaclass(klass, RBASIC(super)->klass);
2043  rb_class_inherited(super, klass);
2044  rb_mod_initialize(klass);
2045 
2046  return klass;
2047 }
2048 
2050 void
2051 rb_undefined_alloc(VALUE klass)
2052 {
2053  rb_raise(rb_eTypeError, "allocator undefined for %"PRIsVALUE,
2054  klass);
2055 }
2056 
2057 /*
2058  * call-seq:
2059  * class.allocate() -> obj
2060  *
2061  * Allocates space for a new object of <i>class</i>'s class and does not
2062  * call initialize on the new instance. The returned object must be an
2063  * instance of <i>class</i>.
2064  *
2065  * klass = Class.new do
2066  * def initialize(*args)
2067  * @initialized = true
2068  * end
2069  *
2070  * def initialized?
2071  * @initialized || false
2072  * end
2073  * end
2074  *
2075  * klass.allocate.initialized? #=> false
2076  *
2077  */
2078 
2079 static VALUE
2080 rb_class_alloc(VALUE klass)
2081 {
2082  VALUE obj;
2083  rb_alloc_func_t allocator;
2084 
2085  if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
2086  rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
2087  }
2088  if (FL_TEST(klass, FL_SINGLETON)) {
2089  rb_raise(rb_eTypeError, "can't create instance of singleton class");
2090  }
2091  allocator = rb_get_alloc_func(klass);
2092  if (!allocator) {
2093  rb_undefined_alloc(klass);
2094  }
2095 
2096  RUBY_DTRACE_CREATE_HOOK(OBJECT, rb_class2name(klass));
2097 
2098  obj = (*allocator)(klass);
2099 
2100  if (rb_obj_class(obj) != rb_class_real(klass)) {
2101  rb_raise(rb_eTypeError, "wrong instance allocation");
2102  }
2103  return obj;
2104 }
2105 
2120 VALUE
2122 {
2123  Check_Type(klass, T_CLASS);
2124  return rb_class_alloc(klass);
2125 }
2126 
2127 static VALUE
2128 rb_class_allocate_instance(VALUE klass)
2129 {
2130  NEWOBJ_OF(obj, struct RObject, klass, T_OBJECT | (RGENGC_WB_PROTECTED_OBJECT ? FL_WB_PROTECTED : 0));
2131  return (VALUE)obj;
2132 }
2133 
2134 /*
2135  * call-seq:
2136  * class.new(args, ...) -> obj
2137  *
2138  * Calls <code>allocate</code> to create a new object of
2139  * <i>class</i>'s class, then invokes that object's
2140  * <code>initialize</code> method, passing it <i>args</i>.
2141  * This is the method that ends up getting called whenever
2142  * an object is constructed using .new.
2143  *
2144  */
2145 
2146 static VALUE
2147 rb_class_s_new(int argc, const VALUE *argv, VALUE klass)
2148 {
2149  VALUE obj;
2150 
2151  obj = rb_class_alloc(klass);
2152  rb_obj_call_init(obj, argc, argv);
2153 
2154  return obj;
2155 }
2156 
2169 VALUE
2171 {
2172  Check_Type(klass, T_CLASS);
2173  return rb_class_s_new(argc, argv, klass);
2174 }
2175 
2203 VALUE
2205 {
2206  VALUE super = RCLASS_SUPER(klass);
2207 
2208  if (!super) {
2209  if (klass == rb_cBasicObject) return Qnil;
2210  rb_raise(rb_eTypeError, "uninitialized class");
2211  }
2212  while (RB_TYPE_P(super, T_ICLASS)) {
2213  super = RCLASS_SUPER(super);
2214  }
2215  if (!super) {
2216  return Qnil;
2217  }
2218  return super;
2219 }
2220 
2228 VALUE
2230 {
2231  return RCLASS(klass)->super;
2232 }
2233 
2235 #define id_for_var(obj, name, part, type) \
2236  id_for_setter(obj, name, type, "`%1$s' is not allowed as "#part" "#type" variable name")
2237 
2238 #define id_for_setter(obj, name, type, message) \
2239  check_setter_id(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2240 static ID
2241 check_setter_id(VALUE obj, VALUE *pname,
2242  int (*valid_id_p)(ID), int (*valid_name_p)(VALUE),
2243  const char *message, size_t message_len)
2244 {
2245  ID id = rb_check_id(pname);
2246  VALUE name = *pname;
2247 
2248  if (id ? !valid_id_p(id) : !valid_name_p(name)) {
2249  rb_name_err_raise_str(rb_fstring_new(message, message_len),
2250  obj, name);
2251  }
2252  return id;
2253 }
2254 
2255 static int
2256 rb_is_attr_name(VALUE name)
2257 {
2258  return rb_is_local_name(name) || rb_is_const_name(name);
2259 }
2260 
2261 static int
2262 rb_is_attr_id(ID id)
2263 {
2264  return rb_is_local_id(id) || rb_is_const_id(id);
2265 }
2266 
2267 static const char wrong_constant_name[] = "wrong constant name %1$s";
2268 static const char invalid_attribute_name[] = "invalid attribute name `%1$s'";
2269 
2270 static ID
2271 id_for_attr(VALUE obj, VALUE name)
2272 {
2273  ID id = id_for_setter(obj, name, attr, invalid_attribute_name);
2274  if (!id) id = rb_intern_str(name);
2275  return id;
2276 }
2277 
2278 /*
2279  * call-seq:
2280  * attr_reader(symbol, ...) -> nil
2281  * attr(symbol, ...) -> nil
2282  * attr_reader(string, ...) -> nil
2283  * attr(string, ...) -> nil
2284  *
2285  * Creates instance variables and corresponding methods that return the
2286  * value of each instance variable. Equivalent to calling
2287  * ``<code>attr</code><i>:name</i>'' on each name in turn.
2288  * String arguments are converted to symbols.
2289  */
2290 
2291 static VALUE
2292 rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
2293 {
2294  int i;
2295 
2296  for (i=0; i<argc; i++) {
2297  rb_attr(klass, id_for_attr(klass, argv[i]), TRUE, FALSE, TRUE);
2298  }
2299  return Qnil;
2300 }
2301 
2316 VALUE
2317 rb_mod_attr(int argc, VALUE *argv, VALUE klass)
2318 {
2319  if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
2320  rb_warning("optional boolean argument is obsoleted");
2321  rb_attr(klass, id_for_attr(klass, argv[0]), 1, RTEST(argv[1]), TRUE);
2322  return Qnil;
2323  }
2324  return rb_mod_attr_reader(argc, argv, klass);
2325 }
2326 
2327 /*
2328  * call-seq:
2329  * attr_writer(symbol, ...) -> nil
2330  * attr_writer(string, ...) -> nil
2331  *
2332  * Creates an accessor method to allow assignment to the attribute
2333  * <i>symbol</i><code>.id2name</code>.
2334  * String arguments are converted to symbols.
2335  */
2336 
2337 static VALUE
2338 rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
2339 {
2340  int i;
2341 
2342  for (i=0; i<argc; i++) {
2343  rb_attr(klass, id_for_attr(klass, argv[i]), FALSE, TRUE, TRUE);
2344  }
2345  return Qnil;
2346 }
2347 
2348 /*
2349  * call-seq:
2350  * attr_accessor(symbol, ...) -> nil
2351  * attr_accessor(string, ...) -> nil
2352  *
2353  * Defines a named attribute for this module, where the name is
2354  * <i>symbol.</i><code>id2name</code>, creating an instance variable
2355  * (<code>@name</code>) and a corresponding access method to read it.
2356  * Also creates a method called <code>name=</code> to set the attribute.
2357  * String arguments are converted to symbols.
2358  *
2359  * module Mod
2360  * attr_accessor(:one, :two)
2361  * end
2362  * Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
2363  */
2364 
2365 static VALUE
2366 rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
2367 {
2368  int i;
2369 
2370  for (i=0; i<argc; i++) {
2371  rb_attr(klass, id_for_attr(klass, argv[i]), TRUE, TRUE, TRUE);
2372  }
2373  return Qnil;
2374 }
2375 
2376 /*
2377  * call-seq:
2378  * mod.const_get(sym, inherit=true) -> obj
2379  * mod.const_get(str, inherit=true) -> obj
2380  *
2381  * Checks for a constant with the given name in <i>mod</i>.
2382  * If +inherit+ is set, the lookup will also search
2383  * the ancestors (and +Object+ if <i>mod</i> is a +Module+).
2384  *
2385  * The value of the constant is returned if a definition is found,
2386  * otherwise a +NameError+ is raised.
2387  *
2388  * Math.const_get(:PI) #=> 3.14159265358979
2389  *
2390  * This method will recursively look up constant names if a namespaced
2391  * class name is provided. For example:
2392  *
2393  * module Foo; class Bar; end end
2394  * Object.const_get 'Foo::Bar'
2395  *
2396  * The +inherit+ flag is respected on each lookup. For example:
2397  *
2398  * module Foo
2399  * class Bar
2400  * VAL = 10
2401  * end
2402  *
2403  * class Baz < Bar; end
2404  * end
2405  *
2406  * Object.const_get 'Foo::Baz::VAL' # => 10
2407  * Object.const_get 'Foo::Baz::VAL', false # => NameError
2408  *
2409  * If the argument is not a valid constant name a +NameError+ will be
2410  * raised with a warning "wrong constant name".
2411  *
2412  * Object.const_get 'foobar' #=> NameError: wrong constant name foobar
2413  *
2414  */
2415 
2416 static VALUE
2417 rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
2418 {
2419  VALUE name, recur;
2420  rb_encoding *enc;
2421  const char *pbeg, *p, *path, *pend;
2422  ID id;
2423 
2424  rb_check_arity(argc, 1, 2);
2425  name = argv[0];
2426  recur = (argc == 1) ? Qtrue : argv[1];
2427 
2428  if (SYMBOL_P(name)) {
2429  if (!rb_is_const_sym(name)) goto wrong_name;
2430  id = rb_check_id(&name);
2431  if (!id) return rb_const_missing(mod, name);
2432  return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
2433  }
2434 
2435  path = StringValuePtr(name);
2436  enc = rb_enc_get(name);
2437 
2438  if (!rb_enc_asciicompat(enc)) {
2439  rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
2440  }
2441 
2442  pbeg = p = path;
2443  pend = path + RSTRING_LEN(name);
2444 
2445  if (p >= pend || !*p) {
2446  wrong_name:
2447  rb_name_err_raise(wrong_constant_name, mod, name);
2448  }
2449 
2450  if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
2451  mod = rb_cObject;
2452  p += 2;
2453  pbeg = p;
2454  }
2455 
2456  while (p < pend) {
2457  VALUE part;
2458  long len, beglen;
2459 
2460  while (p < pend && *p != ':') p++;
2461 
2462  if (pbeg == p) goto wrong_name;
2463 
2464  id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
2465  beglen = pbeg-path;
2466 
2467  if (p < pend && p[0] == ':') {
2468  if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2469  p += 2;
2470  pbeg = p;
2471  }
2472 
2473  if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2474  rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2475  QUOTE(name));
2476  }
2477 
2478  if (!id) {
2479  part = rb_str_subseq(name, beglen, len);
2480  OBJ_FREEZE(part);
2481  if (!ISUPPER(*pbeg) || !rb_is_const_name(part)) {
2482  name = part;
2483  goto wrong_name;
2484  }
2485  else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
2486  part = rb_str_intern(part);
2487  mod = rb_const_missing(mod, part);
2488  continue;
2489  }
2490  else {
2491  rb_mod_const_missing(mod, part);
2492  }
2493  }
2494  if (!rb_is_const_id(id)) {
2495  name = ID2SYM(id);
2496  goto wrong_name;
2497  }
2498  mod = RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
2499  }
2500 
2501  return mod;
2502 }
2503 
2504 /*
2505  * call-seq:
2506  * mod.const_set(sym, obj) -> obj
2507  * mod.const_set(str, obj) -> obj
2508  *
2509  * Sets the named constant to the given object, returning that object.
2510  * Creates a new constant if no constant with the given name previously
2511  * existed.
2512  *
2513  * Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
2514  * Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
2515  *
2516  * If +sym+ or +str+ is not a valid constant name a +NameError+ will be
2517  * raised with a warning "wrong constant name".
2518  *
2519  * Object.const_set('foobar', 42) #=> NameError: wrong constant name foobar
2520  *
2521  */
2522 
2523 static VALUE
2524 rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
2525 {
2526  ID id = id_for_setter(mod, name, const, wrong_constant_name);
2527  if (!id) id = rb_intern_str(name);
2528  rb_const_set(mod, id, value);
2529 
2530  return value;
2531 }
2532 
2533 /*
2534  * call-seq:
2535  * mod.const_defined?(sym, inherit=true) -> true or false
2536  * mod.const_defined?(str, inherit=true) -> true or false
2537  *
2538  * Says whether _mod_ or its ancestors have a constant with the given name:
2539  *
2540  * Float.const_defined?(:EPSILON) #=> true, found in Float itself
2541  * Float.const_defined?("String") #=> true, found in Object (ancestor)
2542  * BasicObject.const_defined?(:Hash) #=> false
2543  *
2544  * If _mod_ is a +Module+, additionally +Object+ and its ancestors are checked:
2545  *
2546  * Math.const_defined?(:String) #=> true, found in Object
2547  *
2548  * In each of the checked classes or modules, if the constant is not present
2549  * but there is an autoload for it, +true+ is returned directly without
2550  * autoloading:
2551  *
2552  * module Admin
2553  * autoload :User, 'admin/user'
2554  * end
2555  * Admin.const_defined?(:User) #=> true
2556  *
2557  * If the constant is not found the callback +const_missing+ is *not* called
2558  * and the method returns +false+.
2559  *
2560  * If +inherit+ is false, the lookup only checks the constants in the receiver:
2561  *
2562  * IO.const_defined?(:SYNC) #=> true, found in File::Constants (ancestor)
2563  * IO.const_defined?(:SYNC, false) #=> false, not found in IO itself
2564  *
2565  * In this case, the same logic for autoloading applies.
2566  *
2567  * If the argument is not a valid constant name a +NameError+ is raised with the
2568  * message "wrong constant name _name_":
2569  *
2570  * Hash.const_defined? 'foobar' #=> NameError: wrong constant name foobar
2571  *
2572  */
2573 
2574 static VALUE
2575 rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
2576 {
2577  VALUE name, recur;
2578  rb_encoding *enc;
2579  const char *pbeg, *p, *path, *pend;
2580  ID id;
2581 
2582  rb_check_arity(argc, 1, 2);
2583  name = argv[0];
2584  recur = (argc == 1) ? Qtrue : argv[1];
2585 
2586  if (SYMBOL_P(name)) {
2587  if (!rb_is_const_sym(name)) goto wrong_name;
2588  id = rb_check_id(&name);
2589  if (!id) return Qfalse;
2590  return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
2591  }
2592 
2593  path = StringValuePtr(name);
2594  enc = rb_enc_get(name);
2595 
2596  if (!rb_enc_asciicompat(enc)) {
2597  rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
2598  }
2599 
2600  pbeg = p = path;
2601  pend = path + RSTRING_LEN(name);
2602 
2603  if (p >= pend || !*p) {
2604  wrong_name:
2605  rb_name_err_raise(wrong_constant_name, mod, name);
2606  }
2607 
2608  if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
2609  mod = rb_cObject;
2610  p += 2;
2611  pbeg = p;
2612  }
2613 
2614  while (p < pend) {
2615  VALUE part;
2616  long len, beglen;
2617 
2618  while (p < pend && *p != ':') p++;
2619 
2620  if (pbeg == p) goto wrong_name;
2621 
2622  id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
2623  beglen = pbeg-path;
2624 
2625  if (p < pend && p[0] == ':') {
2626  if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2627  p += 2;
2628  pbeg = p;
2629  }
2630 
2631  if (!id) {
2632  part = rb_str_subseq(name, beglen, len);
2633  OBJ_FREEZE(part);
2634  if (!ISUPPER(*pbeg) || !rb_is_const_name(part)) {
2635  name = part;
2636  goto wrong_name;
2637  }
2638  else {
2639  return Qfalse;
2640  }
2641  }
2642  if (!rb_is_const_id(id)) {
2643  name = ID2SYM(id);
2644  goto wrong_name;
2645  }
2646  if (RTEST(recur)) {
2647  if (!rb_const_defined(mod, id))
2648  return Qfalse;
2649  mod = rb_const_get(mod, id);
2650  }
2651  else {
2652  if (!rb_const_defined_at(mod, id))
2653  return Qfalse;
2654  mod = rb_const_get_at(mod, id);
2655  }
2656  recur = Qfalse;
2657 
2658  if (p < pend && !RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2659  rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2660  QUOTE(name));
2661  }
2662  }
2663 
2664  return Qtrue;
2665 }
2666 
2667 /*
2668  * call-seq:
2669  * obj.instance_variable_get(symbol) -> obj
2670  * obj.instance_variable_get(string) -> obj
2671  *
2672  * Returns the value of the given instance variable, or nil if the
2673  * instance variable is not set. The <code>@</code> part of the
2674  * variable name should be included for regular instance
2675  * variables. Throws a <code>NameError</code> exception if the
2676  * supplied symbol is not valid as an instance variable name.
2677  * String arguments are converted to symbols.
2678  *
2679  * class Fred
2680  * def initialize(p1, p2)
2681  * @a, @b = p1, p2
2682  * end
2683  * end
2684  * fred = Fred.new('cat', 99)
2685  * fred.instance_variable_get(:@a) #=> "cat"
2686  * fred.instance_variable_get("@b") #=> 99
2687  */
2688 
2689 static VALUE
2690 rb_obj_ivar_get(VALUE obj, VALUE iv)
2691 {
2692  ID id = id_for_var(obj, iv, an, instance);
2693 
2694  if (!id) {
2695  return Qnil;
2696  }
2697  return rb_ivar_get(obj, id);
2698 }
2699 
2700 /*
2701  * call-seq:
2702  * obj.instance_variable_set(symbol, obj) -> obj
2703  * obj.instance_variable_set(string, obj) -> obj
2704  *
2705  * Sets the instance variable named by <i>symbol</i> to the given
2706  * object, thereby frustrating the efforts of the class's
2707  * author to attempt to provide proper encapsulation. The variable
2708  * does not have to exist prior to this call.
2709  * If the instance variable name is passed as a string, that string
2710  * is converted to a symbol.
2711  *
2712  * class Fred
2713  * def initialize(p1, p2)
2714  * @a, @b = p1, p2
2715  * end
2716  * end
2717  * fred = Fred.new('cat', 99)
2718  * fred.instance_variable_set(:@a, 'dog') #=> "dog"
2719  * fred.instance_variable_set(:@c, 'cat') #=> "cat"
2720  * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
2721  */
2722 
2723 static VALUE
2724 rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
2725 {
2726  ID id = id_for_var(obj, iv, an, instance);
2727  if (!id) id = rb_intern_str(iv);
2728  return rb_ivar_set(obj, id, val);
2729 }
2730 
2731 /*
2732  * call-seq:
2733  * obj.instance_variable_defined?(symbol) -> true or false
2734  * obj.instance_variable_defined?(string) -> true or false
2735  *
2736  * Returns <code>true</code> if the given instance variable is
2737  * defined in <i>obj</i>.
2738  * String arguments are converted to symbols.
2739  *
2740  * class Fred
2741  * def initialize(p1, p2)
2742  * @a, @b = p1, p2
2743  * end
2744  * end
2745  * fred = Fred.new('cat', 99)
2746  * fred.instance_variable_defined?(:@a) #=> true
2747  * fred.instance_variable_defined?("@b") #=> true
2748  * fred.instance_variable_defined?("@c") #=> false
2749  */
2750 
2751 static VALUE
2752 rb_obj_ivar_defined(VALUE obj, VALUE iv)
2753 {
2754  ID id = id_for_var(obj, iv, an, instance);
2755 
2756  if (!id) {
2757  return Qfalse;
2758  }
2759  return rb_ivar_defined(obj, id);
2760 }
2761 
2762 /*
2763  * call-seq:
2764  * mod.class_variable_get(symbol) -> obj
2765  * mod.class_variable_get(string) -> obj
2766  *
2767  * Returns the value of the given class variable (or throws a
2768  * <code>NameError</code> exception). The <code>@@</code> part of the
2769  * variable name should be included for regular class variables.
2770  * String arguments are converted to symbols.
2771  *
2772  * class Fred
2773  * @@foo = 99
2774  * end
2775  * Fred.class_variable_get(:@@foo) #=> 99
2776  */
2777 
2778 static VALUE
2779 rb_mod_cvar_get(VALUE obj, VALUE iv)
2780 {
2781  ID id = id_for_var(obj, iv, a, class);
2782 
2783  if (!id) {
2784  rb_name_err_raise("uninitialized class variable %1$s in %2$s",
2785  obj, iv);
2786  }
2787  return rb_cvar_get(obj, id);
2788 }
2789 
2790 /*
2791  * call-seq:
2792  * obj.class_variable_set(symbol, obj) -> obj
2793  * obj.class_variable_set(string, obj) -> obj
2794  *
2795  * Sets the class variable named by <i>symbol</i> to the given
2796  * object.
2797  * If the class variable name is passed as a string, that string
2798  * is converted to a symbol.
2799  *
2800  * class Fred
2801  * @@foo = 99
2802  * def foo
2803  * @@foo
2804  * end
2805  * end
2806  * Fred.class_variable_set(:@@foo, 101) #=> 101
2807  * Fred.new.foo #=> 101
2808  */
2809 
2810 static VALUE
2811 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
2812 {
2813  ID id = id_for_var(obj, iv, a, class);
2814  if (!id) id = rb_intern_str(iv);
2815  rb_cvar_set(obj, id, val);
2816  return val;
2817 }
2818 
2819 /*
2820  * call-seq:
2821  * obj.class_variable_defined?(symbol) -> true or false
2822  * obj.class_variable_defined?(string) -> true or false
2823  *
2824  * Returns <code>true</code> if the given class variable is defined
2825  * in <i>obj</i>.
2826  * String arguments are converted to symbols.
2827  *
2828  * class Fred
2829  * @@foo = 99
2830  * end
2831  * Fred.class_variable_defined?(:@@foo) #=> true
2832  * Fred.class_variable_defined?(:@@bar) #=> false
2833  */
2834 
2835 static VALUE
2836 rb_mod_cvar_defined(VALUE obj, VALUE iv)
2837 {
2838  ID id = id_for_var(obj, iv, a, class);
2839 
2840  if (!id) {
2841  return Qfalse;
2842  }
2843  return rb_cvar_defined(obj, id);
2844 }
2845 
2846 /*
2847  * call-seq:
2848  * mod.singleton_class? -> true or false
2849  *
2850  * Returns <code>true</code> if <i>mod</i> is a singleton class or
2851  * <code>false</code> if it is an ordinary class or module.
2852  *
2853  * class C
2854  * end
2855  * C.singleton_class? #=> false
2856  * C.singleton_class.singleton_class? #=> true
2857  */
2858 
2859 static VALUE
2860 rb_mod_singleton_p(VALUE klass)
2861 {
2862  if (RB_TYPE_P(klass, T_CLASS) && FL_TEST(klass, FL_SINGLETON))
2863  return Qtrue;
2864  return Qfalse;
2865 }
2866 
2868 static const struct conv_method_tbl {
2869  const char method[6];
2870  unsigned short id;
2871 } conv_method_names[] = {
2872 #define M(n) {#n, (unsigned short)idTo_##n}
2873  M(int),
2874  M(ary),
2875  M(str),
2876  M(sym),
2877  M(hash),
2878  M(proc),
2879  M(io),
2880  M(a),
2881  M(s),
2882  M(i),
2883  M(r),
2884 #undef M
2885 };
2886 #define IMPLICIT_CONVERSIONS 7
2887 
2888 static int
2889 conv_method_index(const char *method)
2890 {
2891  static const char prefix[] = "to_";
2892 
2893  if (strncmp(prefix, method, sizeof(prefix)-1) == 0) {
2894  const char *const meth = &method[sizeof(prefix)-1];
2895  int i;
2896  for (i=0; i < numberof(conv_method_names); i++) {
2897  if (conv_method_names[i].method[0] == meth[0] &&
2898  strcmp(conv_method_names[i].method, meth) == 0) {
2899  return i;
2900  }
2901  }
2902  }
2903  return numberof(conv_method_names);
2904 }
2905 
2906 static VALUE
2907 convert_type_with_id(VALUE val, const char *tname, ID method, int raise, int index)
2908 {
2909  VALUE r = rb_check_funcall(val, method, 0, 0);
2910  if (r == Qundef) {
2911  if (raise) {
2912  const char *msg =
2913  ((index < 0 ? conv_method_index(rb_id2name(method)) : index)
2915  "no implicit conversion of" : "can't convert";
2916  const char *cname = NIL_P(val) ? "nil" :
2917  val == Qtrue ? "true" :
2918  val == Qfalse ? "false" :
2919  NULL;
2920  if (cname)
2921  rb_raise(rb_eTypeError, "%s %s into %s", msg, cname, tname);
2922  rb_raise(rb_eTypeError, "%s %"PRIsVALUE" into %s", msg,
2923  rb_obj_class(val),
2924  tname);
2925  }
2926  return Qnil;
2927  }
2928  return r;
2929 }
2930 
2931 static VALUE
2932 convert_type(VALUE val, const char *tname, const char *method, int raise)
2933 {
2934  int i = conv_method_index(method);
2935  ID m = i < numberof(conv_method_names) ?
2936  conv_method_names[i].id : rb_intern(method);
2937  return convert_type_with_id(val, tname, m, raise, i);
2938 }
2939 
2941 NORETURN(static void conversion_mismatch(VALUE, const char *, const char *, VALUE));
2942 static void
2943 conversion_mismatch(VALUE val, const char *tname, const char *method, VALUE result)
2944 {
2945  VALUE cname = rb_obj_class(val);
2947  "can't convert %"PRIsVALUE" to %s (%"PRIsVALUE"#%s gives %"PRIsVALUE")",
2948  cname, tname, cname, method, rb_obj_class(result));
2949 }
2950 
2964 VALUE
2965 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
2966 {
2967  VALUE v;
2968 
2969  if (TYPE(val) == type) return val;
2970  v = convert_type(val, tname, method, TRUE);
2971  if (TYPE(v) != type) {
2972  conversion_mismatch(val, tname, method, v);
2973  }
2974  return v;
2975 }
2976 
2978 VALUE
2979 rb_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
2980 {
2981  VALUE v;
2982 
2983  if (TYPE(val) == type) return val;
2984  v = convert_type_with_id(val, tname, method, TRUE, -1);
2985  if (TYPE(v) != type) {
2986  conversion_mismatch(val, tname, RSTRING_PTR(rb_id2str(method)), v);
2987  }
2988  return v;
2989 }
2990 
3005 VALUE
3006 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
3007 {
3008  VALUE v;
3009 
3010  /* always convert T_DATA */
3011  if (TYPE(val) == type && type != T_DATA) return val;
3012  v = convert_type(val, tname, method, FALSE);
3013  if (NIL_P(v)) return Qnil;
3014  if (TYPE(v) != type) {
3015  conversion_mismatch(val, tname, method, v);
3016  }
3017  return v;
3018 }
3019 
3021 VALUE
3022 rb_check_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
3023 {
3024  VALUE v;
3025 
3026  /* always convert T_DATA */
3027  if (TYPE(val) == type && type != T_DATA) return val;
3028  v = convert_type_with_id(val, tname, method, FALSE, -1);
3029  if (NIL_P(v)) return Qnil;
3030  if (TYPE(v) != type) {
3031  conversion_mismatch(val, tname, RSTRING_PTR(rb_id2str(method)), v);
3032  }
3033  return v;
3034 }
3035 
3036 
3037 static VALUE
3038 rb_to_integer(VALUE val, const char *method)
3039 {
3040  VALUE v;
3041 
3042  if (FIXNUM_P(val)) return val;
3043  if (RB_TYPE_P(val, T_BIGNUM)) return val;
3044  v = convert_type(val, "Integer", method, TRUE);
3045  if (!rb_obj_is_kind_of(v, rb_cInteger)) {
3046  conversion_mismatch(val, "Integer", method, v);
3047  }
3048  return v;
3049 }
3050 
3061 VALUE
3062 rb_check_to_integer(VALUE val, const char *method)
3063 {
3064  VALUE v;
3065 
3066  if (FIXNUM_P(val)) return val;
3067  if (RB_TYPE_P(val, T_BIGNUM)) return val;
3068  v = convert_type(val, "Integer", method, FALSE);
3069  if (!rb_obj_is_kind_of(v, rb_cInteger)) {
3070  return Qnil;
3071  }
3072  return v;
3073 }
3074 
3083 VALUE
3085 {
3086  return rb_to_integer(val, "to_int");
3087 }
3088 
3098 VALUE
3100 {
3101  return rb_check_to_integer(val, "to_int");
3102 }
3103 
3104 static VALUE
3105 rb_convert_to_integer(VALUE val, int base)
3106 {
3107  VALUE tmp;
3108 
3109  if (RB_FLOAT_TYPE_P(val)) {
3110  double f;
3111  if (base != 0) goto arg_error;
3112  f = RFLOAT_VALUE(val);
3113  if (FIXABLE(f)) return LONG2FIX((long)f);
3114  return rb_dbl2big(f);
3115  }
3116  else if (RB_INTEGER_TYPE_P(val)) {
3117  if (base != 0) goto arg_error;
3118  return val;
3119  }
3120  else if (RB_TYPE_P(val, T_STRING)) {
3121  return rb_str_to_inum(val, base, TRUE);
3122  }
3123  else if (NIL_P(val)) {
3124  if (base != 0) goto arg_error;
3125  rb_raise(rb_eTypeError, "can't convert nil into Integer");
3126  }
3127  if (base != 0) {
3128  tmp = rb_check_string_type(val);
3129  if (!NIL_P(tmp)) return rb_str_to_inum(tmp, base, TRUE);
3130  arg_error:
3131  rb_raise(rb_eArgError, "base specified for non string value");
3132  }
3133  tmp = convert_type(val, "Integer", "to_int", FALSE);
3134  if (NIL_P(tmp)) {
3135  return rb_to_integer(val, "to_i");
3136  }
3137  return tmp;
3138 
3139 }
3140 
3147 VALUE
3149 {
3150  return rb_convert_to_integer(val, 0);
3151 }
3152 
3153 /*
3154  * call-seq:
3155  * Integer(arg, base=0) -> integer
3156  *
3157  * Converts <i>arg</i> to an <code>Integer</code>.
3158  * Numeric types are converted directly (with floating point numbers
3159  * being truncated). <i>base</i> (0, or between 2 and 36) is a base for
3160  * integer string representation. If <i>arg</i> is a <code>String</code>,
3161  * when <i>base</i> is omitted or equals zero, radix indicators
3162  * (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
3163  * In any case, strings should be strictly conformed to numeric
3164  * representation. This behavior is different from that of
3165  * <code>String#to_i</code>. Non string values will be converted by first
3166  * trying <code>to_int</code>, then <code>to_i</code>. Passing <code>nil</code>
3167  * raises a TypeError.
3168  *
3169  * Integer(123.999) #=> 123
3170  * Integer("0x1a") #=> 26
3171  * Integer(Time.new) #=> 1204973019
3172  * Integer("0930", 10) #=> 930
3173  * Integer("111", 2) #=> 7
3174  * Integer(nil) #=> TypeError
3175  */
3176 
3177 static VALUE
3178 rb_f_integer(int argc, VALUE *argv, VALUE obj)
3179 {
3180  VALUE arg = Qnil;
3181  int base = 0;
3182 
3183  switch (argc) {
3184  case 2:
3185  base = NUM2INT(argv[1]);
3186  case 1:
3187  arg = argv[0];
3188  break;
3189  default:
3190  /* should cause ArgumentError */
3191  rb_scan_args(argc, argv, "11", NULL, NULL);
3192  }
3193  return rb_convert_to_integer(arg, base);
3194 }
3195 
3207 double
3208 rb_cstr_to_dbl(const char *p, int badcheck)
3209 {
3210  const char *q;
3211  char *end;
3212  double d;
3213  const char *ellipsis = "";
3214  int w;
3215  enum {max_width = 20};
3216 #define OutOfRange() ((end - p > max_width) ? \
3217  (w = max_width, ellipsis = "...") : \
3218  (w = (int)(end - p), ellipsis = ""))
3219 
3220  if (!p) return 0.0;
3221  q = p;
3222  while (ISSPACE(*p)) p++;
3223 
3224  if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
3225  return 0.0;
3226  }
3227 
3228  d = strtod(p, &end);
3229  if (errno == ERANGE) {
3230  OutOfRange();
3231  rb_warning("Float %.*s%s out of range", w, p, ellipsis);
3232  errno = 0;
3233  }
3234  if (p == end) {
3235  if (badcheck) {
3236  bad:
3237  rb_invalid_str(q, "Float()");
3238  }
3239  return d;
3240  }
3241  if (*end) {
3242  char buf[DBL_DIG * 4 + 10];
3243  char *n = buf;
3244  char *e = buf + sizeof(buf) - 1;
3245  char prev = 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 (n < e) *n++ = prev;
3258  }
3259  *n = '\0';
3260  p = buf;
3261 
3262  if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
3263  return 0.0;
3264  }
3265 
3266  d = strtod(p, &end);
3267  if (errno == ERANGE) {
3268  OutOfRange();
3269  rb_warning("Float %.*s%s out of range", w, p, ellipsis);
3270  errno = 0;
3271  }
3272  if (badcheck) {
3273  if (!end || p == end) goto bad;
3274  while (*end && ISSPACE(*end)) end++;
3275  if (*end) goto bad;
3276  }
3277  }
3278  if (errno == ERANGE) {
3279  errno = 0;
3280  OutOfRange();
3281  rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
3282  }
3283  return d;
3284 }
3285 
3297 double
3298 rb_str_to_dbl(VALUE str, int badcheck)
3299 {
3300  char *s;
3301  long len;
3302  double ret;
3303  VALUE v = 0;
3304 
3305  StringValue(str);
3306  s = RSTRING_PTR(str);
3307  len = RSTRING_LEN(str);
3308  if (s) {
3309  if (badcheck && memchr(s, '\0', len)) {
3310  rb_raise(rb_eArgError, "string for Float contains null byte");
3311  }
3312  if (s[len]) { /* no sentinel somehow */
3313  char *p = ALLOCV(v, (size_t)len + 1);
3314  MEMCPY(p, s, char, len);
3315  p[len] = '\0';
3316  s = p;
3317  }
3318  }
3319  ret = rb_cstr_to_dbl(s, badcheck);
3320  if (v)
3321  ALLOCV_END(v);
3322  return ret;
3323 }
3324 
3326 #define fix2dbl_without_to_f(x) (double)FIX2LONG(x)
3327 #define big2dbl_without_to_f(x) rb_big2dbl(x)
3328 #define int2dbl_without_to_f(x) \
3329  (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : big2dbl_without_to_f(x))
3330 #define rat2dbl_without_to_f(x) \
3331  (int2dbl_without_to_f(rb_rational_num(x)) / \
3332  int2dbl_without_to_f(rb_rational_den(x)))
3333 
3334 #define special_const_to_float(val, pre, post) \
3335  switch (val) { \
3336  case Qnil: \
3337  rb_raise_static(rb_eTypeError, pre "nil" post); \
3338  case Qtrue: \
3339  rb_raise_static(rb_eTypeError, pre "true" post); \
3340  case Qfalse: \
3341  rb_raise_static(rb_eTypeError, pre "false" post); \
3342  }
3343 
3345 static inline void
3346 conversion_to_float(VALUE val)
3347 {
3348  special_const_to_float(val, "can't convert ", " into Float");
3349 }
3350 
3351 static inline void
3352 implicit_conversion_to_float(VALUE val)
3353 {
3354  special_const_to_float(val, "no implicit conversion to float from ", "");
3355 }
3356 
3357 static int
3358 to_float(VALUE *valp)
3359 {
3360  VALUE val = *valp;
3361  if (SPECIAL_CONST_P(val)) {
3362  if (FIXNUM_P(val)) {
3363  *valp = DBL2NUM(fix2dbl_without_to_f(val));
3364  return T_FLOAT;
3365  }
3366  else if (FLONUM_P(val)) {
3367  return T_FLOAT;
3368  }
3369  else {
3370  conversion_to_float(val);
3371  }
3372  }
3373  else {
3374  int type = BUILTIN_TYPE(val);
3375  switch (type) {
3376  case T_FLOAT:
3377  return T_FLOAT;
3378  case T_BIGNUM:
3379  *valp = DBL2NUM(big2dbl_without_to_f(val));
3380  return T_FLOAT;
3381  case T_RATIONAL:
3382  *valp = DBL2NUM(rat2dbl_without_to_f(val));
3383  return T_FLOAT;
3384  case T_STRING:
3385  return T_STRING;
3386  }
3387  }
3388  return T_NONE;
3389 }
3390 
3397 VALUE
3399 {
3400  switch (to_float(&val)) {
3401  case T_FLOAT:
3402  return val;
3403  case T_STRING:
3404  return DBL2NUM(rb_str_to_dbl(val, TRUE));
3405  }
3406  return rb_convert_type(val, T_FLOAT, "Float", "to_f");
3407 }
3408 
3409 FUNC_MINIMIZED(static VALUE rb_f_float(VALUE obj, VALUE arg));
3411 /*
3412  * call-seq:
3413  * Float(arg) -> float
3414  *
3415  * Returns <i>arg</i> converted to a float. Numeric types are converted
3416  * directly, and with exception to string and nil the rest are converted using <i>arg</i>.to_f.
3417  * Converting a <code>string</code> with invalid characters will result in a <code>ArgumentError</code>.
3418  * Converting <code>nil</code> generates a <code>TypeError</code>.
3419  *
3420  * Float(1) #=> 1.0
3421  * Float("123.456") #=> 123.456
3422  * Float("123.0_badstring") #=> ArgumentError: invalid value for Float(): "123.0_badstring"
3423  * Float(nil) #=> TypeError: can't convert nil into Float
3424  */
3425 
3426 static VALUE
3427 rb_f_float(VALUE obj, VALUE arg)
3428 {
3429  return rb_Float(arg);
3430 }
3431 
3432 static VALUE
3433 numeric_to_float(VALUE val)
3434 {
3435  if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
3436  rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into Float",
3437  rb_obj_class(val));
3438  }
3439  return rb_convert_type(val, T_FLOAT, "Float", "to_f");
3440 }
3441 
3447 VALUE
3449 {
3450  switch (to_float(&val)) {
3451  case T_FLOAT:
3452  return val;
3453  }
3454  return numeric_to_float(val);
3455 }
3456 
3464 VALUE
3466 {
3467  if (RB_TYPE_P(val, T_FLOAT)) return val;
3468  if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
3469  return Qnil;
3470  }
3471  return rb_check_convert_type(val, T_FLOAT, "Float", "to_f");
3472 }
3473 
3474 static ID id_to_f;
3475 
3476 static inline int
3477 basic_to_f_p(VALUE klass)
3478 {
3479  return rb_method_basic_definition_p(klass, id_to_f);
3480 }
3481 
3483 double
3485 {
3486  if (SPECIAL_CONST_P(val)) {
3487  if (FIXNUM_P(val)) {
3488  if (basic_to_f_p(rb_cInteger))
3489  return fix2dbl_without_to_f(val);
3490  }
3491  else if (FLONUM_P(val)) {
3492  return rb_float_flonum_value(val);
3493  }
3494  else {
3495  conversion_to_float(val);
3496  }
3497  }
3498  else {
3499  switch (BUILTIN_TYPE(val)) {
3500  case T_FLOAT:
3501  return rb_float_noflonum_value(val);
3502  case T_BIGNUM:
3503  if (basic_to_f_p(rb_cInteger))
3504  return big2dbl_without_to_f(val);
3505  break;
3506  case T_RATIONAL:
3507  if (basic_to_f_p(rb_cRational))
3508  return rat2dbl_without_to_f(val);
3509  break;
3510  }
3511  }
3512  val = numeric_to_float(val);
3513  return RFLOAT_VALUE(val);
3514 }
3515 
3523 double
3525 {
3526  if (SPECIAL_CONST_P(val)) {
3527  if (FIXNUM_P(val)) {
3528  return fix2dbl_without_to_f(val);
3529  }
3530  else if (FLONUM_P(val)) {
3531  return rb_float_flonum_value(val);
3532  }
3533  else {
3534  implicit_conversion_to_float(val);
3535  }
3536  }
3537  else {
3538  switch (BUILTIN_TYPE(val)) {
3539  case T_FLOAT:
3540  return rb_float_noflonum_value(val);
3541  case T_BIGNUM:
3542  return big2dbl_without_to_f(val);
3543  case T_RATIONAL:
3544  return rat2dbl_without_to_f(val);
3545  case T_STRING:
3546  rb_raise(rb_eTypeError, "no implicit conversion to float from string");
3547  }
3548  }
3549  val = rb_convert_type(val, T_FLOAT, "Float", "to_f");
3550  return RFLOAT_VALUE(val);
3551 }
3552 
3559 VALUE
3561 {
3562  VALUE tmp = rb_check_string_type(val);
3563  if (NIL_P(tmp))
3564  tmp = rb_convert_type_with_id(val, T_STRING, "String", idTo_s);
3565  return tmp;
3566 }
3567 
3568 
3569 /*
3570  * call-seq:
3571  * String(arg) -> string
3572  *
3573  * Returns <i>arg</i> as a <code>String</code>.
3574  *
3575  * First tries to call its <code>to_str</code> method, then its <code>to_s</code> method.
3576  *
3577  * String(self) #=> "main"
3578  * String(self.class) #=> "Object"
3579  * String(123456) #=> "123456"
3580  */
3581 
3582 static VALUE
3583 rb_f_string(VALUE obj, VALUE arg)
3584 {
3585  return rb_String(arg);
3586 }
3587 
3591 VALUE
3593 {
3594  VALUE tmp = rb_check_array_type(val);
3595 
3596  if (NIL_P(tmp)) {
3597  tmp = rb_check_convert_type_with_id(val, T_ARRAY, "Array", idTo_a);
3598  if (NIL_P(tmp)) {
3599  return rb_ary_new3(1, val);
3600  }
3601  }
3602  return tmp;
3603 }
3604 
3605 /*
3606  * call-seq:
3607  * Array(arg) -> array
3608  *
3609  * Returns +arg+ as an Array.
3610  *
3611  * First tries to call <code>to_ary</code> on +arg+, then <code>to_a</code>.
3612  * If +arg+ does not respond to <code>to_ary</code> or <code>to_a</code>,
3613  * returns an Array of length 1 containing +arg+.
3614  *
3615  * If <code>to_ary</code> or <code>to_a</code> returns something other than
3616  * an Array, raises a <code>TypeError</code>.
3617  *
3618  * Array(["a", "b"]) #=> ["a", "b"]
3619  * Array(1..5) #=> [1, 2, 3, 4, 5]
3620  * Array(key: :value) #=> [[:key, :value]]
3621  * Array(nil) #=> []
3622  * Array(1) #=> [1]
3623  */
3624 
3625 static VALUE
3626 rb_f_array(VALUE obj, VALUE arg)
3627 {
3628  return rb_Array(arg);
3629 }
3630 
3634 VALUE
3636 {
3637  VALUE tmp;
3638 
3639  if (NIL_P(val)) return rb_hash_new();
3640  tmp = rb_check_hash_type(val);
3641  if (NIL_P(tmp)) {
3642  if (RB_TYPE_P(val, T_ARRAY) && RARRAY_LEN(val) == 0)
3643  return rb_hash_new();
3644  rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
3645  }
3646  return tmp;
3647 }
3648 
3649 /*
3650  * call-seq:
3651  * Hash(arg) -> hash
3652  *
3653  * Converts <i>arg</i> to a <code>Hash</code> by calling
3654  * <i>arg</i><code>.to_hash</code>. Returns an empty <code>Hash</code> when
3655  * <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>.
3656  *
3657  * Hash([]) #=> {}
3658  * Hash(nil) #=> {}
3659  * Hash(key: :value) #=> {:key => :value}
3660  * Hash([1, 2, 3]) #=> TypeError
3661  */
3662 
3663 static VALUE
3664 rb_f_hash(VALUE obj, VALUE arg)
3665 {
3666  return rb_Hash(arg);
3667 }
3668 
3670 struct dig_method {
3671  VALUE klass;
3672  int basic;
3673 };
3674 
3675 static ID id_dig;
3676 
3677 static int
3678 dig_basic_p(VALUE obj, struct dig_method *cache)
3679 {
3680  VALUE klass = RBASIC_CLASS(obj);
3681  if (klass != cache->klass) {
3682  cache->klass = klass;
3683  cache->basic = rb_method_basic_definition_p(klass, id_dig);
3684  }
3685  return cache->basic;
3686 }
3687 
3688 static void
3689 no_dig_method(int found, VALUE recv, ID mid, int argc, const VALUE *argv, VALUE data)
3690 {
3691  if (!found) {
3692  rb_raise(rb_eTypeError, "%"PRIsVALUE" does not have #dig method",
3693  CLASS_OF(data));
3694  }
3695 }
3696 
3698 VALUE
3699 rb_obj_dig(int argc, VALUE *argv, VALUE obj, VALUE notfound)
3700 {
3701  struct dig_method hash = {Qnil}, ary = {Qnil}, strt = {Qnil};
3702 
3703  for (; argc > 0; ++argv, --argc) {
3704  if (NIL_P(obj)) return notfound;
3705  if (!SPECIAL_CONST_P(obj)) {
3706  switch (BUILTIN_TYPE(obj)) {
3707  case T_HASH:
3708  if (dig_basic_p(obj, &hash)) {
3709  obj = rb_hash_aref(obj, *argv);
3710  continue;
3711  }
3712  break;
3713  case T_ARRAY:
3714  if (dig_basic_p(obj, &ary)) {
3715  obj = rb_ary_at(obj, *argv);
3716  continue;
3717  }
3718  break;
3719  case T_STRUCT:
3720  if (dig_basic_p(obj, &strt)) {
3721  obj = rb_struct_lookup(obj, *argv);
3722  continue;
3723  }
3724  break;
3725  }
3726  }
3727  return rb_check_funcall_with_hook(obj, id_dig, argc, argv,
3728  no_dig_method, obj);
3729  }
3730  return obj;
3731 }
3732 
3733 /*
3734  * Document-class: Class
3735  *
3736  * Classes in Ruby are first-class objects---each is an instance of
3737  * class <code>Class</code>.
3738  *
3739  * Typically, you create a new class by using:
3740  *
3741  * class Name
3742  * # some code describing the class behavior
3743  * end
3744  *
3745  * When a new class is created, an object of type Class is initialized and
3746  * assigned to a global constant (<code>Name</code> in this case).
3747  *
3748  * When <code>Name.new</code> is called to create a new object, the
3749  * <code>new</code> method in <code>Class</code> is run by default.
3750  * This can be demonstrated by overriding <code>new</code> in
3751  * <code>Class</code>:
3752  *
3753  * class Class
3754  * alias old_new new
3755  * def new(*args)
3756  * print "Creating a new ", self.name, "\n"
3757  * old_new(*args)
3758  * end
3759  * end
3760  *
3761  * class Name
3762  * end
3763  *
3764  * n = Name.new
3765  *
3766  * <em>produces:</em>
3767  *
3768  * Creating a new Name
3769  *
3770  * Classes, modules, and objects are interrelated. In the diagram
3771  * that follows, the vertical arrows represent inheritance, and the
3772  * parentheses metaclasses. All metaclasses are instances
3773  * of the class `Class'.
3774  * +---------+ +-...
3775  * | | |
3776  * BasicObject-----|-->(BasicObject)-------|-...
3777  * ^ | ^ |
3778  * | | | |
3779  * Object---------|----->(Object)---------|-...
3780  * ^ | ^ |
3781  * | | | |
3782  * +-------+ | +--------+ |
3783  * | | | | | |
3784  * | Module-|---------|--->(Module)-|-...
3785  * | ^ | | ^ |
3786  * | | | | | |
3787  * | Class-|---------|---->(Class)-|-...
3788  * | ^ | | ^ |
3789  * | +---+ | +----+
3790  * | |
3791  * obj--->OtherClass---------->(OtherClass)-----------...
3792  *
3793  */
3794 
3795 
3796 /* Document-class: BasicObject
3797  *
3798  * BasicObject is the parent class of all classes in Ruby. It's an explicit
3799  * blank class.
3800  *
3801  * BasicObject can be used for creating object hierarchies independent of
3802  * Ruby's object hierarchy, proxy objects like the Delegator class, or other
3803  * uses where namespace pollution from Ruby's methods and classes must be
3804  * avoided.
3805  *
3806  * To avoid polluting BasicObject for other users an appropriately named
3807  * subclass of BasicObject should be created instead of directly modifying
3808  * BasicObject:
3809  *
3810  * class MyObjectSystem < BasicObject
3811  * end
3812  *
3813  * BasicObject does not include Kernel (for methods like +puts+) and
3814  * BasicObject is outside of the namespace of the standard library so common
3815  * classes will not be found without using a full class path.
3816  *
3817  * A variety of strategies can be used to provide useful portions of the
3818  * standard library to subclasses of BasicObject. A subclass could
3819  * <code>include Kernel</code> to obtain +puts+, +exit+, etc. A custom
3820  * Kernel-like module could be created and included or delegation can be used
3821  * via #method_missing:
3822  *
3823  * class MyObjectSystem < BasicObject
3824  * DELEGATE = [:puts, :p]
3825  *
3826  * def method_missing(name, *args, &block)
3827  * super unless DELEGATE.include? name
3828  * ::Kernel.send(name, *args, &block)
3829  * end
3830  *
3831  * def respond_to_missing?(name, include_private = false)
3832  * DELEGATE.include?(name) or super
3833  * end
3834  * end
3835  *
3836  * Access to classes and modules from the Ruby standard library can be
3837  * obtained in a BasicObject subclass by referencing the desired constant
3838  * from the root like <code>::File</code> or <code>::Enumerator</code>.
3839  * Like #method_missing, #const_missing can be used to delegate constant
3840  * lookup to +Object+:
3841  *
3842  * class MyObjectSystem < BasicObject
3843  * def self.const_missing(name)
3844  * ::Object.const_get(name)
3845  * end
3846  * end
3847  */
3848 
3849 /* Document-class: Object
3850  *
3851  * Object is the default root of all Ruby objects. Object inherits from
3852  * BasicObject which allows creating alternate object hierarchies. Methods
3853  * on Object are available to all classes unless explicitly overridden.
3854  *
3855  * Object mixes in the Kernel module, making the built-in kernel functions
3856  * globally accessible. Although the instance methods of Object are defined
3857  * by the Kernel module, we have chosen to document them here for clarity.
3858  *
3859  * When referencing constants in classes inheriting from Object you do not
3860  * need to use the full namespace. For example, referencing +File+ inside
3861  * +YourClass+ will find the top-level File class.
3862  *
3863  * In the descriptions of Object's methods, the parameter <i>symbol</i> refers
3864  * to a symbol, which is either a quoted string or a Symbol (such as
3865  * <code>:name</code>).
3866  */
3867 
3889 void
3890 InitVM_Object(void)
3891 {
3893 
3894 #if 0
3895  // teach RDoc about these classes
3896  rb_cBasicObject = rb_define_class("BasicObject", Qnil);
3898  rb_cModule = rb_define_class("Module", rb_cObject);
3899  rb_cClass = rb_define_class("Class", rb_cModule);
3900 #endif
3901 
3902 #undef rb_intern
3903 #define rb_intern(str) rb_intern_const(str)
3904 
3905  rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0);
3906  rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
3907  rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
3908  rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
3909  rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
3911 
3912  rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1);
3913  rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1);
3914  rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
3915 
3916  /* Document-module: Kernel
3917  *
3918  * The Kernel module is included by class Object, so its methods are
3919  * available in every Ruby object.
3920  *
3921  * The Kernel instance methods are documented in class Object while the
3922  * module methods are documented here. These methods are called without a
3923  * receiver and thus can be called in functional form:
3924  *
3925  * sprintf "%.1f", 1.234 #=> "1.2"
3926  *
3927  */
3928  rb_mKernel = rb_define_module("Kernel");
3930  rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1);
3931  rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1);
3932  rb_define_private_method(rb_cModule, "extended", rb_obj_dummy, 1);
3933  rb_define_private_method(rb_cModule, "prepended", rb_obj_dummy, 1);
3934  rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
3935  rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
3936  rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
3937 
3938  rb_define_method(rb_mKernel, "nil?", rb_false, 0);
3939  rb_define_method(rb_mKernel, "===", rb_equal, 1);
3940  rb_define_method(rb_mKernel, "=~", rb_obj_match, 1);
3941  rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
3942  rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
3944  rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
3945 
3947  rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
3948  rb_define_method(rb_mKernel, "clone", rb_obj_clone2, -1);
3950  rb_define_method(rb_mKernel, "itself", rb_obj_itself, 0);
3951  rb_define_method(rb_mKernel, "yield_self", rb_obj_yield_self, 0);
3952  rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
3953  rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
3954  rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1);
3955 
3957  rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
3958  rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
3959  rb_define_method(rb_mKernel, "untrust", rb_obj_untrust, 0);
3960  rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0);
3962  rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
3964 
3966  rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
3967  rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */
3968  rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
3969  rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
3970  rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */
3971  rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */
3972  rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
3973  rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
3974  rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
3975  rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
3976  rb_define_method(rb_mKernel, "remove_instance_variable",
3977  rb_obj_remove_instance_variable, 1); /* in variable.c */
3978 
3979  rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
3982  rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
3983 
3984  rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */
3985  rb_define_global_function("format", rb_f_sprintf, -1); /* in sprintf.c */
3986 
3987  rb_define_global_function("Integer", rb_f_integer, -1);
3988  rb_define_global_function("Float", rb_f_float, 1);
3989 
3990  rb_define_global_function("String", rb_f_string, 1);
3991  rb_define_global_function("Array", rb_f_array, 1);
3992  rb_define_global_function("Hash", rb_f_hash, 1);
3993 
3994  rb_cNilClass = rb_define_class("NilClass", rb_cObject);
3995  rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
3996  rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
3997  rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
3998  rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
3999  rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0);
4000  rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
4001  rb_define_method(rb_cNilClass, "&", false_and, 1);
4002  rb_define_method(rb_cNilClass, "|", false_or, 1);
4003  rb_define_method(rb_cNilClass, "^", false_xor, 1);
4005 
4006  rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
4009  /*
4010  * An obsolete alias of +nil+
4011  */
4012  rb_define_global_const("NIL", Qnil);
4014 
4015  rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
4016  rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
4017  rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
4018  rb_define_method(rb_cModule, "<=>", rb_mod_cmp, 1);
4019  rb_define_method(rb_cModule, "<", rb_mod_lt, 1);
4021  rb_define_method(rb_cModule, ">", rb_mod_gt, 1);
4022  rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
4023  rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
4024  rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
4025  rb_define_alias(rb_cModule, "inspect", "to_s");
4026  rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
4027  rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
4028  rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */
4029  rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
4030 
4031  rb_define_private_method(rb_cModule, "attr", rb_mod_attr, -1);
4032  rb_define_private_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
4033  rb_define_private_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
4034  rb_define_private_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
4035 
4036  rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
4037  rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
4038  rb_define_method(rb_cModule, "initialize_clone", rb_mod_initialize_clone, 1);
4039  rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
4040  rb_define_method(rb_cModule, "public_instance_methods",
4041  rb_class_public_instance_methods, -1); /* in class.c */
4042  rb_define_method(rb_cModule, "protected_instance_methods",
4043  rb_class_protected_instance_methods, -1); /* in class.c */
4044  rb_define_method(rb_cModule, "private_instance_methods",
4045  rb_class_private_instance_methods, -1); /* in class.c */
4046 
4047  rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
4048  rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
4049  rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
4050  rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
4051  rb_define_private_method(rb_cModule, "remove_const",
4052  rb_mod_remove_const, 1); /* in variable.c */
4053  rb_define_method(rb_cModule, "const_missing",
4054  rb_mod_const_missing, 1); /* in variable.c */
4055  rb_define_method(rb_cModule, "class_variables",
4056  rb_mod_class_variables, -1); /* in variable.c */
4057  rb_define_method(rb_cModule, "remove_class_variable",
4058  rb_mod_remove_cvar, 1); /* in variable.c */
4059  rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
4060  rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
4061  rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
4062  rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); /* in variable.c */
4063  rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); /* in variable.c */
4064  rb_define_method(rb_cModule, "deprecate_constant", rb_mod_deprecate_constant, -1); /* in variable.c */
4065  rb_define_method(rb_cModule, "singleton_class?", rb_mod_singleton_p, 0);
4066 
4067  rb_define_method(rb_cClass, "allocate", rb_class_alloc, 0);
4068  rb_define_method(rb_cClass, "new", rb_class_s_new, -1);
4069  rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
4070  rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
4071  rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
4072  rb_undef_method(rb_cClass, "extend_object");
4073  rb_undef_method(rb_cClass, "append_features");
4074  rb_undef_method(rb_cClass, "prepend_features");
4075 
4076  /*
4077  * Document-class: Data
4078  *
4079  * This is a recommended base class for C extensions using Data_Make_Struct
4080  * or Data_Wrap_Struct, see doc/extension.rdoc for details.
4081  */
4082  rb_cData = rb_define_class("Data", rb_cObject);
4084 
4085  rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
4086  rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
4087  rb_define_alias(rb_cTrueClass, "inspect", "to_s");
4088  rb_define_method(rb_cTrueClass, "&", true_and, 1);
4089  rb_define_method(rb_cTrueClass, "|", true_or, 1);
4090  rb_define_method(rb_cTrueClass, "^", true_xor, 1);
4094  /*
4095  * An obsolete alias of +true+
4096  */
4097  rb_define_global_const("TRUE", Qtrue);
4099 
4100  rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
4101  rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
4102  rb_define_alias(rb_cFalseClass, "inspect", "to_s");
4103  rb_define_method(rb_cFalseClass, "&", false_and, 1);
4104  rb_define_method(rb_cFalseClass, "|", false_or, 1);
4105  rb_define_method(rb_cFalseClass, "^", false_xor, 1);
4109  /*
4110  * An obsolete alias of +false+
4111  */
4112  rb_define_global_const("FALSE", Qfalse);
4114 }
4115 
4116 void
4118 {
4119  id_to_f = rb_intern_const("to_f");
4120  id_dig = rb_intern_const("dig");
4121  InitVM(Object);
4122 }
4123 
void rb_define_global_const(const char *, VALUE)
Definition: variable.c:2702
#define RBASIC_CLEAR_CLASS(obj)
Definition: internal.h:1469
VALUE rb_cvar_get(VALUE, ID)
Definition: variable.c:2879
#define T_SYMBOL
Definition: ruby.h:508
#define T_OBJECT
Definition: ruby.h:491
#define ISDIGIT(c)
Definition: ruby.h:2150
VALUE rb_mod_module_exec(int, const VALUE *, VALUE)
Definition: vm_eval.c:1776
ID rb_check_id(volatile VALUE *)
Returns ID for the given name if it is interned already, or 0.
Definition: symbol.c:915
VALUE rb_check_to_float(VALUE val)
Tries to convert an object into Float.
Definition: object.c:3465
int rb_is_instance_id(ID id)
Definition: symbol.c:838
#define FL_EXIVAR
Definition: ruby.h:1215
void rb_bug(const char *fmt,...)
Definition: error.c:521
VALUE rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1294
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Definition: class.c:1053
#define RARRAY_LEN(a)
Definition: ruby.h:1019
#define FALSE
Definition: nkf.h:174
VALUE rb_mod_public_constant(int argc, const VALUE *argv, VALUE obj)
Definition: variable.c:2786
void rb_check_inheritable(VALUE super)
Ensures a class can be derived from super.
Definition: class.c:220
VALUE rb_mod_name(VALUE)
Definition: variable.c:229
VALUE rb_mod_const_missing(VALUE klass, VALUE name)
Definition: variable.c:1792
#define NUM2INT(x)
Definition: ruby.h:684
VALUE rb_check_to_int(VALUE val)
Tries to convert val into Integer.
Definition: object.c:3099
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:675
VALUE rb_cTrueClass
TrueClass class.
Definition: object.c:39
void Init_Object(void)
Definition: object.c:4117
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:835
VALUE rb_f_sprintf(int, const VALUE *)
Definition: sprintf.c:458
#define DBL_DIG
Definition: numeric.c:54
void rb_obj_infect(VALUE victim, VALUE carrier)
Convenient function to infect victim with the taintedness of carrier.
Definition: object.c:1296
#define rb_usascii_str_new2
Definition: intern.h:841
#define FL_TAINT
Definition: ruby.h:1213
#define CLASS_OF(v)
Definition: ruby.h:453
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2284
#define rb_name_err_raise_str(mesg, recv, name)
Definition: internal.h:1166
#define InitVM(ext)
Definition: ruby.h:2164
#define T_MODULE
Definition: ruby.h:494
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:1847
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1256
#define Qtrue
Definition: ruby.h:437
#define rb_id2str(id)
Definition: vm_backtrace.c:29
#define CLASS_OR_MODULE_P(obj)
Definition: eval.c:37
Definition: st.h:99
VALUE rb_mod_ancestors(VALUE mod)
Definition: class.c:1085
#define OBJ_FREEZE(x)
Definition: ruby.h:1306
const int id
Definition: nkf.c:209
VALUE rb_obj_not_equal(VALUE obj1, VALUE obj2)
call-seq: obj != other -> true or false
Definition: object.c:236
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Definition: symbol.c:984
int rb_is_const_id(ID id)
Definition: symbol.c:820
void rb_copy_wb_protected_attribute(VALUE dest, VALUE obj)
Definition: gc.c:6104
VALUE rb_str_escape(VALUE str)
Definition: string.c:5737
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1527
void rb_check_trusted(VALUE obj)
Definition: error.c:2622
#define T_RATIONAL
Definition: ruby.h:509
#define rb_check_arity
Definition: intern.h:298
VALUE rb_check_convert_type_with_id(VALUE, int, const char *, ID)
Definition: object.c:3022
rb_encoding * rb_default_internal_encoding(void)
Definition: encoding.c:1510
void rb_cvar_set(VALUE, ID, VALUE)
Definition: variable.c:2846
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:2999
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
Definition: class.c:314
VALUE rb_obj_init_dup_clone(VALUE obj, VALUE orig)
:nodoc: Default implementation of #initialize_dup and #initialize_clone
Definition: object.c:611
double rb_cstr_to_dbl(const char *p, int badcheck)
Parses a string representation of a floating point number.
Definition: object.c:3208
VALUE rb_String(VALUE val)
Equivalent to Kernel#String in Ruby.
Definition: object.c:3560
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
void Init_class_hierarchy(void)
Definition: class.c:546
#define RBASIC_SET_CLASS(obj, cls)
Definition: internal.h:1471
#define Check_Type(v, t)
Definition: ruby.h:562
VALUE rb_obj_dup(VALUE obj)
call-seq: obj.dup -> an_object
Definition: object.c:526
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1210
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4709
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_Integer(VALUE val)
Equivalent to Kernel#Integer in Ruby.
Definition: object.c:3148
#define T_HASH
Definition: ruby.h:499
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of klass.
Definition: object.c:2121
int rb_const_defined(VALUE, ID)
Definition: variable.c:2537
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:3006
VALUE rb_obj_untrusted(VALUE obj)
call-seq: obj.untrusted? -> true or false
Definition: object.c:1234
VALUE rb_cBasicObject
BasicObject class.
Definition: object.c:31
#define id_for_var(obj, name, part, type)
Definition: object.c:2235
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:864
VALUE rb_refinement_module_get_refined_class(VALUE module)
Definition: eval.c:1388
#define FL_UNSET(x, f)
Definition: ruby.h:1290
#define T_ARRAY
Definition: ruby.h:498
#define PUREFUNC(x)
Definition: defines.h:30
#define RGENGC_WB_PROTECTED_OBJECT
Definition: ruby.h:783
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1745
VALUE rb_obj_init_copy(VALUE obj, VALUE orig)
:nodoc: Default implementation of #initialize_copy
Definition: object.c:590
void rb_obj_call_init(VALUE obj, int argc, const VALUE *argv)
Calls #initialize method of obj with the given arguments.
Definition: eval.c:1583
#define FIXNUM_P(f)
Definition: ruby.h:365
VALUE rb_inspect(VALUE obj)
Convenient wrapper of Object::inspect.
Definition: object.c:656
#define FL_TEST(x, f)
Definition: ruby.h:1282
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1533
VALUE rb_ivar_defined(VALUE, ID)
Definition: variable.c:1374
#define rb_name_err_raise(mesg, recv, name)
Definition: internal.h:1168
VALUE rb_mod_remove_cvar(VALUE, VALUE)
Definition: variable.c:3066
#define NORETURN(x)
Definition: defines.h:34
void rb_ivar_foreach(VALUE, int(*)(ANYARGS), st_data_t)
Definition: variable.c:1544
const char * rb_obj_classname(VALUE)
Definition: variable.c:459
VALUE rb_equal_opt(VALUE obj1, VALUE obj2)
#define rb_ary_new2
Definition: intern.h:90
#define OutOfRange()
VALUE rb_obj_untrust(VALUE obj)
call-seq: obj.untrust -> obj
Definition: object.c:1257
VALUE rb_cClass
Class class.
Definition: object.c:35
VALUE rb_eArgError
Definition: error.c:802
#define sym(x)
Definition: date_core.c:3721
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:22
#define NEWOBJ_OF(obj, type, klass, flags)
Definition: ruby.h:754
#define FL_SINGLETON
Definition: ruby.h:1208
VALUE rb_to_float(VALUE val)
Converts a Numeric object into Float.
Definition: object.c:3448
VALUE rb_cNilClass
NilClass class.
Definition: object.c:38
#define strtod(s, e)
Definition: util.h:77
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1689
VALUE rb_obj_class(VALUE obj)
call-seq: obj.class -> class
Definition: object.c:277
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
VALUE rb_obj_is_kind_of(VALUE obj, VALUE c)
call-seq: obj.is_a?(class) -> true or false obj.kind_of?(class) -> true or false
Definition: object.c:842
rb_encoding * rb_default_external_encoding(void)
Definition: encoding.c:1425
VALUE rb_obj_tainted(VALUE obj)
call-seq: obj.tainted? -> true or false
Definition: object.c:1147
VALUE rb_cvar_defined(VALUE, ID)
Definition: variable.c:2906
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition: class.c:620
VALUE rb_obj_taint(VALUE obj)
call-seq: obj.taint -> obj
Definition: object.c:1179
#define ROBJECT_IVPTR(o)
Definition: ruby.h:904
#define rb_intern_str(string)
Definition: generator.h:16
VALUE rb_equal(VALUE obj1, VALUE obj2)
call-seq: obj === other -> true or false
Definition: object.c:126
VALUE rb_class_name(VALUE)
Definition: variable.c:444
VALUE rb_dbl2big(double d)
Definition: bignum.c:5214
#define ALLOC_N(type, n)
Definition: ruby.h:1587
VALUE rb_convert_type_with_id(VALUE, int, const char *, ID)
Definition: object.c:2979
void rb_gc_copy_finalizer(VALUE dest, VALUE obj)
Definition: gc.c:2756
#define val
#define id_for_setter(obj, name, type, message)
Definition: object.c:2238
VALUE rb_obj_dig(int argc, VALUE *argv, VALUE self, VALUE notfound)
Definition: object.c:3699
double rb_num_to_dbl(VALUE val)
Definition: object.c:3484
VALUE rb_ary_at(VALUE ary, VALUE pos)
Definition: array.c:1332
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
Definition: bignum.c:4226
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:1137
VALUE rb_str_cat2(VALUE, const char *)
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1410
VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1379
VALUE rb_any_to_s(VALUE obj)
call-seq: obj.to_s -> string
Definition: object.c:631
#define M(n)
#define RCLASS_ORIGIN(c)
Definition: internal.h:794
VALUE rb_check_to_integer(VALUE val, const char *method)
Tries to convert val into Integer.
Definition: object.c:3062
#define NIL_P(v)
Definition: ruby.h:451
void rb_invalid_str(const char *str, const char *type)
Definition: error.c:1549
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
#define FLONUM_P(x)
Definition: ruby.h:399
VALUE rb_mKernel
Kernel module.
Definition: object.c:32
#define T_FLOAT
Definition: ruby.h:495
#define TYPE(x)
Definition: ruby.h:521
int argc
Definition: ruby.c:187
VALUE rb_struct_lookup(VALUE s, VALUE idx)
Definition: struct.c:936
#define Qfalse
Definition: ruby.h:436
VALUE rb_immutable_obj_clone(int, VALUE *, VALUE)
Definition: object.c:406
#define T_BIGNUM
Definition: ruby.h:501
VALUE rb_obj_is_instance_of(VALUE obj, VALUE c)
call-seq: obj.instance_of?(class) -> true or false
Definition: object.c:798
#define ISUPPER(c)
Definition: ruby.h:2146
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1661
rb_alloc_func_t rb_get_alloc_func(VALUE)
Definition: vm_method.c:681
void rb_obj_copy_ivar(VALUE dest, VALUE obj)
Definition: object.c:307
VALUE rb_Array(VALUE val)
Equivalent to Kernel#Array in Ruby.
Definition: object.c:3592
#define OBJ_TAINTABLE(x)
Definition: ruby.h:1294
#define T_COMPLEX
Definition: ruby.h:510
VALUE rb_cModule
Module class.
Definition: object.c:34
#define ALLOCV_END(v)
Definition: ruby.h:1658
#define numberof(array)
Definition: etc.c:618
#define RUBY_DTRACE_CREATE_HOOK(name, arg)
Definition: internal.h:1932
#define IMPLICIT_CONVERSIONS
Definition: object.c:2886
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:2292
VALUE rb_str_subseq(VALUE, long, long)
Definition: string.c:2406
#define FL_FINALIZE
Definition: ruby.h:1212
#define rb_intern(str)
#define FL_PROMOTED1
Definition: ruby.h:1211
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1758
#define RSTRING_LEN(str)
Definition: ruby.h:971
VALUE rb_yield(VALUE)
Definition: vm_eval.c:973
#define RCLASS_M_TBL(c)
Definition: internal.h:791
#define FL_PROMOTED0
Definition: ruby.h:1210
VALUE rb_check_funcall_with_hook(VALUE recv, ID mid, int argc, const VALUE *argv, rb_check_funcall_hook *hook, VALUE arg)
Definition: vm_eval.c:417
int errno
void rb_deprecate_constant(VALUE mod, const char *name)
Definition: variable.c:2749
#define TRUE
Definition: nkf.h:175
#define T_DATA
Definition: ruby.h:506
VALUE rb_obj_reveal(VALUE obj, VALUE klass)
Make a hidden object visible again.
Definition: object.c:89
VALUE rb_obj_freeze(VALUE obj)
call-seq: obj.freeze -> obj
Definition: object.c:1331
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1452
int rb_is_const_name(VALUE name)
Definition: symbol.c:1076
VALUE rb_const_missing(VALUE klass, VALUE name)
Definition: variable.c:1747
VALUE rb_class_search_ancestor(VALUE klass, VALUE super)
Definition: object.c:863
VALUE rb_hash_new(void)
Definition: hash.c:424
VALUE rb_Hash(VALUE val)
Equivalent to Kernel#Hash in Ruby.
Definition: object.c:3635
#define id_eq
Definition: numeric.c:176
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1908
VALUE rb_class_inherited_p(VALUE mod, VALUE arg)
call-seq: mod <= other -> true, false, or nil
Definition: object.c:1827
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1315
VALUE rb_check_hash_type(VALUE hash)
Definition: hash.c:722
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1241
VALUE rb_obj_frozen_p(VALUE obj)
call-seq: obj.frozen? -> true or false
Definition: object.c:1360
#define T_STRUCT
Definition: ruby.h:500
#define Qnil
Definition: ruby.h:438
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:2573
#define BUILTIN_TYPE(x)
Definition: ruby.h:518
unsigned long VALUE
Definition: ruby.h:85
#define OBJ_TAINTED(x)
Definition: ruby.h:1296
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
Definition: intern.h:234
#define RBASIC(obj)
Definition: ruby.h:1197
const char * rb_class2name(VALUE)
Definition: variable.c:450
VALUE rb_mod_class_variables(int, const VALUE *, VALUE)
Definition: variable.c:3025
#define FUNC_MINIMIZED(x)
Definition: defines.h:329
RUBY_EXTERN VALUE rb_cInteger
Definition: ruby.h:1912
VALUE rb_eTypeError
Definition: error.c:801
#define bad(x)
Definition: _sdbm.c:124
VALUE rb_make_metaclass(VALUE obj, VALUE unused)
Definition: class.c:577
VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1349
#define rb_ary_new3
Definition: intern.h:91
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:389
const char * rb_id2name(ID)
Definition: symbol.c:751
#define rb_enc_asciicompat(enc)
Definition: encoding.h:239
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition: object.c:72
RUBY_EXTERN VALUE rb_cNumeric
Definition: ruby.h:1919
VALUE rb_obj_trust(VALUE obj)
call-seq: obj.trust -> obj
Definition: object.c:1281
int rb_is_const_sym(VALUE sym)
Definition: symbol.c:862
VALUE rb_obj_remove_instance_variable(VALUE, VALUE)
Definition: variable.c:1687
VALUE rb_str_dup(VALUE)
Definition: string.c:1488
#define FIXABLE(f)
Definition: ruby.h:368
#define RB_FLOAT_TYPE_P(obj)
Definition: ruby.h:523
VALUE rb_fstring_new(const char *ptr, long len)
Definition: string.c:374
#define ROBJECT(obj)
Definition: ruby.h:1198
#define rb_funcallv
Definition: console.c:21
unsigned int uint32_t
Definition: sha2.h:101
register unsigned int len
Definition: zonetab.h:51
VALUE rb_mod_constants(int, const VALUE *, VALUE)
Definition: variable.c:2481
VALUE rb_obj_hash(VALUE obj)
Definition: hash.c:258
#define recur(fmt)
#define RSTRING_PTR(str)
Definition: ruby.h:975
VALUE rb_eql_opt(VALUE obj1, VALUE obj2)
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:2543
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:860
#define RFLOAT_VALUE(v)
Definition: ruby.h:933
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attach a object to a singleton class.
Definition: class.c:421
VALUE rb_yield_values2(int n, const VALUE *argv)
Definition: vm_eval.c:1007
#define f
#define INT2FIX(i)
Definition: ruby.h:232
#define RCLASS_SUPER(c)
Definition: classext.h:16
VALUE rb_module_new(void)
Definition: class.c:749
VALUE rb_cFalseClass
FalseClass class.
Definition: object.c:40
double rb_str_to_dbl(VALUE str, int badcheck)
Parses a string representation of a floating point number.
Definition: object.c:3298
VALUE rb_Float(VALUE val)
Equivalent to Kernel#Float in Ruby.
Definition: object.c:3398
#define RBASIC_CLASS(obj)
Definition: ruby.h:878
VALUE rb_class_superclass(VALUE klass)
call-seq: class.superclass -> a_super_class or nil
Definition: object.c:2204
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:651
VALUE rb_hash_aref(VALUE hash, VALUE key)
Definition: hash.c:831
VALUE rb_convert_type(VALUE val, int type, const char *tname, const char *method)
Converts an object into another type.
Definition: object.c:2965
VALUE rb_str_catf(VALUE str, const char *format,...)
Definition: sprintf.c:1492
#define FL_WB_PROTECTED
Definition: ruby.h:1209
VALUE rb_check_string_type(VALUE)
Definition: string.c:2246
#define LONG2FIX(i)
Definition: ruby.h:234
VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1418
#define ALLOCV(v, n)
Definition: ruby.h:1656
#define RTEST(v)
Definition: ruby.h:450
#define T_STRING
Definition: ruby.h:496
void rb_warning(const char *fmt,...)
Definition: error.c:267
VALUE rb_mod_remove_const(VALUE, VALUE)
Definition: variable.c:2332
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates and initializes an instance of klass.
Definition: object.c:2170
#define OBJ_INFECT(x, s)
Definition: ruby.h:1302
int rb_is_local_name(VALUE name)
Definition: symbol.c:1106
int rb_method_basic_definition_p(VALUE, ID)
Definition: vm_method.c:1879
VALUE rb_mod_private_constant(int argc, const VALUE *argv, VALUE obj)
Definition: variable.c:2772
#define OBJ_FROZEN(x)
Definition: ruby.h:1304
VALUE rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
Definition: class.c:1279
int rb_is_local_id(ID id)
Definition: symbol.c:850
VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1330
double rb_num2dbl(VALUE val)
Converts a Numeric object to double.
Definition: object.c:3524
VALUE rb_obj_untaint(VALUE obj)
call-seq: obj.untaint -> obj
Definition: object.c:1208
#define T_CLASS
Definition: ruby.h:492
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:2298
VALUE rb_cData
Data class.
Definition: object.c:36
RUBY_EXTERN VALUE rb_cRational
Definition: ruby.h:1923
const char * name
Definition: nkf.c:208
#define ID2SYM(x)
Definition: ruby.h:383
int rb_eql(VALUE obj1, VALUE obj2)
Determines if obj1 and obj2 are equal in terms of Object::eql?.
Definition: object.c:149
#define StringValuePtr(v)
Definition: ruby.h:570
VALUE rb_mod_included_modules(VALUE mod)
Definition: class.c:1017
#define FL_FREEZE
Definition: ruby.h:1216
Definition: ruby.h:889
#define QUOTE(str)
Definition: internal.h:1635
#define rb_check_frozen(obj)
Definition: intern.h:271
#define CONST_ID(var, str)
Definition: ruby.h:1743
VALUE rb_str_intern(VALUE)
Definition: symbol.c:661
#define rb_intern_const(str)
Definition: ruby.h:1777
void rb_copy_generic_ivar(VALUE, VALUE)
Definition: variable.c:1502
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1242
void void xfree(void *)
VALUE rb_define_module(const char *name)
Definition: class.c:768
int rb_enc_str_asciionly_p(VALUE)
Definition: string.c:641
VALUE rb_usascii_str_new(const char *, long)
Definition: string.c:743
#define SYMBOL_P(x)
Definition: ruby.h:382
#define RCLASS(obj)
Definition: ruby.h:1199
#define mod(x, y)
Definition: date_strftime.c:28
#define T_NONE
Definition: ruby.h:489
#define RB_INTEGER_TYPE_P(obj)
Definition: ruby_missing.h:15
VALUE(* rb_alloc_func_t)(VALUE)
Definition: intern.h:370
#define NULL
Definition: _sdbm.c:102
VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1364
#define Qundef
Definition: ruby.h:439
#define T_ICLASS
Definition: ruby.h:493
VALUE rb_class_real(VALUE cl)
Looks up the nearest ancestor of cl, skipping singleton classes or module inclusions.
Definition: object.c:251
#define OBJ_TAINT(x)
Definition: ruby.h:1298
VALUE rb_obj_clone(VALUE obj)
:nodoc Almost same as Object::clone ++
Definition: object.c:475
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2900
VALUE rb_obj_setup(VALUE obj, VALUE klass, VALUE type)
Fills common (RBasic) fields in obj.
Definition: object.c:106
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition: class.c:201
#define rb_obj_instance_variables(object)
Definition: generator.h:20
VALUE rb_cObject
Object class.
Definition: object.c:33
st_index_t rb_ivar_count(VALUE)
Definition: variable.c:1566
VALUE rb_mod_deprecate_constant(int argc, const VALUE *argv, VALUE obj)
Definition: variable.c:2800
#define T_MASK
Definition: md5.c:131
VALUE rb_to_int(VALUE val)
Converts val into Integer.
Definition: object.c:3084
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1224
char ** argv
Definition: ruby.c:188
#define DBL2NUM(dbl)
Definition: ruby.h:934
#define ISSPACE(c)
Definition: ruby.h:2145
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
Definition: class.c:371
#define StringValue(v)
Definition: ruby.h:569
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:2229