Ruby  1.9.3p448(2013-06-27revision41675)
object.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  object.c -
4 
5  $Author: usa $
6  created at: Thu Jul 15 12:01:24 JST 1993
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9  Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10  Copyright (C) 2000 Information-technology Promotion Agency, Japan
11 
12 **********************************************************************/
13 
14 #include "ruby/ruby.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 "internal.h"
24 
31 
35 
38 
39 /*
40  * call-seq:
41  * obj === other -> true or false
42  *
43  * Case Equality---For class <code>Object</code>, effectively the same
44  * as calling <code>#==</code>, but typically overridden by descendants
45  * to provide meaningful semantics in <code>case</code> statements.
46  */
47 
48 VALUE
49 rb_equal(VALUE obj1, VALUE obj2)
50 {
51  VALUE result;
52 
53  if (obj1 == obj2) return Qtrue;
54  result = rb_funcall(obj1, id_eq, 1, obj2);
55  if (RTEST(result)) return Qtrue;
56  return Qfalse;
57 }
58 
59 int
60 rb_eql(VALUE obj1, VALUE obj2)
61 {
62  return RTEST(rb_funcall(obj1, id_eql, 1, obj2));
63 }
64 
65 /*
66  * call-seq:
67  * obj == other -> true or false
68  * obj.equal?(other) -> true or false
69  * obj.eql?(other) -> true or false
70  *
71  * Equality---At the <code>Object</code> level, <code>==</code> returns
72  * <code>true</code> only if <i>obj</i> and <i>other</i> are the
73  * same object. Typically, this method is overridden in descendant
74  * classes to provide class-specific meaning.
75  *
76  * Unlike <code>==</code>, the <code>equal?</code> method should never be
77  * overridden by subclasses: it is used to determine object identity
78  * (that is, <code>a.equal?(b)</code> iff <code>a</code> is the same
79  * object as <code>b</code>).
80  *
81  * The <code>eql?</code> method returns <code>true</code> if
82  * <i>obj</i> and <i>anObject</i> have the same value. Used by
83  * <code>Hash</code> to test members for equality. For objects of
84  * class <code>Object</code>, <code>eql?</code> is synonymous with
85  * <code>==</code>. Subclasses normally continue this tradition, but
86  * there are exceptions. <code>Numeric</code> types, for example,
87  * perform type conversion across <code>==</code>, but not across
88  * <code>eql?</code>, so:
89  *
90  * 1 == 1.0 #=> true
91  * 1.eql? 1.0 #=> false
92  */
93 
94 VALUE
96 {
97  if (obj1 == obj2) return Qtrue;
98  return Qfalse;
99 }
100 
101 /*
102  * Generates a <code>Fixnum</code> hash value for this object.
103  * This function must have the property that a.eql?(b) implies
104  * a.hash <code>==</code> b.hash.
105  * The hash value is used by class <code>Hash</code>.
106  * Any hash value that exceeds the capacity of a <code>Fixnum</code> will be
107  * truncated before being used.
108  *
109  * "waffle".hash #=> -910576647
110  */
111 VALUE
113 {
114  VALUE oid = rb_obj_id(obj);
115 #if SIZEOF_LONG == SIZEOF_VOIDP
116  st_index_t index = NUM2LONG(oid);
117 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
118  st_index_t index = NUM2LL(oid);
119 #else
120 # error not supported
121 #endif
123  return LONG2FIX(h);
124 }
125 
126 /*
127  * call-seq:
128  * !obj -> true or false
129  *
130  * Boolean negate.
131  */
132 
133 VALUE
135 {
136  return RTEST(obj) ? Qfalse : Qtrue;
137 }
138 
139 /*
140  * call-seq:
141  * obj != other -> true or false
142  *
143  * Returns true if two objects are not-equal, otherwise false.
144  */
145 
146 VALUE
148 {
149  VALUE result = rb_funcall(obj1, id_eq, 1, obj2);
150  return RTEST(result) ? Qfalse : Qtrue;
151 }
152 
153 VALUE
155 {
156  if (cl == 0)
157  return 0;
158  while ((RBASIC(cl)->flags & FL_SINGLETON) || BUILTIN_TYPE(cl) == T_ICLASS) {
159  cl = RCLASS_SUPER(cl);
160  }
161  return cl;
162 }
163 
164 /*
165  * call-seq:
166  * obj.class -> class
167  *
168  * Returns the class of <i>obj</i>. This method must always be
169  * called with an explicit receiver, as <code>class</code> is also a
170  * reserved word in Ruby.
171  *
172  * 1.class #=> Fixnum
173  * self.class #=> Object
174  */
175 
176 VALUE
178 {
179  return rb_class_real(CLASS_OF(obj));
180 }
181 
182 /*
183  * call-seq:
184  * obj.singleton_class -> class
185  *
186  * Returns the singleton class of <i>obj</i>. This method creates
187  * a new singleton class if <i>obj</i> does not have it.
188  *
189  * If <i>obj</i> is <code>nil</code>, <code>true</code>, or
190  * <code>false</code>, it returns NilClass, TrueClass, or FalseClass,
191  * respectively.
192  * If <i>obj</i> is a Fixnum or a Symbol, it raises a TypeError.
193  *
194  * Object.new.singleton_class #=> #<Class:#<Object:0xb7ce1e24>>
195  * String.singleton_class #=> #<Class:String>
196  * nil.singleton_class #=> NilClass
197  */
198 
199 static VALUE
201 {
202  return rb_singleton_class(obj);
203 }
204 
205 static void
207 {
208  if (OBJ_FROZEN(dest)) {
209  rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
210  }
211  RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
212  RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR|FL_TAINT|FL_UNTRUSTED);
213  rb_copy_generic_ivar(dest, obj);
214  rb_gc_copy_finalizer(dest, obj);
215  switch (TYPE(obj)) {
216  case T_OBJECT:
217  if (!(RBASIC(dest)->flags & ROBJECT_EMBED) && ROBJECT_IVPTR(dest)) {
218  xfree(ROBJECT_IVPTR(dest));
219  ROBJECT(dest)->as.heap.ivptr = 0;
220  ROBJECT(dest)->as.heap.numiv = 0;
221  ROBJECT(dest)->as.heap.iv_index_tbl = 0;
222  }
223  if (RBASIC(obj)->flags & ROBJECT_EMBED) {
224  MEMCPY(ROBJECT(dest)->as.ary, ROBJECT(obj)->as.ary, VALUE, ROBJECT_EMBED_LEN_MAX);
225  RBASIC(dest)->flags |= ROBJECT_EMBED;
226  }
227  else {
228  long len = ROBJECT(obj)->as.heap.numiv;
229  VALUE *ptr = ALLOC_N(VALUE, len);
230  MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
231  ROBJECT(dest)->as.heap.ivptr = ptr;
232  ROBJECT(dest)->as.heap.numiv = len;
233  ROBJECT(dest)->as.heap.iv_index_tbl = ROBJECT(obj)->as.heap.iv_index_tbl;
234  RBASIC(dest)->flags &= ~ROBJECT_EMBED;
235  }
236  break;
237  case T_CLASS:
238  case T_MODULE:
239  if (RCLASS_IV_TBL(dest)) {
241  RCLASS_IV_TBL(dest) = 0;
242  }
243  if (RCLASS_CONST_TBL(dest)) {
245  RCLASS_CONST_TBL(dest) = 0;
246  }
247  if (RCLASS_IV_TBL(obj)) {
248  RCLASS_IV_TBL(dest) = st_copy(RCLASS_IV_TBL(obj));
249  }
250  break;
251  }
252 }
253 
254 /*
255  * call-seq:
256  * obj.clone -> an_object
257  *
258  * Produces a shallow copy of <i>obj</i>---the instance variables of
259  * <i>obj</i> are copied, but not the objects they reference. Copies
260  * the frozen and tainted state of <i>obj</i>. See also the discussion
261  * under <code>Object#dup</code>.
262  *
263  * class Klass
264  * attr_accessor :str
265  * end
266  * s1 = Klass.new #=> #<Klass:0x401b3a38>
267  * s1.str = "Hello" #=> "Hello"
268  * s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello">
269  * s2.str[1,4] = "i" #=> "i"
270  * s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
271  * s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
272  *
273  * This method may have class-specific behavior. If so, that
274  * behavior will be documented under the #+initialize_copy+ method of
275  * the class.
276  */
277 
278 VALUE
280 {
281  VALUE clone;
282 
283  if (rb_special_const_p(obj)) {
284  rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
285  }
286  clone = rb_obj_alloc(rb_obj_class(obj));
287  RBASIC(clone)->klass = rb_singleton_class_clone(obj);
288  RBASIC(clone)->flags = (RBASIC(obj)->flags | FL_TEST(clone, FL_TAINT) | FL_TEST(clone, FL_UNTRUSTED)) & ~(FL_FREEZE|FL_FINALIZE|FL_MARK);
289  init_copy(clone, obj);
290  rb_funcall(clone, id_init_clone, 1, obj);
291  RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
292 
293  return clone;
294 }
295 
296 /*
297  * call-seq:
298  * obj.dup -> an_object
299  *
300  * Produces a shallow copy of <i>obj</i>---the instance variables of
301  * <i>obj</i> are copied, but not the objects they reference.
302  * <code>dup</code> copies the tainted state of <i>obj</i>. See also
303  * the discussion under <code>Object#clone</code>. In general,
304  * <code>clone</code> and <code>dup</code> may have different semantics
305  * in descendant classes. While <code>clone</code> is used to duplicate
306  * an object, including its internal state, <code>dup</code> typically
307  * uses the class of the descendant object to create the new instance.
308  *
309  * This method may have class-specific behavior. If so, that
310  * behavior will be documented under the #+initialize_copy+ method of
311  * the class.
312  */
313 
314 VALUE
316 {
317  VALUE dup;
318 
319  if (rb_special_const_p(obj)) {
320  rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj));
321  }
322  dup = rb_obj_alloc(rb_obj_class(obj));
323  init_copy(dup, obj);
324  rb_funcall(dup, id_init_dup, 1, obj);
325 
326  return dup;
327 }
328 
329 /* :nodoc: */
330 VALUE
332 {
333  if (obj == orig) return obj;
334  rb_check_frozen(obj);
335  if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
336  rb_raise(rb_eTypeError, "initialize_copy should take same class object");
337  }
338  return obj;
339 }
340 
341 /* :nodoc: */
342 VALUE
344 {
345  rb_funcall(obj, id_init_copy, 1, orig);
346  return obj;
347 }
348 
349 /*
350  * call-seq:
351  * obj.to_s -> string
352  *
353  * Returns a string representing <i>obj</i>. The default
354  * <code>to_s</code> prints the object's class and an encoding of the
355  * object id. As a special case, the top-level object that is the
356  * initial execution context of Ruby programs returns ``main.''
357  */
358 
359 VALUE
361 {
362  const char *cname = rb_obj_classname(obj);
363  VALUE str;
364 
365  str = rb_sprintf("#<%s:%p>", cname, (void*)obj);
366  OBJ_INFECT(str, obj);
367 
368  return str;
369 }
370 
371 VALUE
373 {
374  return rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
375 }
376 
377 static int
378 inspect_i(ID id, VALUE value, VALUE str)
379 {
380  VALUE str2;
381  const char *ivname;
382 
383  /* need not to show internal data */
384  if (CLASS_OF(value) == 0) return ST_CONTINUE;
385  if (!rb_is_instance_id(id)) return ST_CONTINUE;
386  if (RSTRING_PTR(str)[0] == '-') { /* first element */
387  RSTRING_PTR(str)[0] = '#';
388  rb_str_cat2(str, " ");
389  }
390  else {
391  rb_str_cat2(str, ", ");
392  }
393  ivname = rb_id2name(id);
394  rb_str_cat2(str, ivname);
395  rb_str_cat2(str, "=");
396  str2 = rb_inspect(value);
397  rb_str_append(str, str2);
398  OBJ_INFECT(str, str2);
399 
400  return ST_CONTINUE;
401 }
402 
403 static VALUE
404 inspect_obj(VALUE obj, VALUE str, int recur)
405 {
406  if (recur) {
407  rb_str_cat2(str, " ...");
408  }
409  else {
410  rb_ivar_foreach(obj, inspect_i, str);
411  }
412  rb_str_cat2(str, ">");
413  RSTRING_PTR(str)[0] = '#';
414  OBJ_INFECT(str, obj);
415 
416  return str;
417 }
418 
419 /*
420  * call-seq:
421  * obj.inspect -> string
422  *
423  * Returns a string containing a human-readable representation of
424  * <i>obj</i>. If not overridden and no instance variables, uses the
425  * <code>to_s</code> method to generate the string.
426  * <i>obj</i>. If not overridden, uses the <code>to_s</code> method to
427  * generate the string.
428  *
429  * [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
430  * Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
431  */
432 
433 static VALUE
435 {
436  if (TYPE(obj) == T_OBJECT && rb_obj_basic_to_s_p(obj)) {
437  int has_ivar = 0;
438  VALUE *ptr = ROBJECT_IVPTR(obj);
439  long len = ROBJECT_NUMIV(obj);
440  long i;
441 
442  for (i = 0; i < len; i++) {
443  if (ptr[i] != Qundef) {
444  has_ivar = 1;
445  break;
446  }
447  }
448 
449  if (has_ivar) {
450  VALUE str;
451  const char *c = rb_obj_classname(obj);
452 
453  str = rb_sprintf("-<%s:%p", c, (void*)obj);
454  return rb_exec_recursive(inspect_obj, obj, str);
455  }
456  return rb_any_to_s(obj);
457  }
458  return rb_funcall(obj, rb_intern("to_s"), 0, 0);
459 }
460 
461 
462 /*
463  * call-seq:
464  * obj.instance_of?(class) -> true or false
465  *
466  * Returns <code>true</code> if <i>obj</i> is an instance of the given
467  * class. See also <code>Object#kind_of?</code>.
468  *
469  * class A; end
470  * class B < A; end
471  * class C < B; end
472  *
473  * b = B.new
474  * b.instance_of? A #=> false
475  * b.instance_of? B #=> true
476  * b.instance_of? C #=> false
477  */
478 
479 VALUE
481 {
482  switch (TYPE(c)) {
483  case T_MODULE:
484  case T_CLASS:
485  case T_ICLASS:
486  break;
487  default:
488  rb_raise(rb_eTypeError, "class or module required");
489  }
490 
491  if (rb_obj_class(obj) == c) return Qtrue;
492  return Qfalse;
493 }
494 
495 
496 /*
497  * call-seq:
498  * obj.is_a?(class) -> true or false
499  * obj.kind_of?(class) -> true or false
500  *
501  * Returns <code>true</code> if <i>class</i> is the class of
502  * <i>obj</i>, or if <i>class</i> is one of the superclasses of
503  * <i>obj</i> or modules included in <i>obj</i>.
504  *
505  * module M; end
506  * class A
507  * include M
508  * end
509  * class B < A; end
510  * class C < B; end
511  *
512  * b = B.new
513  * b.is_a? A #=> true
514  * b.is_a? B #=> true
515  * b.is_a? C #=> false
516  * b.is_a? M #=> true
517  *
518  * b.kind_of? A #=> true
519  * b.kind_of? B #=> true
520  * b.kind_of? C #=> false
521  * b.kind_of? M #=> true
522  */
523 
524 VALUE
526 {
527  VALUE cl = CLASS_OF(obj);
528 
529  switch (TYPE(c)) {
530  case T_MODULE:
531  case T_CLASS:
532  case T_ICLASS:
533  break;
534 
535  default:
536  rb_raise(rb_eTypeError, "class or module required");
537  }
538 
539  while (cl) {
540  if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
541  return Qtrue;
542  cl = RCLASS_SUPER(cl);
543  }
544  return Qfalse;
545 }
546 
547 
548 /*
549  * call-seq:
550  * obj.tap{|x|...} -> obj
551  *
552  * Yields <code>x</code> to the block, and then returns <code>x</code>.
553  * The primary purpose of this method is to "tap into" a method chain,
554  * in order to perform operations on intermediate results within the chain.
555  *
556  * (1..10) .tap {|x| puts "original: #{x.inspect}"}
557  * .to_a .tap {|x| puts "array: #{x.inspect}"}
558  * .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"}
559  * .map { |x| x*x } .tap {|x| puts "squares: #{x.inspect}"}
560  *
561  */
562 
563 VALUE
565 {
566  rb_yield(obj);
567  return obj;
568 }
569 
570 
571 /*
572  * Document-method: inherited
573  *
574  * call-seq:
575  * inherited(subclass)
576  *
577  * Callback invoked whenever a subclass of the current class is created.
578  *
579  * Example:
580  *
581  * class Foo
582  * def self.inherited(subclass)
583  * puts "New subclass: #{subclass}"
584  * end
585  * end
586  *
587  * class Bar < Foo
588  * end
589  *
590  * class Baz < Bar
591  * end
592  *
593  * produces:
594  *
595  * New subclass: Bar
596  * New subclass: Baz
597  */
598 
599 /* Document-method: method_added
600  *
601  * call-seq:
602  * method_added(method_name)
603  *
604  * Invoked as a callback whenever an instance method is added to the
605  * receiver.
606  *
607  * module Chatty
608  * def self.method_added(method_name)
609  * puts "Adding #{method_name.inspect}"
610  * end
611  * def self.some_class_method() end
612  * def some_instance_method() end
613  * end
614  *
615  * produces:
616  *
617  * Adding :some_instance_method
618  *
619  */
620 
621 /* Document-method: method_removed
622  *
623  * call-seq:
624  * method_removed(method_name)
625  *
626  * Invoked as a callback whenever an instance method is removed from the
627  * receiver.
628  *
629  * module Chatty
630  * def self.method_removed(method_name)
631  * puts "Removing #{method_name.inspect}"
632  * end
633  * def self.some_class_method() end
634  * def some_instance_method() end
635  * class << self
636  * remove_method :some_class_method
637  * end
638  * remove_method :some_instance_method
639  * end
640  *
641  * produces:
642  *
643  * Removing :some_instance_method
644  *
645  */
646 
647 /*
648  * Document-method: singleton_method_added
649  *
650  * call-seq:
651  * singleton_method_added(symbol)
652  *
653  * Invoked as a callback whenever a singleton method is added to the
654  * receiver.
655  *
656  * module Chatty
657  * def Chatty.singleton_method_added(id)
658  * puts "Adding #{id.id2name}"
659  * end
660  * def self.one() end
661  * def two() end
662  * def Chatty.three() end
663  * end
664  *
665  * <em>produces:</em>
666  *
667  * Adding singleton_method_added
668  * Adding one
669  * Adding three
670  *
671  */
672 
673 /*
674  * Document-method: singleton_method_removed
675  *
676  * call-seq:
677  * singleton_method_removed(symbol)
678  *
679  * Invoked as a callback whenever a singleton method is removed from
680  * the receiver.
681  *
682  * module Chatty
683  * def Chatty.singleton_method_removed(id)
684  * puts "Removing #{id.id2name}"
685  * end
686  * def self.one() end
687  * def two() end
688  * def Chatty.three() end
689  * class << self
690  * remove_method :three
691  * remove_method :one
692  * end
693  * end
694  *
695  * <em>produces:</em>
696  *
697  * Removing three
698  * Removing one
699  */
700 
701 /*
702  * Document-method: singleton_method_undefined
703  *
704  * call-seq:
705  * singleton_method_undefined(symbol)
706  *
707  * Invoked as a callback whenever a singleton method is undefined in
708  * the receiver.
709  *
710  * module Chatty
711  * def Chatty.singleton_method_undefined(id)
712  * puts "Undefining #{id.id2name}"
713  * end
714  * def Chatty.one() end
715  * class << self
716  * undef_method(:one)
717  * end
718  * end
719  *
720  * <em>produces:</em>
721  *
722  * Undefining one
723  */
724 
725 
726 /*
727  * Document-method: included
728  *
729  * call-seq:
730  * included( othermod )
731  *
732  * Callback invoked whenever the receiver is included in another
733  * module or class. This should be used in preference to
734  * <tt>Module.append_features</tt> if your code wants to perform some
735  * action when a module is included in another.
736  *
737  * module A
738  * def A.included(mod)
739  * puts "#{self} included in #{mod}"
740  * end
741  * end
742  * module Enumerable
743  * include A
744  * end
745  */
746 
747 /*
748  * Document-method: initialize
749  *
750  * call-seq:
751  * BasicObject.new
752  *
753  * Returns a new BasicObject.
754  */
755 
756 /*
757  * Not documented
758  */
759 
760 static VALUE
762 {
763  return Qnil;
764 }
765 
766 /*
767  * call-seq:
768  * obj.tainted? -> true or false
769  *
770  * Returns <code>true</code> if the object is tainted.
771  */
772 
773 VALUE
775 {
776  if (OBJ_TAINTED(obj))
777  return Qtrue;
778  return Qfalse;
779 }
780 
781 /*
782  * call-seq:
783  * obj.taint -> obj
784  *
785  * Marks <i>obj</i> as tainted---if the <code>$SAFE</code> level is
786  * set appropriately, many method calls which might alter the running
787  * programs environment will refuse to accept tainted strings.
788  */
789 
790 VALUE
792 {
793  rb_secure(4);
794  if (!OBJ_TAINTED(obj)) {
795  rb_check_frozen(obj);
796  OBJ_TAINT(obj);
797  }
798  return obj;
799 }
800 
801 
802 /*
803  * call-seq:
804  * obj.untaint -> obj
805  *
806  * Removes the taint from <i>obj</i>.
807  */
808 
809 VALUE
811 {
812  rb_secure(3);
813  if (OBJ_TAINTED(obj)) {
814  rb_check_frozen(obj);
815  FL_UNSET(obj, FL_TAINT);
816  }
817  return obj;
818 }
819 
820 /*
821  * call-seq:
822  * obj.untrusted? -> true or false
823  *
824  * Returns <code>true</code> if the object is untrusted.
825  */
826 
827 VALUE
829 {
830  if (OBJ_UNTRUSTED(obj))
831  return Qtrue;
832  return Qfalse;
833 }
834 
835 /*
836  * call-seq:
837  * obj.untrust -> obj
838  *
839  * Marks <i>obj</i> as untrusted.
840  */
841 
842 VALUE
844 {
845  rb_secure(4);
846  if (!OBJ_UNTRUSTED(obj)) {
847  rb_check_frozen(obj);
848  OBJ_UNTRUST(obj);
849  }
850  return obj;
851 }
852 
853 
854 /*
855  * call-seq:
856  * obj.trust -> obj
857  *
858  * Removes the untrusted mark from <i>obj</i>.
859  */
860 
861 VALUE
863 {
864  rb_secure(3);
865  if (OBJ_UNTRUSTED(obj)) {
866  rb_check_frozen(obj);
867  FL_UNSET(obj, FL_UNTRUSTED);
868  }
869  return obj;
870 }
871 
872 void
874 {
875  OBJ_INFECT(obj1, obj2);
876 }
877 
879 
880 /*
881  * call-seq:
882  * obj.freeze -> obj
883  *
884  * Prevents further modifications to <i>obj</i>. A
885  * <code>RuntimeError</code> will be raised if modification is attempted.
886  * There is no way to unfreeze a frozen object. See also
887  * <code>Object#frozen?</code>.
888  *
889  * This method returns self.
890  *
891  * a = [ "a", "b", "c" ]
892  * a.freeze
893  * a << "z"
894  *
895  * <em>produces:</em>
896  *
897  * prog.rb:3:in `<<': can't modify frozen array (RuntimeError)
898  * from prog.rb:3
899  */
900 
901 VALUE
903 {
904  if (!OBJ_FROZEN(obj)) {
905  if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(obj)) {
906  rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
907  }
908  OBJ_FREEZE(obj);
909  if (SPECIAL_CONST_P(obj)) {
910  if (!immediate_frozen_tbl) {
911  immediate_frozen_tbl = st_init_numtable();
912  }
913  st_insert(immediate_frozen_tbl, obj, (st_data_t)Qtrue);
914  }
915  }
916  return obj;
917 }
918 
919 /*
920  * call-seq:
921  * obj.frozen? -> true or false
922  *
923  * Returns the freeze status of <i>obj</i>.
924  *
925  * a = [ "a", "b", "c" ]
926  * a.freeze #=> ["a", "b", "c"]
927  * a.frozen? #=> true
928  */
929 
930 VALUE
932 {
933  if (OBJ_FROZEN(obj)) return Qtrue;
934  if (SPECIAL_CONST_P(obj)) {
935  if (!immediate_frozen_tbl) return Qfalse;
936  if (st_lookup(immediate_frozen_tbl, obj, 0)) return Qtrue;
937  }
938  return Qfalse;
939 }
940 
941 
942 /*
943  * Document-class: NilClass
944  *
945  * The class of the singleton object <code>nil</code>.
946  */
947 
948 /*
949  * call-seq:
950  * nil.to_i -> 0
951  *
952  * Always returns zero.
953  *
954  * nil.to_i #=> 0
955  */
956 
957 
958 static VALUE
960 {
961  return INT2FIX(0);
962 }
963 
964 /*
965  * call-seq:
966  * nil.to_f -> 0.0
967  *
968  * Always returns zero.
969  *
970  * nil.to_f #=> 0.0
971  */
972 
973 static VALUE
975 {
976  return DBL2NUM(0.0);
977 }
978 
979 /*
980  * call-seq:
981  * nil.to_s -> ""
982  *
983  * Always returns the empty string.
984  */
985 
986 static VALUE
988 {
989  return rb_usascii_str_new(0, 0);
990 }
991 
992 /*
993  * Document-method: to_a
994  *
995  * call-seq:
996  * nil.to_a -> []
997  *
998  * Always returns an empty array.
999  *
1000  * nil.to_a #=> []
1001  */
1002 
1003 static VALUE
1005 {
1006  return rb_ary_new2(0);
1007 }
1008 
1009 /*
1010  * call-seq:
1011  * nil.inspect -> "nil"
1012  *
1013  * Always returns the string "nil".
1014  */
1015 
1016 static VALUE
1018 {
1019  return rb_usascii_str_new2("nil");
1020 }
1021 
1022 /***********************************************************************
1023  * Document-class: TrueClass
1024  *
1025  * The global value <code>true</code> is the only instance of class
1026  * <code>TrueClass</code> and represents a logically true value in
1027  * boolean expressions. The class provides operators allowing
1028  * <code>true</code> to be used in logical expressions.
1029  */
1030 
1031 
1032 /*
1033  * call-seq:
1034  * true.to_s -> "true"
1035  *
1036  * The string representation of <code>true</code> is "true".
1037  */
1038 
1039 static VALUE
1041 {
1042  return rb_usascii_str_new2("true");
1043 }
1044 
1045 
1046 /*
1047  * call-seq:
1048  * true & obj -> true or false
1049  *
1050  * And---Returns <code>false</code> if <i>obj</i> is
1051  * <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
1052  */
1053 
1054 static VALUE
1056 {
1057  return RTEST(obj2)?Qtrue:Qfalse;
1058 }
1059 
1060 /*
1061  * call-seq:
1062  * true | obj -> true
1063  *
1064  * Or---Returns <code>true</code>. As <i>anObject</i> is an argument to
1065  * a method call, it is always evaluated; there is no short-circuit
1066  * evaluation in this case.
1067  *
1068  * true | puts("or")
1069  * true || puts("logical or")
1070  *
1071  * <em>produces:</em>
1072  *
1073  * or
1074  */
1075 
1076 static VALUE
1077 true_or(VALUE obj, VALUE obj2)
1078 {
1079  return Qtrue;
1080 }
1081 
1082 
1083 /*
1084  * call-seq:
1085  * true ^ obj -> !obj
1086  *
1087  * Exclusive Or---Returns <code>true</code> if <i>obj</i> is
1088  * <code>nil</code> or <code>false</code>, <code>false</code>
1089  * otherwise.
1090  */
1091 
1092 static VALUE
1094 {
1095  return RTEST(obj2)?Qfalse:Qtrue;
1096 }
1097 
1098 
1099 /*
1100  * Document-class: FalseClass
1101  *
1102  * The global value <code>false</code> is the only instance of class
1103  * <code>FalseClass</code> and represents a logically false value in
1104  * boolean expressions. The class provides operators allowing
1105  * <code>false</code> to participate correctly in logical expressions.
1106  *
1107  */
1108 
1109 /*
1110  * call-seq:
1111  * false.to_s -> "false"
1112  *
1113  * 'nuf said...
1114  */
1115 
1116 static VALUE
1118 {
1119  return rb_usascii_str_new2("false");
1120 }
1121 
1122 /*
1123  * call-seq:
1124  * false & obj -> false
1125  * nil & obj -> false
1126  *
1127  * And---Returns <code>false</code>. <i>obj</i> is always
1128  * evaluated as it is the argument to a method call---there is no
1129  * short-circuit evaluation in this case.
1130  */
1131 
1132 static VALUE
1134 {
1135  return Qfalse;
1136 }
1137 
1138 
1139 /*
1140  * call-seq:
1141  * false | obj -> true or false
1142  * nil | obj -> true or false
1143  *
1144  * Or---Returns <code>false</code> if <i>obj</i> is
1145  * <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
1146  */
1147 
1148 static VALUE
1150 {
1151  return RTEST(obj2)?Qtrue:Qfalse;
1152 }
1153 
1154 
1155 
1156 /*
1157  * call-seq:
1158  * false ^ obj -> true or false
1159  * nil ^ obj -> true or false
1160  *
1161  * Exclusive Or---If <i>obj</i> is <code>nil</code> or
1162  * <code>false</code>, returns <code>false</code>; otherwise, returns
1163  * <code>true</code>.
1164  *
1165  */
1166 
1167 static VALUE
1169 {
1170  return RTEST(obj2)?Qtrue:Qfalse;
1171 }
1172 
1173 /*
1174  * call_seq:
1175  * nil.nil? -> true
1176  *
1177  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1178  */
1179 
1180 static VALUE
1182 {
1183  return Qtrue;
1184 }
1185 
1186 /*
1187  * call_seq:
1188  * nil.nil? -> true
1189  * <anything_else>.nil? -> false
1190  *
1191  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1192  */
1193 
1194 
1195 static VALUE
1197 {
1198  return Qfalse;
1199 }
1200 
1201 
1202 /*
1203  * call-seq:
1204  * obj =~ other -> nil
1205  *
1206  * Pattern Match---Overridden by descendants (notably
1207  * <code>Regexp</code> and <code>String</code>) to provide meaningful
1208  * pattern-match semantics.
1209  */
1210 
1211 static VALUE
1213 {
1214  return Qnil;
1215 }
1216 
1217 /*
1218  * call-seq:
1219  * obj !~ other -> true or false
1220  *
1221  * Returns true if two objects do not match (using the <i>=~</i>
1222  * method), otherwise false.
1223  */
1224 
1225 static VALUE
1227 {
1228  VALUE result = rb_funcall(obj1, id_match, 1, obj2);
1229  return RTEST(result) ? Qfalse : Qtrue;
1230 }
1231 
1232 
1233 /*
1234  * call-seq:
1235  * obj <=> other -> 0 or nil
1236  *
1237  * Returns 0 if obj === other, otherwise nil.
1238  */
1239 static VALUE
1241 {
1242  if (obj1 == obj2 || rb_equal(obj1, obj2))
1243  return INT2FIX(0);
1244  return Qnil;
1245 }
1246 
1247 /***********************************************************************
1248  *
1249  * Document-class: Module
1250  *
1251  * A <code>Module</code> is a collection of methods and constants. The
1252  * methods in a module may be instance methods or module methods.
1253  * Instance methods appear as methods in a class when the module is
1254  * included, module methods do not. Conversely, module methods may be
1255  * called without creating an encapsulating object, while instance
1256  * methods may not. (See <code>Module#module_function</code>)
1257  *
1258  * In the descriptions that follow, the parameter <i>sym</i> refers
1259  * to a symbol, which is either a quoted string or a
1260  * <code>Symbol</code> (such as <code>:name</code>).
1261  *
1262  * module Mod
1263  * include Math
1264  * CONST = 1
1265  * def meth
1266  * # ...
1267  * end
1268  * end
1269  * Mod.class #=> Module
1270  * Mod.constants #=> [:CONST, :PI, :E]
1271  * Mod.instance_methods #=> [:meth]
1272  *
1273  */
1274 
1275 /*
1276  * call-seq:
1277  * mod.to_s -> string
1278  *
1279  * Return a string representing this module or class. For basic
1280  * classes and modules, this is the name. For singletons, we
1281  * show information on the thing we're attached to as well.
1282  */
1283 
1284 static VALUE
1286 {
1287  if (FL_TEST(klass, FL_SINGLETON)) {
1288  VALUE s = rb_usascii_str_new2("#<");
1289  VALUE v = rb_iv_get(klass, "__attached__");
1290 
1291  rb_str_cat2(s, "Class:");
1292  switch (TYPE(v)) {
1293  case T_CLASS: case T_MODULE:
1294  rb_str_append(s, rb_inspect(v));
1295  break;
1296  default:
1297  rb_str_append(s, rb_any_to_s(v));
1298  break;
1299  }
1300  rb_str_cat2(s, ">");
1301 
1302  return s;
1303  }
1304  return rb_str_dup(rb_class_name(klass));
1305 }
1306 
1307 /*
1308  * call-seq:
1309  * mod.freeze -> mod
1310  *
1311  * Prevents further modifications to <i>mod</i>.
1312  *
1313  * This method returns self.
1314  */
1315 
1316 static VALUE
1318 {
1319  rb_class_name(mod);
1320  return rb_obj_freeze(mod);
1321 }
1322 
1323 /*
1324  * call-seq:
1325  * mod === obj -> true or false
1326  *
1327  * Case Equality---Returns <code>true</code> if <i>anObject</i> is an
1328  * instance of <i>mod</i> or one of <i>mod</i>'s descendants. Of
1329  * limited use for modules, but can be used in <code>case</code>
1330  * statements to classify objects by class.
1331  */
1332 
1333 static VALUE
1335 {
1336  return rb_obj_is_kind_of(arg, mod);
1337 }
1338 
1339 /*
1340  * call-seq:
1341  * mod <= other -> true, false, or nil
1342  *
1343  * Returns true if <i>mod</i> is a subclass of <i>other</i> or
1344  * is the same as <i>other</i>. Returns
1345  * <code>nil</code> if there's no relationship between the two.
1346  * (Think of the relationship in terms of the class definition:
1347  * "class A<B" implies "A<B").
1348  *
1349  */
1350 
1351 VALUE
1353 {
1354  VALUE start = mod;
1355 
1356  if (mod == arg) return Qtrue;
1357  switch (TYPE(arg)) {
1358  case T_MODULE:
1359  case T_CLASS:
1360  break;
1361  default:
1362  rb_raise(rb_eTypeError, "compared with non class/module");
1363  }
1364  while (mod) {
1365  if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg))
1366  return Qtrue;
1367  mod = RCLASS_SUPER(mod);
1368  }
1369  /* not mod < arg; check if mod > arg */
1370  while (arg) {
1371  if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start))
1372  return Qfalse;
1373  arg = RCLASS_SUPER(arg);
1374  }
1375  return Qnil;
1376 }
1377 
1378 /*
1379  * call-seq:
1380  * mod < other -> true, false, or nil
1381  *
1382  * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
1383  * <code>nil</code> if there's no relationship between the two.
1384  * (Think of the relationship in terms of the class definition:
1385  * "class A<B" implies "A<B").
1386  *
1387  */
1388 
1389 static VALUE
1391 {
1392  if (mod == arg) return Qfalse;
1393  return rb_class_inherited_p(mod, arg);
1394 }
1395 
1396 
1397 /*
1398  * call-seq:
1399  * mod >= other -> true, false, or nil
1400  *
1401  * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
1402  * two modules are the same. Returns
1403  * <code>nil</code> if there's no relationship between the two.
1404  * (Think of the relationship in terms of the class definition:
1405  * "class A<B" implies "B>A").
1406  *
1407  */
1408 
1409 static VALUE
1411 {
1412  switch (TYPE(arg)) {
1413  case T_MODULE:
1414  case T_CLASS:
1415  break;
1416  default:
1417  rb_raise(rb_eTypeError, "compared with non class/module");
1418  }
1419 
1420  return rb_class_inherited_p(arg, mod);
1421 }
1422 
1423 /*
1424  * call-seq:
1425  * mod > other -> true, false, or nil
1426  *
1427  * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
1428  * <code>nil</code> if there's no relationship between the two.
1429  * (Think of the relationship in terms of the class definition:
1430  * "class A<B" implies "B>A").
1431  *
1432  */
1433 
1434 static VALUE
1436 {
1437  if (mod == arg) return Qfalse;
1438  return rb_mod_ge(mod, arg);
1439 }
1440 
1441 /*
1442  * call-seq:
1443  * mod <=> other_mod -> -1, 0, +1, or nil
1444  *
1445  * Comparison---Returns -1 if <i>mod</i> includes <i>other_mod</i>, 0 if
1446  * <i>mod</i> is the same as <i>other_mod</i>, and +1 if <i>mod</i> is
1447  * included by <i>other_mod</i>. Returns <code>nil</code> if <i>mod</i>
1448  * has no relationship with <i>other_mod</i> or if <i>other_mod</i> is
1449  * not a module.
1450  */
1451 
1452 static VALUE
1454 {
1455  VALUE cmp;
1456 
1457  if (mod == arg) return INT2FIX(0);
1458  switch (TYPE(arg)) {
1459  case T_MODULE:
1460  case T_CLASS:
1461  break;
1462  default:
1463  return Qnil;
1464  }
1465 
1466  cmp = rb_class_inherited_p(mod, arg);
1467  if (NIL_P(cmp)) return Qnil;
1468  if (cmp) {
1469  return INT2FIX(-1);
1470  }
1471  return INT2FIX(1);
1472 }
1473 
1474 static VALUE
1476 {
1477  VALUE mod = rb_module_new();
1478 
1479  RBASIC(mod)->klass = klass;
1480  return mod;
1481 }
1482 
1483 static VALUE
1485 {
1486  return rb_class_boot(0);
1487 }
1488 
1489 /*
1490  * call-seq:
1491  * Module.new -> mod
1492  * Module.new {|mod| block } -> mod
1493  *
1494  * Creates a new anonymous module. If a block is given, it is passed
1495  * the module object, and the block is evaluated in the context of this
1496  * module using <code>module_eval</code>.
1497  *
1498  * fred = Module.new do
1499  * def meth1
1500  * "hello"
1501  * end
1502  * def meth2
1503  * "bye"
1504  * end
1505  * end
1506  * a = "my string"
1507  * a.extend(fred) #=> "my string"
1508  * a.meth1 #=> "hello"
1509  * a.meth2 #=> "bye"
1510  *
1511  * Assign the module to a constant (name starting uppercase) if you
1512  * want to treat it like a regular module.
1513  */
1514 
1515 static VALUE
1517 {
1518  if (rb_block_given_p()) {
1519  rb_mod_module_exec(1, &module, module);
1520  }
1521  return Qnil;
1522 }
1523 
1524 /*
1525  * call-seq:
1526  * Class.new(super_class=Object) -> a_class
1527  * Class.new(super_class=Object) { |mod| ... } -> a_class
1528  *
1529  * Creates a new anonymous (unnamed) class with the given superclass
1530  * (or <code>Object</code> if no parameter is given). You can give a
1531  * class a name by assigning the class object to a constant.
1532  *
1533  * If a block is given, it is passed the class object, and the block
1534  * is evaluated in the context of this class using
1535  * <code>class_eval</code>.
1536  *
1537  * fred = Class.new do
1538  * def meth1
1539  * "hello"
1540  * end
1541  * def meth2
1542  * "bye"
1543  * end
1544  * end
1545  *
1546  * a = fred.new #=> #<#<Class:0x100381890>:0x100376b98>
1547  * a.meth1 #=> "hello"
1548  * a.meth2 #=> "bye"
1549  *
1550  * Assign the class to a constant (name starting uppercase) if you
1551  * want to treat it like a regular class.
1552  */
1553 
1554 static VALUE
1556 {
1557  VALUE super;
1558 
1559  if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
1560  rb_raise(rb_eTypeError, "already initialized class");
1561  }
1562  if (argc == 0) {
1563  super = rb_cObject;
1564  }
1565  else {
1566  rb_scan_args(argc, argv, "01", &super);
1567  rb_check_inheritable(super);
1568  }
1569  RCLASS_SUPER(klass) = super;
1570  rb_make_metaclass(klass, RBASIC(super)->klass);
1571  rb_class_inherited(super, klass);
1572  rb_mod_initialize(klass);
1573 
1574  return klass;
1575 }
1576 
1577 /*
1578  * call-seq:
1579  * class.allocate() -> obj
1580  *
1581  * Allocates space for a new object of <i>class</i>'s class and does not
1582  * call initialize on the new instance. The returned object must be an
1583  * instance of <i>class</i>.
1584  *
1585  * klass = Class.new do
1586  * def initialize(*args)
1587  * @initialized = true
1588  * end
1589  *
1590  * def initialized?
1591  * @initialized || false
1592  * end
1593  * end
1594  *
1595  * klass.allocate.initialized? #=> false
1596  *
1597  */
1598 
1599 VALUE
1601 {
1602  VALUE obj;
1603 
1604  if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
1605  rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
1606  }
1607  if (FL_TEST(klass, FL_SINGLETON)) {
1608  rb_raise(rb_eTypeError, "can't create instance of singleton class");
1609  }
1610  obj = rb_funcall(klass, ID_ALLOCATOR, 0, 0);
1611  if (rb_obj_class(obj) != rb_class_real(klass)) {
1612  rb_raise(rb_eTypeError, "wrong instance allocation");
1613  }
1614  return obj;
1615 }
1616 
1617 static VALUE
1619 {
1620  NEWOBJ(obj, struct RObject);
1621  OBJSETUP(obj, klass, T_OBJECT);
1622  return (VALUE)obj;
1623 }
1624 
1625 /*
1626  * call-seq:
1627  * class.new(args, ...) -> obj
1628  *
1629  * Calls <code>allocate</code> to create a new object of
1630  * <i>class</i>'s class, then invokes that object's
1631  * <code>initialize</code> method, passing it <i>args</i>.
1632  * This is the method that ends up getting called whenever
1633  * an object is constructed using .new.
1634  *
1635  */
1636 
1637 VALUE
1639 {
1640  VALUE obj;
1641 
1642  obj = rb_obj_alloc(klass);
1643  rb_obj_call_init(obj, argc, argv);
1644 
1645  return obj;
1646 }
1647 
1648 /*
1649  * call-seq:
1650  * class.superclass -> a_super_class or nil
1651  *
1652  * Returns the superclass of <i>class</i>, or <code>nil</code>.
1653  *
1654  * File.superclass #=> IO
1655  * IO.superclass #=> Object
1656  * Object.superclass #=> BasicObject
1657  * class Foo; end
1658  * class Bar < Foo; end
1659  * Bar.superclass #=> Foo
1660  *
1661  * returns nil when the given class hasn't a parent class:
1662  *
1663  * BasicObject.superclass #=> nil
1664  *
1665  */
1666 
1667 VALUE
1669 {
1670  VALUE super = RCLASS_SUPER(klass);
1671 
1672  if (!super) {
1673  if (klass == rb_cBasicObject) return Qnil;
1674  rb_raise(rb_eTypeError, "uninitialized class");
1675  }
1676  while (TYPE(super) == T_ICLASS) {
1677  super = RCLASS_SUPER(super);
1678  }
1679  if (!super) {
1680  return Qnil;
1681  }
1682  return super;
1683 }
1684 
1685 VALUE
1687 {
1688  return RCLASS_SUPER(klass);
1689 }
1690 
1691 /*
1692  * call-seq:
1693  * attr_reader(symbol, ...) -> nil
1694  * attr(symbol, ...) -> nil
1695  *
1696  * Creates instance variables and corresponding methods that return the
1697  * value of each instance variable. Equivalent to calling
1698  * ``<code>attr</code><i>:name</i>'' on each name in turn.
1699  */
1700 
1701 static VALUE
1703 {
1704  int i;
1705 
1706  for (i=0; i<argc; i++) {
1707  rb_attr(klass, rb_to_id(argv[i]), TRUE, FALSE, TRUE);
1708  }
1709  return Qnil;
1710 }
1711 
1712 VALUE
1714 {
1715  if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
1716  rb_warning("optional boolean argument is obsoleted");
1717  rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), TRUE);
1718  return Qnil;
1719  }
1720  return rb_mod_attr_reader(argc, argv, klass);
1721 }
1722 
1723 /*
1724  * call-seq:
1725  * attr_writer(symbol, ...) -> nil
1726  *
1727  * Creates an accessor method to allow assignment to the attribute
1728  * <i>aSymbol</i><code>.id2name</code>.
1729  */
1730 
1731 static VALUE
1733 {
1734  int i;
1735 
1736  for (i=0; i<argc; i++) {
1737  rb_attr(klass, rb_to_id(argv[i]), FALSE, TRUE, TRUE);
1738  }
1739  return Qnil;
1740 }
1741 
1742 /*
1743  * call-seq:
1744  * attr_accessor(symbol, ...) -> nil
1745  *
1746  * Defines a named attribute for this module, where the name is
1747  * <i>symbol.</i><code>id2name</code>, creating an instance variable
1748  * (<code>@name</code>) and a corresponding access method to read it.
1749  * Also creates a method called <code>name=</code> to set the attribute.
1750  *
1751  * module Mod
1752  * attr_accessor(:one, :two)
1753  * end
1754  * Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
1755  */
1756 
1757 static VALUE
1759 {
1760  int i;
1761 
1762  for (i=0; i<argc; i++) {
1763  rb_attr(klass, rb_to_id(argv[i]), TRUE, TRUE, TRUE);
1764  }
1765  return Qnil;
1766 }
1767 
1768 /*
1769  * call-seq:
1770  * mod.const_get(sym, inherit=true) -> obj
1771  *
1772  * Checks for a constant with the given name in <i>mod</i>
1773  * If +inherit+ is set, the lookup will also search
1774  * the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
1775  *
1776  * The value of the constant is returned if a definition is found,
1777  * otherwise a +NameError+ is raised.
1778  *
1779  * Math.const_get(:PI) #=> 3.14159265358979
1780  */
1781 
1782 static VALUE
1784 {
1785  VALUE name, recur;
1786  ID id;
1787 
1788  if (argc == 1) {
1789  name = argv[0];
1790  recur = Qtrue;
1791  }
1792  else {
1793  rb_scan_args(argc, argv, "11", &name, &recur);
1794  }
1795  id = rb_to_id(name);
1796  if (!rb_is_const_id(id)) {
1797  rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1798  }
1799  return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
1800 }
1801 
1802 /*
1803  * call-seq:
1804  * mod.const_set(sym, obj) -> obj
1805  *
1806  * Sets the named constant to the given object, returning that object.
1807  * Creates a new constant if no constant with the given name previously
1808  * existed.
1809  *
1810  * Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
1811  * Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
1812  */
1813 
1814 static VALUE
1816 {
1817  ID id = rb_to_id(name);
1818 
1819  if (!rb_is_const_id(id)) {
1820  rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1821  }
1822  rb_const_set(mod, id, value);
1823  return value;
1824 }
1825 
1826 /*
1827  * call-seq:
1828  * mod.const_defined?(sym, inherit=true) -> true or false
1829  *
1830  * Checks for a constant with the given name in <i>mod</i>
1831  * If +inherit+ is set, the lookup will also search
1832  * the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
1833  *
1834  * Returns whether or not a definition is found:
1835  *
1836  * Math.const_defined? "PI" #=> true
1837  * IO.const_defined? :SYNC #=> true
1838  * IO.const_defined? :SYNC, false #=> false
1839  */
1840 
1841 static VALUE
1843 {
1844  VALUE name, recur;
1845  ID id;
1846 
1847  if (argc == 1) {
1848  name = argv[0];
1849  recur = Qtrue;
1850  }
1851  else {
1852  rb_scan_args(argc, argv, "11", &name, &recur);
1853  }
1854  id = rb_to_id(name);
1855  if (!rb_is_const_id(id)) {
1856  rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1857  }
1858  return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
1859 }
1860 
1861 /*
1862  * call-seq:
1863  * obj.instance_variable_get(symbol) -> obj
1864  *
1865  * Returns the value of the given instance variable, or nil if the
1866  * instance variable is not set. The <code>@</code> part of the
1867  * variable name should be included for regular instance
1868  * variables. Throws a <code>NameError</code> exception if the
1869  * supplied symbol is not valid as an instance variable name.
1870  *
1871  * class Fred
1872  * def initialize(p1, p2)
1873  * @a, @b = p1, p2
1874  * end
1875  * end
1876  * fred = Fred.new('cat', 99)
1877  * fred.instance_variable_get(:@a) #=> "cat"
1878  * fred.instance_variable_get("@b") #=> 99
1879  */
1880 
1881 static VALUE
1883 {
1884  ID id = rb_to_id(iv);
1885 
1886  if (!rb_is_instance_id(id)) {
1887  rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1888  }
1889  return rb_ivar_get(obj, id);
1890 }
1891 
1892 /*
1893  * call-seq:
1894  * obj.instance_variable_set(symbol, obj) -> obj
1895  *
1896  * Sets the instance variable names by <i>symbol</i> to
1897  * <i>object</i>, thereby frustrating the efforts of the class's
1898  * author to attempt to provide proper encapsulation. The variable
1899  * did not have to exist prior to this call.
1900  *
1901  * class Fred
1902  * def initialize(p1, p2)
1903  * @a, @b = p1, p2
1904  * end
1905  * end
1906  * fred = Fred.new('cat', 99)
1907  * fred.instance_variable_set(:@a, 'dog') #=> "dog"
1908  * fred.instance_variable_set(:@c, 'cat') #=> "cat"
1909  * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
1910  */
1911 
1912 static VALUE
1914 {
1915  ID id = rb_to_id(iv);
1916 
1917  if (!rb_is_instance_id(id)) {
1918  rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1919  }
1920  return rb_ivar_set(obj, id, val);
1921 }
1922 
1923 /*
1924  * call-seq:
1925  * obj.instance_variable_defined?(symbol) -> true or false
1926  *
1927  * Returns <code>true</code> if the given instance variable is
1928  * defined in <i>obj</i>.
1929  *
1930  * class Fred
1931  * def initialize(p1, p2)
1932  * @a, @b = p1, p2
1933  * end
1934  * end
1935  * fred = Fred.new('cat', 99)
1936  * fred.instance_variable_defined?(:@a) #=> true
1937  * fred.instance_variable_defined?("@b") #=> true
1938  * fred.instance_variable_defined?("@c") #=> false
1939  */
1940 
1941 static VALUE
1943 {
1944  ID id = rb_to_id(iv);
1945 
1946  if (!rb_is_instance_id(id)) {
1947  rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1948  }
1949  return rb_ivar_defined(obj, id);
1950 }
1951 
1952 /*
1953  * call-seq:
1954  * mod.class_variable_get(symbol) -> obj
1955  *
1956  * Returns the value of the given class variable (or throws a
1957  * <code>NameError</code> exception). The <code>@@</code> part of the
1958  * variable name should be included for regular class variables
1959  *
1960  * class Fred
1961  * @@foo = 99
1962  * end
1963  * Fred.class_variable_get(:@@foo) #=> 99
1964  */
1965 
1966 static VALUE
1968 {
1969  ID id = rb_to_id(iv);
1970 
1971  if (!rb_is_class_id(id)) {
1972  rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
1973  }
1974  return rb_cvar_get(obj, id);
1975 }
1976 
1977 /*
1978  * call-seq:
1979  * obj.class_variable_set(symbol, obj) -> obj
1980  *
1981  * Sets the class variable names by <i>symbol</i> to
1982  * <i>object</i>.
1983  *
1984  * class Fred
1985  * @@foo = 99
1986  * def foo
1987  * @@foo
1988  * end
1989  * end
1990  * Fred.class_variable_set(:@@foo, 101) #=> 101
1991  * Fred.new.foo #=> 101
1992  */
1993 
1994 static VALUE
1996 {
1997  ID id = rb_to_id(iv);
1998 
1999  if (!rb_is_class_id(id)) {
2000  rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
2001  }
2002  rb_cvar_set(obj, id, val);
2003  return val;
2004 }
2005 
2006 /*
2007  * call-seq:
2008  * obj.class_variable_defined?(symbol) -> true or false
2009  *
2010  * Returns <code>true</code> if the given class variable is defined
2011  * in <i>obj</i>.
2012  *
2013  * class Fred
2014  * @@foo = 99
2015  * end
2016  * Fred.class_variable_defined?(:@@foo) #=> true
2017  * Fred.class_variable_defined?(:@@bar) #=> false
2018  */
2019 
2020 static VALUE
2022 {
2023  ID id = rb_to_id(iv);
2024 
2025  if (!rb_is_class_id(id)) {
2026  rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
2027  }
2028  return rb_cvar_defined(obj, id);
2029 }
2030 
2031 static struct conv_method_tbl {
2032  const char *method;
2034 } conv_method_names[] = {
2035  {"to_int", 0},
2036  {"to_ary", 0},
2037  {"to_str", 0},
2038  {"to_sym", 0},
2039  {"to_hash", 0},
2040  {"to_proc", 0},
2041  {"to_io", 0},
2042  {"to_a", 0},
2043  {"to_s", 0},
2044  {NULL, 0}
2045 };
2046 
2047 static VALUE
2048 convert_type(VALUE val, const char *tname, const char *method, int raise)
2049 {
2050  ID m = 0;
2051  int i;
2052  VALUE r;
2053 
2054  for (i=0; conv_method_names[i].method; i++) {
2055  if (conv_method_names[i].method[0] == method[0] &&
2056  strcmp(conv_method_names[i].method, method) == 0) {
2057  m = conv_method_names[i].id;
2058  break;
2059  }
2060  }
2061  if (!m) m = rb_intern(method);
2062  r = rb_check_funcall(val, m, 0, 0);
2063  if (r == Qundef) {
2064  if (raise) {
2065  rb_raise(rb_eTypeError, "can't convert %s into %s",
2066  NIL_P(val) ? "nil" :
2067  val == Qtrue ? "true" :
2068  val == Qfalse ? "false" :
2069  rb_obj_classname(val),
2070  tname);
2071  }
2072  return Qnil;
2073  }
2074  return r;
2075 }
2076 
2077 VALUE
2078 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
2079 {
2080  VALUE v;
2081 
2082  if (TYPE(val) == type) return val;
2083  v = convert_type(val, tname, method, TRUE);
2084  if (TYPE(v) != type) {
2085  const char *cname = rb_obj_classname(val);
2086  rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
2087  cname, tname, cname, method, rb_obj_classname(v));
2088  }
2089  return v;
2090 }
2091 
2092 VALUE
2093 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
2094 {
2095  VALUE v;
2096 
2097  /* always convert T_DATA */
2098  if (TYPE(val) == type && type != T_DATA) return val;
2099  v = convert_type(val, tname, method, FALSE);
2100  if (NIL_P(v)) return Qnil;
2101  if (TYPE(v) != type) {
2102  const char *cname = rb_obj_classname(val);
2103  rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
2104  cname, tname, cname, method, rb_obj_classname(v));
2105  }
2106  return v;
2107 }
2108 
2109 
2110 static VALUE
2111 rb_to_integer(VALUE val, const char *method)
2112 {
2113  VALUE v;
2114 
2115  if (FIXNUM_P(val)) return val;
2116  if (TYPE(val) == T_BIGNUM) return val;
2117  v = convert_type(val, "Integer", method, TRUE);
2118  if (!rb_obj_is_kind_of(v, rb_cInteger)) {
2119  const char *cname = rb_obj_classname(val);
2120  rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)",
2121  cname, cname, method, rb_obj_classname(v));
2122  }
2123  return v;
2124 }
2125 
2126 VALUE
2127 rb_check_to_integer(VALUE val, const char *method)
2128 {
2129  VALUE v;
2130 
2131  if (FIXNUM_P(val)) return val;
2132  if (TYPE(val) == T_BIGNUM) return val;
2133  v = convert_type(val, "Integer", method, FALSE);
2134  if (!rb_obj_is_kind_of(v, rb_cInteger)) {
2135  return Qnil;
2136  }
2137  return v;
2138 }
2139 
2140 VALUE
2142 {
2143  return rb_to_integer(val, "to_int");
2144 }
2145 
2146 static VALUE
2148 {
2149  VALUE tmp;
2150 
2151  switch (TYPE(val)) {
2152  case T_FLOAT:
2153  if (base != 0) goto arg_error;
2154  if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX
2155  && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) {
2156  break;
2157  }
2158  return rb_dbl2big(RFLOAT_VALUE(val));
2159 
2160  case T_FIXNUM:
2161  case T_BIGNUM:
2162  if (base != 0) goto arg_error;
2163  return val;
2164 
2165  case T_STRING:
2166  string_conv:
2167  return rb_str_to_inum(val, base, TRUE);
2168 
2169  case T_NIL:
2170  if (base != 0) goto arg_error;
2171  rb_raise(rb_eTypeError, "can't convert nil into Integer");
2172  break;
2173 
2174  default:
2175  break;
2176  }
2177  if (base != 0) {
2178  tmp = rb_check_string_type(val);
2179  if (!NIL_P(tmp)) goto string_conv;
2180  arg_error:
2181  rb_raise(rb_eArgError, "base specified for non string value");
2182  }
2183  tmp = convert_type(val, "Integer", "to_int", FALSE);
2184  if (NIL_P(tmp)) {
2185  return rb_to_integer(val, "to_i");
2186  }
2187  return tmp;
2188 
2189 }
2190 
2191 VALUE
2193 {
2194  return rb_convert_to_integer(val, 0);
2195 }
2196 
2197 /*
2198  * call-seq:
2199  * Integer(arg,base=0) -> integer
2200  *
2201  * Converts <i>arg</i> to a <code>Fixnum</code> or <code>Bignum</code>.
2202  * Numeric types are converted directly (with floating point numbers
2203  * being truncated). <i>base</i> (0, or between 2 and 36) is a base for
2204  * integer string representation. If <i>arg</i> is a <code>String</code>,
2205  * when <i>base</i> is omitted or equals to zero, radix indicators
2206  * (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
2207  * In any case, strings should be strictly conformed to numeric
2208  * representation. This behavior is different from that of
2209  * <code>String#to_i</code>. Non string values will be converted using
2210  * <code>to_int</code>, and <code>to_i</code>.
2211  *
2212  * Integer(123.999) #=> 123
2213  * Integer("0x1a") #=> 26
2214  * Integer(Time.new) #=> 1204973019
2215  * Integer("0930", 10) #=> 930
2216  * Integer("111", 2) #=> 7
2217  */
2218 
2219 static VALUE
2221 {
2222  VALUE arg = Qnil;
2223  int base = 0;
2224 
2225  switch (argc) {
2226  case 2:
2227  base = NUM2INT(argv[1]);
2228  case 1:
2229  arg = argv[0];
2230  break;
2231  default:
2232  /* should cause ArgumentError */
2233  rb_scan_args(argc, argv, "11", NULL, NULL);
2234  }
2235  return rb_convert_to_integer(arg, base);
2236 }
2237 
2238 double
2239 rb_cstr_to_dbl(const char *p, int badcheck)
2240 {
2241  const char *q;
2242  char *end;
2243  double d;
2244  const char *ellipsis = "";
2245  int w;
2246  enum {max_width = 20};
2247 #define OutOfRange() ((end - p > max_width) ? \
2248  (w = max_width, ellipsis = "...") : \
2249  (w = (int)(end - p), ellipsis = ""))
2250 
2251  if (!p) return 0.0;
2252  q = p;
2253  while (ISSPACE(*p)) p++;
2254 
2255  if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2256  return 0.0;
2257  }
2258 
2259  d = strtod(p, &end);
2260  if (errno == ERANGE) {
2261  OutOfRange();
2262  rb_warning("Float %.*s%s out of range", w, p, ellipsis);
2263  errno = 0;
2264  }
2265  if (p == end) {
2266  if (badcheck) {
2267  bad:
2268  rb_invalid_str(q, "Float()");
2269  }
2270  return d;
2271  }
2272  if (*end) {
2273  char buf[DBL_DIG * 4 + 10];
2274  char *n = buf;
2275  char *e = buf + sizeof(buf) - 1;
2276  char prev = 0;
2277 
2278  while (p < end && n < e) prev = *n++ = *p++;
2279  while (*p) {
2280  if (*p == '_') {
2281  /* remove underscores between digits */
2282  if (badcheck) {
2283  if (n == buf || !ISDIGIT(prev)) goto bad;
2284  ++p;
2285  if (!ISDIGIT(*p)) goto bad;
2286  }
2287  else {
2288  while (*++p == '_');
2289  continue;
2290  }
2291  }
2292  prev = *p++;
2293  if (n < e) *n++ = prev;
2294  }
2295  *n = '\0';
2296  p = buf;
2297 
2298  if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2299  return 0.0;
2300  }
2301 
2302  d = strtod(p, &end);
2303  if (errno == ERANGE) {
2304  OutOfRange();
2305  rb_warning("Float %.*s%s out of range", w, p, ellipsis);
2306  errno = 0;
2307  }
2308  if (badcheck) {
2309  if (!end || p == end) goto bad;
2310  while (*end && ISSPACE(*end)) end++;
2311  if (*end) goto bad;
2312  }
2313  }
2314  if (errno == ERANGE) {
2315  errno = 0;
2316  OutOfRange();
2317  rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
2318  }
2319  return d;
2320 }
2321 
2322 double
2323 rb_str_to_dbl(VALUE str, int badcheck)
2324 {
2325  char *s;
2326  long len;
2327  double ret;
2328  VALUE v = 0;
2329 
2330  StringValue(str);
2331  s = RSTRING_PTR(str);
2332  len = RSTRING_LEN(str);
2333  if (s) {
2334  if (badcheck && memchr(s, '\0', len)) {
2335  rb_raise(rb_eArgError, "string for Float contains null byte");
2336  }
2337  if (s[len]) { /* no sentinel somehow */
2338  char *p = ALLOCV(v, len);
2339  MEMCPY(p, s, char, len);
2340  p[len] = '\0';
2341  s = p;
2342  }
2343  }
2344  ret = rb_cstr_to_dbl(s, badcheck);
2345  if (v)
2346  ALLOCV_END(v);
2347  return ret;
2348 }
2349 
2350 VALUE
2352 {
2353  switch (TYPE(val)) {
2354  case T_FIXNUM:
2355  return DBL2NUM((double)FIX2LONG(val));
2356 
2357  case T_FLOAT:
2358  return val;
2359 
2360  case T_BIGNUM:
2361  return DBL2NUM(rb_big2dbl(val));
2362 
2363  case T_STRING:
2364  return DBL2NUM(rb_str_to_dbl(val, TRUE));
2365 
2366  case T_NIL:
2367  rb_raise(rb_eTypeError, "can't convert nil into Float");
2368  break;
2369 
2370  default:
2371  return rb_convert_type(val, T_FLOAT, "Float", "to_f");
2372  }
2373 }
2374 
2375 /*
2376  * call-seq:
2377  * Float(arg) -> float
2378  *
2379  * Returns <i>arg</i> converted to a float. Numeric types are converted
2380  * directly, the rest are converted using <i>arg</i>.to_f. As of Ruby
2381  * 1.8, converting <code>nil</code> generates a <code>TypeError</code>.
2382  *
2383  * Float(1) #=> 1.0
2384  * Float("123.456") #=> 123.456
2385  */
2386 
2387 static VALUE
2389 {
2390  return rb_Float(arg);
2391 }
2392 
2393 VALUE
2395 {
2396  if (TYPE(val) == T_FLOAT) return val;
2397  if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
2398  rb_raise(rb_eTypeError, "can't convert %s into Float",
2399  NIL_P(val) ? "nil" :
2400  val == Qtrue ? "true" :
2401  val == Qfalse ? "false" :
2402  rb_obj_classname(val));
2403  }
2404  return rb_convert_type(val, T_FLOAT, "Float", "to_f");
2405 }
2406 
2407 VALUE
2409 {
2410  if (TYPE(val) == T_FLOAT) return val;
2411  if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
2412  return Qnil;
2413  }
2414  return rb_check_convert_type(val, T_FLOAT, "Float", "to_f");
2415 }
2416 
2417 double
2419 {
2420  switch (TYPE(val)) {
2421  case T_FLOAT:
2422  return RFLOAT_VALUE(val);
2423 
2424  case T_STRING:
2425  rb_raise(rb_eTypeError, "no implicit conversion to float from string");
2426  break;
2427 
2428  case T_NIL:
2429  rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
2430  break;
2431 
2432  default:
2433  break;
2434  }
2435 
2436  return RFLOAT_VALUE(rb_Float(val));
2437 }
2438 
2439 VALUE
2441 {
2442  VALUE tmp = rb_check_string_type(val);
2443  if (NIL_P(tmp))
2444  tmp = rb_convert_type(val, T_STRING, "String", "to_s");
2445  return tmp;
2446 }
2447 
2448 
2449 /*
2450  * call-seq:
2451  * String(arg) -> string
2452  *
2453  * Converts <i>arg</i> to a <code>String</code> by calling its
2454  * <code>to_s</code> method.
2455  *
2456  * String(self) #=> "main"
2457  * String(self.class) #=> "Object"
2458  * String(123456) #=> "123456"
2459  */
2460 
2461 static VALUE
2463 {
2464  return rb_String(arg);
2465 }
2466 
2467 VALUE
2469 {
2470  VALUE tmp = rb_check_array_type(val);
2471 
2472  if (NIL_P(tmp)) {
2473  tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
2474  if (NIL_P(tmp)) {
2475  return rb_ary_new3(1, val);
2476  }
2477  }
2478  return tmp;
2479 }
2480 
2481 /*
2482  * call-seq:
2483  * Array(arg) -> array
2484  *
2485  * Returns <i>arg</i> as an <code>Array</code>. First tries to call
2486  * <i>arg</i><code>.to_ary</code>, then <i>arg</i><code>.to_a</code>.
2487  *
2488  * Array(1..5) #=> [1, 2, 3, 4, 5]
2489  */
2490 
2491 static VALUE
2493 {
2494  return rb_Array(arg);
2495 }
2496 
2497 /*
2498  * Document-class: Class
2499  *
2500  * Classes in Ruby are first-class objects---each is an instance of
2501  * class <code>Class</code>.
2502  *
2503  * When a new class is created (typically using <code>class Name ...
2504  * end</code>), an object of type <code>Class</code> is created and
2505  * assigned to a global constant (<code>Name</code> in this case). When
2506  * <code>Name.new</code> is called to create a new object, the
2507  * <code>new</code> method in <code>Class</code> is run by default.
2508  * This can be demonstrated by overriding <code>new</code> in
2509  * <code>Class</code>:
2510  *
2511  * class Class
2512  * alias oldNew new
2513  * def new(*args)
2514  * print "Creating a new ", self.name, "\n"
2515  * oldNew(*args)
2516  * end
2517  * end
2518  *
2519  *
2520  * class Name
2521  * end
2522  *
2523  *
2524  * n = Name.new
2525  *
2526  * <em>produces:</em>
2527  *
2528  * Creating a new Name
2529  *
2530  * Classes, modules, and objects are interrelated. In the diagram
2531  * that follows, the vertical arrows represent inheritance, and the
2532  * parentheses meta-classes. All metaclasses are instances
2533  * of the class `Class'.
2534  * +---------+ +-...
2535  * | | |
2536  * BasicObject-----|-->(BasicObject)-------|-...
2537  * ^ | ^ |
2538  * | | | |
2539  * Object---------|----->(Object)---------|-...
2540  * ^ | ^ |
2541  * | | | |
2542  * +-------+ | +--------+ |
2543  * | | | | | |
2544  * | Module-|---------|--->(Module)-|-...
2545  * | ^ | | ^ |
2546  * | | | | | |
2547  * | Class-|---------|---->(Class)-|-...
2548  * | ^ | | ^ |
2549  * | +---+ | +----+
2550  * | |
2551  * obj--->OtherClass---------->(OtherClass)-----------...
2552  *
2553  */
2554 
2555 
2574 /* Document-class: BasicObject
2575  *
2576  * BasicObject is the parent class of all classes in Ruby. It's an explicit
2577  * blank class.
2578  *
2579  * BasicObject can be used for creating object hierarchies independent of
2580  * Ruby's object hierarchy, proxy objects like the Delegator class, or other
2581  * uses where namespace pollution from Ruby's methods and classes must be
2582  * avoided.
2583  *
2584  * To avoid polluting BasicObject for other users an appropriately named
2585  * subclass of BasicObject should be created instead of directly modifying
2586  * BasicObject:
2587  *
2588  * class MyObjectSystem < BasicObject
2589  * end
2590  *
2591  * BasicObject does not include Kernel (for methods like +puts+) and
2592  * BasicObject is outside of the namespace of the standard library so common
2593  * classes will not be found without a using a full class path.
2594  *
2595  * A variety of strategies can be used to provide useful portions of the
2596  * standard library to subclasses of BasicObject. A subclass could
2597  * <code>include Kernel</code> to obtain +puts+, +exit+, etc. A custom
2598  * Kernel-like module could be created and included or delegation can be used
2599  * via #method_missing:
2600  *
2601  * class MyObjectSystem < BasicObject
2602  * DELEGATE = [:puts, :p]
2603  *
2604  * def method_missing(name, *args, &block)
2605  * super unless DELEGATE.include? name
2606  * ::Kernel.send(name, *args, &block)
2607  * end
2608  *
2609  * def respond_to_missing?(name, include_private = false)
2610  * DELGATE.include?(name) or super
2611  * end
2612  * end
2613  *
2614  * Access to classes and modules from the Ruby standard library can be
2615  * obtained in a BasicObject subclass by referencing the desired constant
2616  * from the root like <code>::File</code> or <code>::Enumerator</code>.
2617  * Like #method_missing, #const_missing can be used to delegate constant
2618  * lookup to +Object+:
2619  *
2620  * class MyObjectSystem < BasicObject
2621  * def self.const_missing(name)
2622  * ::Object.const_get(name)
2623  * end
2624  * end
2625  */
2626 
2627 /* Document-class: Object
2628  *
2629  * Object is the root of Ruby's class hierarchy. Its methods are available
2630  * to all classes unless explicitly overridden.
2631  *
2632  * Object mixes in the Kernel module, making the built-in kernel functions
2633  * globally accessible. Although the instance methods of Object are defined
2634  * by the Kernel module, we have chosen to document them here for clarity.
2635  *
2636  * In the descriptions of Object's methods, the parameter <i>symbol</i> refers
2637  * to a symbol, which is either a quoted string or a Symbol (such as
2638  * <code>:name</code>).
2639  */
2640 
2641 void
2643 {
2644  int i;
2645 
2647 
2648 #if 0
2649  // teach RDoc about these classes
2650  rb_cBasicObject = rb_define_class("BasicObject", Qnil);
2652  rb_cModule = rb_define_class("Module", rb_cObject);
2653  rb_cClass = rb_define_class("Class", rb_cModule);
2654 #endif
2655 
2656 #undef rb_intern
2657 #define rb_intern(str) rb_intern_const(str)
2658 
2665 
2666  rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1);
2667  rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1);
2668  rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
2669 
2670  rb_mKernel = rb_define_module("Kernel");
2675  rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
2676  rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
2677  rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
2678 
2679  rb_define_method(rb_mKernel, "nil?", rb_false, 0);
2680  rb_define_method(rb_mKernel, "===", rb_equal, 1);
2686 
2688  rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
2691  rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
2692  rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
2693  rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1);
2694 
2696  rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
2697  rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
2698  rb_define_method(rb_mKernel, "untrust", rb_obj_untrust, 0);
2699  rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0);
2701  rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
2703 
2705  rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
2706  rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */
2707  rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
2708  rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
2709  rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */
2710  rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */
2711  rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
2712  rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
2713  rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
2714  rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
2715  rb_define_private_method(rb_mKernel, "remove_instance_variable",
2716  rb_obj_remove_instance_variable, 1); /* in variable.c */
2717 
2718  rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
2722 
2723  rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */
2724  rb_define_global_function("format", rb_f_sprintf, -1); /* in sprintf.c */
2725 
2726  rb_define_global_function("Integer", rb_f_integer, -1);
2728 
2729  rb_define_global_function("String", rb_f_string, 1);
2731 
2732  rb_cNilClass = rb_define_class("NilClass", rb_cObject);
2733  rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
2734  rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
2735  rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
2736  rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
2737  rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
2741 
2742  rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
2745  /*
2746  * An alias of +nil+
2747  */
2748  rb_define_global_const("NIL", Qnil);
2749 
2750  rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
2758  rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
2760  rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
2761  rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
2762  rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */
2763  rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
2764 
2769 
2771  rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
2772  rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
2773  rb_define_method(rb_cModule, "public_instance_methods",
2774  rb_class_public_instance_methods, -1); /* in class.c */
2775  rb_define_method(rb_cModule, "protected_instance_methods",
2776  rb_class_protected_instance_methods, -1); /* in class.c */
2777  rb_define_method(rb_cModule, "private_instance_methods",
2778  rb_class_private_instance_methods, -1); /* in class.c */
2779 
2780  rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
2781  rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
2782  rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
2783  rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
2784  rb_define_private_method(rb_cModule, "remove_const",
2785  rb_mod_remove_const, 1); /* in variable.c */
2786  rb_define_method(rb_cModule, "const_missing",
2787  rb_mod_const_missing, 1); /* in variable.c */
2788  rb_define_method(rb_cModule, "class_variables",
2789  rb_mod_class_variables, 0); /* in variable.c */
2790  rb_define_method(rb_cModule, "remove_class_variable",
2791  rb_mod_remove_cvar, 1); /* in variable.c */
2792  rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
2793  rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
2794  rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
2795  rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1);
2796  rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1);
2797 
2798  rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
2800  rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
2801  rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
2803  rb_undef_method(rb_cClass, "extend_object");
2804  rb_undef_method(rb_cClass, "append_features");
2805 
2806  rb_cData = rb_define_class("Data", rb_cObject);
2808 
2809  rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
2816  /*
2817  * An alias of +true+
2818  */
2819  rb_define_global_const("TRUE", Qtrue);
2820 
2821  rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
2828  /*
2829  * An alias of +false+
2830  */
2831  rb_define_global_const("FALSE", Qfalse);
2832 
2833  id_eq = rb_intern("==");
2834  id_eql = rb_intern("eql?");
2835  id_match = rb_intern("=~");
2836  id_inspect = rb_intern("inspect");
2837  id_init_copy = rb_intern("initialize_copy");
2838  id_init_clone = rb_intern("initialize_clone");
2839  id_init_dup = rb_intern("initialize_dup");
2840 
2841  for (i=0; conv_method_names[i].method; i++) {
2843  }
2844 }
2845