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