Ruby  1.9.3p547(2014-05-14revision45962)
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) | (RBASIC(clone)->flags&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+ and +other+ are the same object
1238  * or <code>obj == other</code>, otherwise nil.
1239  */
1240 static VALUE
1242 {
1243  if (obj1 == obj2 || rb_equal(obj1, obj2))
1244  return INT2FIX(0);
1245  return Qnil;
1246 }
1247 
1248 /***********************************************************************
1249  *
1250  * Document-class: Module
1251  *
1252  * A <code>Module</code> is a collection of methods and constants. The
1253  * methods in a module may be instance methods or module methods.
1254  * Instance methods appear as methods in a class when the module is
1255  * included, module methods do not. Conversely, module methods may be
1256  * called without creating an encapsulating object, while instance
1257  * methods may not. (See <code>Module#module_function</code>)
1258  *
1259  * In the descriptions that follow, the parameter <i>sym</i> refers
1260  * to a symbol, which is either a quoted string or a
1261  * <code>Symbol</code> (such as <code>:name</code>).
1262  *
1263  * module Mod
1264  * include Math
1265  * CONST = 1
1266  * def meth
1267  * # ...
1268  * end
1269  * end
1270  * Mod.class #=> Module
1271  * Mod.constants #=> [:CONST, :PI, :E]
1272  * Mod.instance_methods #=> [:meth]
1273  *
1274  */
1275 
1276 /*
1277  * call-seq:
1278  * mod.to_s -> string
1279  *
1280  * Return a string representing this module or class. For basic
1281  * classes and modules, this is the name. For singletons, we
1282  * show information on the thing we're attached to as well.
1283  */
1284 
1285 static VALUE
1287 {
1288  if (FL_TEST(klass, FL_SINGLETON)) {
1289  VALUE s = rb_usascii_str_new2("#<");
1290  VALUE v = rb_iv_get(klass, "__attached__");
1291 
1292  rb_str_cat2(s, "Class:");
1293  switch (TYPE(v)) {
1294  case T_CLASS: case T_MODULE:
1295  rb_str_append(s, rb_inspect(v));
1296  break;
1297  default:
1298  rb_str_append(s, rb_any_to_s(v));
1299  break;
1300  }
1301  rb_str_cat2(s, ">");
1302 
1303  return s;
1304  }
1305  return rb_str_dup(rb_class_name(klass));
1306 }
1307 
1308 /*
1309  * call-seq:
1310  * mod.freeze -> mod
1311  *
1312  * Prevents further modifications to <i>mod</i>.
1313  *
1314  * This method returns self.
1315  */
1316 
1317 static VALUE
1319 {
1320  rb_class_name(mod);
1321  return rb_obj_freeze(mod);
1322 }
1323 
1324 /*
1325  * call-seq:
1326  * mod === obj -> true or false
1327  *
1328  * Case Equality---Returns <code>true</code> if <i>anObject</i> is an
1329  * instance of <i>mod</i> or one of <i>mod</i>'s descendants. Of
1330  * limited use for modules, but can be used in <code>case</code>
1331  * statements to classify objects by class.
1332  */
1333 
1334 static VALUE
1336 {
1337  return rb_obj_is_kind_of(arg, mod);
1338 }
1339 
1340 /*
1341  * call-seq:
1342  * mod <= other -> true, false, or nil
1343  *
1344  * Returns true if <i>mod</i> is a subclass of <i>other</i> or
1345  * is the same as <i>other</i>. Returns
1346  * <code>nil</code> if there's no relationship between the two.
1347  * (Think of the relationship in terms of the class definition:
1348  * "class A<B" implies "A<B").
1349  *
1350  */
1351 
1352 VALUE
1354 {
1355  VALUE start = mod;
1356 
1357  if (mod == arg) return Qtrue;
1358  switch (TYPE(arg)) {
1359  case T_MODULE:
1360  case T_CLASS:
1361  break;
1362  default:
1363  rb_raise(rb_eTypeError, "compared with non class/module");
1364  }
1365  while (mod) {
1366  if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg))
1367  return Qtrue;
1368  mod = RCLASS_SUPER(mod);
1369  }
1370  /* not mod < arg; check if mod > arg */
1371  while (arg) {
1372  if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start))
1373  return Qfalse;
1374  arg = RCLASS_SUPER(arg);
1375  }
1376  return Qnil;
1377 }
1378 
1379 /*
1380  * call-seq:
1381  * mod < other -> true, false, or nil
1382  *
1383  * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
1384  * <code>nil</code> if there's no relationship between the two.
1385  * (Think of the relationship in terms of the class definition:
1386  * "class A<B" implies "A<B").
1387  *
1388  */
1389 
1390 static VALUE
1392 {
1393  if (mod == arg) return Qfalse;
1394  return rb_class_inherited_p(mod, arg);
1395 }
1396 
1397 
1398 /*
1399  * call-seq:
1400  * mod >= other -> true, false, or nil
1401  *
1402  * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
1403  * two modules are the same. Returns
1404  * <code>nil</code> if there's no relationship between the two.
1405  * (Think of the relationship in terms of the class definition:
1406  * "class A<B" implies "B>A").
1407  *
1408  */
1409 
1410 static VALUE
1412 {
1413  switch (TYPE(arg)) {
1414  case T_MODULE:
1415  case T_CLASS:
1416  break;
1417  default:
1418  rb_raise(rb_eTypeError, "compared with non class/module");
1419  }
1420 
1421  return rb_class_inherited_p(arg, mod);
1422 }
1423 
1424 /*
1425  * call-seq:
1426  * mod > other -> true, false, or nil
1427  *
1428  * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
1429  * <code>nil</code> if there's no relationship between the two.
1430  * (Think of the relationship in terms of the class definition:
1431  * "class A<B" implies "B>A").
1432  *
1433  */
1434 
1435 static VALUE
1437 {
1438  if (mod == arg) return Qfalse;
1439  return rb_mod_ge(mod, arg);
1440 }
1441 
1442 /*
1443  * call-seq:
1444  * mod <=> other_mod -> -1, 0, +1, or nil
1445  *
1446  * Comparison---Returns -1 if <i>mod</i> includes <i>other_mod</i>, 0 if
1447  * <i>mod</i> is the same as <i>other_mod</i>, and +1 if <i>mod</i> is
1448  * included by <i>other_mod</i>. Returns <code>nil</code> if <i>mod</i>
1449  * has no relationship with <i>other_mod</i> or if <i>other_mod</i> is
1450  * not a module.
1451  */
1452 
1453 static VALUE
1455 {
1456  VALUE cmp;
1457 
1458  if (mod == arg) return INT2FIX(0);
1459  switch (TYPE(arg)) {
1460  case T_MODULE:
1461  case T_CLASS:
1462  break;
1463  default:
1464  return Qnil;
1465  }
1466 
1467  cmp = rb_class_inherited_p(mod, arg);
1468  if (NIL_P(cmp)) return Qnil;
1469  if (cmp) {
1470  return INT2FIX(-1);
1471  }
1472  return INT2FIX(1);
1473 }
1474 
1475 static VALUE
1477 {
1478  VALUE mod = rb_module_new();
1479 
1480  RBASIC(mod)->klass = klass;
1481  return mod;
1482 }
1483 
1484 static VALUE
1486 {
1487  return rb_class_boot(0);
1488 }
1489 
1490 /*
1491  * call-seq:
1492  * Module.new -> mod
1493  * Module.new {|mod| block } -> mod
1494  *
1495  * Creates a new anonymous module. If a block is given, it is passed
1496  * the module object, and the block is evaluated in the context of this
1497  * module using <code>module_eval</code>.
1498  *
1499  * fred = Module.new do
1500  * def meth1
1501  * "hello"
1502  * end
1503  * def meth2
1504  * "bye"
1505  * end
1506  * end
1507  * a = "my string"
1508  * a.extend(fred) #=> "my string"
1509  * a.meth1 #=> "hello"
1510  * a.meth2 #=> "bye"
1511  *
1512  * Assign the module to a constant (name starting uppercase) if you
1513  * want to treat it like a regular module.
1514  */
1515 
1516 static VALUE
1518 {
1519  if (rb_block_given_p()) {
1520  rb_mod_module_exec(1, &module, module);
1521  }
1522  return Qnil;
1523 }
1524 
1525 /*
1526  * call-seq:
1527  * Class.new(super_class=Object) -> a_class
1528  * Class.new(super_class=Object) { |mod| ... } -> a_class
1529  *
1530  * Creates a new anonymous (unnamed) class with the given superclass
1531  * (or <code>Object</code> if no parameter is given). You can give a
1532  * class a name by assigning the class object to a constant.
1533  *
1534  * If a block is given, it is passed the class object, and the block
1535  * is evaluated in the context of this class using
1536  * <code>class_eval</code>.
1537  *
1538  * fred = Class.new do
1539  * def meth1
1540  * "hello"
1541  * end
1542  * def meth2
1543  * "bye"
1544  * end
1545  * end
1546  *
1547  * a = fred.new #=> #<#<Class:0x100381890>:0x100376b98>
1548  * a.meth1 #=> "hello"
1549  * a.meth2 #=> "bye"
1550  *
1551  * Assign the class to a constant (name starting uppercase) if you
1552  * want to treat it like a regular class.
1553  */
1554 
1555 static VALUE
1557 {
1558  VALUE super;
1559 
1560  if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
1561  rb_raise(rb_eTypeError, "already initialized class");
1562  }
1563  if (argc == 0) {
1564  super = rb_cObject;
1565  }
1566  else {
1567  rb_scan_args(argc, argv, "01", &super);
1568  rb_check_inheritable(super);
1569  }
1570  RCLASS_SUPER(klass) = super;
1571  rb_make_metaclass(klass, RBASIC(super)->klass);
1572  rb_class_inherited(super, klass);
1573  rb_mod_initialize(klass);
1574 
1575  return klass;
1576 }
1577 
1578 /*
1579  * call-seq:
1580  * class.allocate() -> obj
1581  *
1582  * Allocates space for a new object of <i>class</i>'s class and does not
1583  * call initialize on the new instance. The returned object must be an
1584  * instance of <i>class</i>.
1585  *
1586  * klass = Class.new do
1587  * def initialize(*args)
1588  * @initialized = true
1589  * end
1590  *
1591  * def initialized?
1592  * @initialized || false
1593  * end
1594  * end
1595  *
1596  * klass.allocate.initialized? #=> false
1597  *
1598  */
1599 
1600 VALUE
1602 {
1603  VALUE obj;
1604 
1605  if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
1606  rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
1607  }
1608  if (FL_TEST(klass, FL_SINGLETON)) {
1609  rb_raise(rb_eTypeError, "can't create instance of singleton class");
1610  }
1611  obj = rb_funcall(klass, ID_ALLOCATOR, 0, 0);
1612  if (rb_obj_class(obj) != rb_class_real(klass)) {
1613  rb_raise(rb_eTypeError, "wrong instance allocation");
1614  }
1615  return obj;
1616 }
1617 
1618 static VALUE
1620 {
1621  NEWOBJ(obj, struct RObject);
1622  OBJSETUP(obj, klass, T_OBJECT);
1623  return (VALUE)obj;
1624 }
1625 
1626 /*
1627  * call-seq:
1628  * class.new(args, ...) -> obj
1629  *
1630  * Calls <code>allocate</code> to create a new object of
1631  * <i>class</i>'s class, then invokes that object's
1632  * <code>initialize</code> method, passing it <i>args</i>.
1633  * This is the method that ends up getting called whenever
1634  * an object is constructed using .new.
1635  *
1636  */
1637 
1638 VALUE
1640 {
1641  VALUE obj;
1642 
1643  obj = rb_obj_alloc(klass);
1644  rb_obj_call_init(obj, argc, argv);
1645 
1646  return obj;
1647 }
1648 
1649 /*
1650  * call-seq:
1651  * class.superclass -> a_super_class or nil
1652  *
1653  * Returns the superclass of <i>class</i>, or <code>nil</code>.
1654  *
1655  * File.superclass #=> IO
1656  * IO.superclass #=> Object
1657  * Object.superclass #=> BasicObject
1658  * class Foo; end
1659  * class Bar < Foo; end
1660  * Bar.superclass #=> Foo
1661  *
1662  * returns nil when the given class hasn't a parent class:
1663  *
1664  * BasicObject.superclass #=> nil
1665  *
1666  */
1667 
1668 VALUE
1670 {
1671  VALUE super = RCLASS_SUPER(klass);
1672 
1673  if (!super) {
1674  if (klass == rb_cBasicObject) return Qnil;
1675  rb_raise(rb_eTypeError, "uninitialized class");
1676  }
1677  while (TYPE(super) == T_ICLASS) {
1678  super = RCLASS_SUPER(super);
1679  }
1680  if (!super) {
1681  return Qnil;
1682  }
1683  return super;
1684 }
1685 
1686 VALUE
1688 {
1689  return RCLASS_SUPER(klass);
1690 }
1691 
1692 /*
1693  * call-seq:
1694  * attr_reader(symbol, ...) -> nil
1695  * attr(symbol, ...) -> nil
1696  *
1697  * Creates instance variables and corresponding methods that return the
1698  * value of each instance variable. Equivalent to calling
1699  * ``<code>attr</code><i>:name</i>'' on each name in turn.
1700  */
1701 
1702 static VALUE
1704 {
1705  int i;
1706 
1707  for (i=0; i<argc; i++) {
1708  rb_attr(klass, rb_to_id(argv[i]), TRUE, FALSE, TRUE);
1709  }
1710  return Qnil;
1711 }
1712 
1713 VALUE
1715 {
1716  if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
1717  rb_warning("optional boolean argument is obsoleted");
1718  rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), TRUE);
1719  return Qnil;
1720  }
1721  return rb_mod_attr_reader(argc, argv, klass);
1722 }
1723 
1724 /*
1725  * call-seq:
1726  * attr_writer(symbol, ...) -> nil
1727  *
1728  * Creates an accessor method to allow assignment to the attribute
1729  * <i>aSymbol</i><code>.id2name</code>.
1730  */
1731 
1732 static VALUE
1734 {
1735  int i;
1736 
1737  for (i=0; i<argc; i++) {
1738  rb_attr(klass, rb_to_id(argv[i]), FALSE, TRUE, TRUE);
1739  }
1740  return Qnil;
1741 }
1742 
1743 /*
1744  * call-seq:
1745  * attr_accessor(symbol, ...) -> nil
1746  *
1747  * Defines a named attribute for this module, where the name is
1748  * <i>symbol.</i><code>id2name</code>, creating an instance variable
1749  * (<code>@name</code>) and a corresponding access method to read it.
1750  * Also creates a method called <code>name=</code> to set the attribute.
1751  *
1752  * module Mod
1753  * attr_accessor(:one, :two)
1754  * end
1755  * Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
1756  */
1757 
1758 static VALUE
1760 {
1761  int i;
1762 
1763  for (i=0; i<argc; i++) {
1764  rb_attr(klass, rb_to_id(argv[i]), TRUE, TRUE, TRUE);
1765  }
1766  return Qnil;
1767 }
1768 
1769 /*
1770  * call-seq:
1771  * mod.const_get(sym, inherit=true) -> obj
1772  *
1773  * Checks for a constant with the given name in <i>mod</i>
1774  * If +inherit+ is set, the lookup will also search
1775  * the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
1776  *
1777  * The value of the constant is returned if a definition is found,
1778  * otherwise a +NameError+ is raised.
1779  *
1780  * Math.const_get(:PI) #=> 3.14159265358979
1781  */
1782 
1783 static VALUE
1785 {
1786  VALUE name, recur;
1787  ID id;
1788 
1789  if (argc == 1) {
1790  name = argv[0];
1791  recur = Qtrue;
1792  }
1793  else {
1794  rb_scan_args(argc, argv, "11", &name, &recur);
1795  }
1796  id = rb_to_id(name);
1797  if (!rb_is_const_id(id)) {
1798  rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1799  }
1800  return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
1801 }
1802 
1803 /*
1804  * call-seq:
1805  * mod.const_set(sym, obj) -> obj
1806  *
1807  * Sets the named constant to the given object, returning that object.
1808  * Creates a new constant if no constant with the given name previously
1809  * existed.
1810  *
1811  * Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
1812  * Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
1813  */
1814 
1815 static VALUE
1817 {
1818  ID id = rb_to_id(name);
1819 
1820  if (!rb_is_const_id(id)) {
1821  rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1822  }
1823  rb_const_set(mod, id, value);
1824  return value;
1825 }
1826 
1827 /*
1828  * call-seq:
1829  * mod.const_defined?(sym, inherit=true) -> true or false
1830  *
1831  * Checks for a constant with the given name in <i>mod</i>
1832  * If +inherit+ is set, the lookup will also search
1833  * the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
1834  *
1835  * Returns whether or not a definition is found:
1836  *
1837  * Math.const_defined? "PI" #=> true
1838  * IO.const_defined? :SYNC #=> true
1839  * IO.const_defined? :SYNC, false #=> false
1840  */
1841 
1842 static VALUE
1844 {
1845  VALUE name, recur;
1846  ID id;
1847 
1848  if (argc == 1) {
1849  name = argv[0];
1850  recur = Qtrue;
1851  }
1852  else {
1853  rb_scan_args(argc, argv, "11", &name, &recur);
1854  }
1855  id = rb_to_id(name);
1856  if (!rb_is_const_id(id)) {
1857  rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1858  }
1859  return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
1860 }
1861 
1862 /*
1863  * call-seq:
1864  * obj.instance_variable_get(symbol) -> obj
1865  *
1866  * Returns the value of the given instance variable, or nil if the
1867  * instance variable is not set. The <code>@</code> part of the
1868  * variable name should be included for regular instance
1869  * variables. Throws a <code>NameError</code> exception if the
1870  * supplied symbol is not valid as an instance variable name.
1871  *
1872  * class Fred
1873  * def initialize(p1, p2)
1874  * @a, @b = p1, p2
1875  * end
1876  * end
1877  * fred = Fred.new('cat', 99)
1878  * fred.instance_variable_get(:@a) #=> "cat"
1879  * fred.instance_variable_get("@b") #=> 99
1880  */
1881 
1882 static VALUE
1884 {
1885  ID id = rb_to_id(iv);
1886 
1887  if (!rb_is_instance_id(id)) {
1888  rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1889  }
1890  return rb_ivar_get(obj, id);
1891 }
1892 
1893 /*
1894  * call-seq:
1895  * obj.instance_variable_set(symbol, obj) -> obj
1896  *
1897  * Sets the instance variable names by <i>symbol</i> to
1898  * <i>object</i>, thereby frustrating the efforts of the class's
1899  * author to attempt to provide proper encapsulation. The variable
1900  * did not have to exist prior to this call.
1901  *
1902  * class Fred
1903  * def initialize(p1, p2)
1904  * @a, @b = p1, p2
1905  * end
1906  * end
1907  * fred = Fred.new('cat', 99)
1908  * fred.instance_variable_set(:@a, 'dog') #=> "dog"
1909  * fred.instance_variable_set(:@c, 'cat') #=> "cat"
1910  * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
1911  */
1912 
1913 static VALUE
1915 {
1916  ID id = rb_to_id(iv);
1917 
1918  if (!rb_is_instance_id(id)) {
1919  rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1920  }
1921  return rb_ivar_set(obj, id, val);
1922 }
1923 
1924 /*
1925  * call-seq:
1926  * obj.instance_variable_defined?(symbol) -> true or false
1927  *
1928  * Returns <code>true</code> if the given instance variable is
1929  * defined in <i>obj</i>.
1930  *
1931  * class Fred
1932  * def initialize(p1, p2)
1933  * @a, @b = p1, p2
1934  * end
1935  * end
1936  * fred = Fred.new('cat', 99)
1937  * fred.instance_variable_defined?(:@a) #=> true
1938  * fred.instance_variable_defined?("@b") #=> true
1939  * fred.instance_variable_defined?("@c") #=> false
1940  */
1941 
1942 static VALUE
1944 {
1945  ID id = rb_to_id(iv);
1946 
1947  if (!rb_is_instance_id(id)) {
1948  rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1949  }
1950  return rb_ivar_defined(obj, id);
1951 }
1952 
1953 /*
1954  * call-seq:
1955  * mod.class_variable_get(symbol) -> obj
1956  *
1957  * Returns the value of the given class variable (or throws a
1958  * <code>NameError</code> exception). The <code>@@</code> part of the
1959  * variable name should be included for regular class variables
1960  *
1961  * class Fred
1962  * @@foo = 99
1963  * end
1964  * Fred.class_variable_get(:@@foo) #=> 99
1965  */
1966 
1967 static VALUE
1969 {
1970  ID id = rb_to_id(iv);
1971 
1972  if (!rb_is_class_id(id)) {
1973  rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
1974  }
1975  return rb_cvar_get(obj, id);
1976 }
1977 
1978 /*
1979  * call-seq:
1980  * obj.class_variable_set(symbol, obj) -> obj
1981  *
1982  * Sets the class variable names by <i>symbol</i> to
1983  * <i>object</i>.
1984  *
1985  * class Fred
1986  * @@foo = 99
1987  * def foo
1988  * @@foo
1989  * end
1990  * end
1991  * Fred.class_variable_set(:@@foo, 101) #=> 101
1992  * Fred.new.foo #=> 101
1993  */
1994 
1995 static VALUE
1997 {
1998  ID id = rb_to_id(iv);
1999 
2000  if (!rb_is_class_id(id)) {
2001  rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
2002  }
2003  rb_cvar_set(obj, id, val);
2004  return val;
2005 }
2006 
2007 /*
2008  * call-seq:
2009  * obj.class_variable_defined?(symbol) -> true or false
2010  *
2011  * Returns <code>true</code> if the given class variable is defined
2012  * in <i>obj</i>.
2013  *
2014  * class Fred
2015  * @@foo = 99
2016  * end
2017  * Fred.class_variable_defined?(:@@foo) #=> true
2018  * Fred.class_variable_defined?(:@@bar) #=> false
2019  */
2020 
2021 static VALUE
2023 {
2024  ID id = rb_to_id(iv);
2025 
2026  if (!rb_is_class_id(id)) {
2027  rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
2028  }
2029  return rb_cvar_defined(obj, id);
2030 }
2031 
2032 static struct conv_method_tbl {
2033  const char *method;
2035 } conv_method_names[] = {
2036  {"to_int", 0},
2037  {"to_ary", 0},
2038  {"to_str", 0},
2039  {"to_sym", 0},
2040  {"to_hash", 0},
2041  {"to_proc", 0},
2042  {"to_io", 0},
2043  {"to_a", 0},
2044  {"to_s", 0},
2045  {NULL, 0}
2046 };
2047 
2048 static VALUE
2049 convert_type(VALUE val, const char *tname, const char *method, int raise)
2050 {
2051  ID m = 0;
2052  int i;
2053  VALUE r;
2054 
2055  for (i=0; conv_method_names[i].method; i++) {
2056  if (conv_method_names[i].method[0] == method[0] &&
2057  strcmp(conv_method_names[i].method, method) == 0) {
2058  m = conv_method_names[i].id;
2059  break;
2060  }
2061  }
2062  if (!m) m = rb_intern(method);
2063  r = rb_check_funcall(val, m, 0, 0);
2064  if (r == Qundef) {
2065  if (raise) {
2066  rb_raise(rb_eTypeError, "can't convert %s into %s",
2067  NIL_P(val) ? "nil" :
2068  val == Qtrue ? "true" :
2069  val == Qfalse ? "false" :
2070  rb_obj_classname(val),
2071  tname);
2072  }
2073  return Qnil;
2074  }
2075  return r;
2076 }
2077 
2078 VALUE
2079 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
2080 {
2081  VALUE v;
2082 
2083  if (TYPE(val) == type) return val;
2084  v = convert_type(val, tname, method, TRUE);
2085  if (TYPE(v) != type) {
2086  const char *cname = rb_obj_classname(val);
2087  rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
2088  cname, tname, cname, method, rb_obj_classname(v));
2089  }
2090  return v;
2091 }
2092 
2093 VALUE
2094 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
2095 {
2096  VALUE v;
2097 
2098  /* always convert T_DATA */
2099  if (TYPE(val) == type && type != T_DATA) return val;
2100  v = convert_type(val, tname, method, FALSE);
2101  if (NIL_P(v)) return Qnil;
2102  if (TYPE(v) != type) {
2103  const char *cname = rb_obj_classname(val);
2104  rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
2105  cname, tname, cname, method, rb_obj_classname(v));
2106  }
2107  return v;
2108 }
2109 
2110 
2111 static VALUE
2112 rb_to_integer(VALUE val, const char *method)
2113 {
2114  VALUE v;
2115 
2116  if (FIXNUM_P(val)) return val;
2117  if (TYPE(val) == T_BIGNUM) return val;
2118  v = convert_type(val, "Integer", method, TRUE);
2119  if (!rb_obj_is_kind_of(v, rb_cInteger)) {
2120  const char *cname = rb_obj_classname(val);
2121  rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)",
2122  cname, cname, method, rb_obj_classname(v));
2123  }
2124  return v;
2125 }
2126 
2127 VALUE
2128 rb_check_to_integer(VALUE val, const char *method)
2129 {
2130  VALUE v;
2131 
2132  if (FIXNUM_P(val)) return val;
2133  if (TYPE(val) == T_BIGNUM) return val;
2134  v = convert_type(val, "Integer", method, FALSE);
2135  if (!rb_obj_is_kind_of(v, rb_cInteger)) {
2136  return Qnil;
2137  }
2138  return v;
2139 }
2140 
2141 VALUE
2143 {
2144  return rb_to_integer(val, "to_int");
2145 }
2146 
2147 static VALUE
2149 {
2150  VALUE tmp;
2151 
2152  switch (TYPE(val)) {
2153  case T_FLOAT:
2154  if (base != 0) goto arg_error;
2155  if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX
2156  && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) {
2157  break;
2158  }
2159  return rb_dbl2big(RFLOAT_VALUE(val));
2160 
2161  case T_FIXNUM:
2162  case T_BIGNUM:
2163  if (base != 0) goto arg_error;
2164  return val;
2165 
2166  case T_STRING:
2167  string_conv:
2168  return rb_str_to_inum(val, base, TRUE);
2169 
2170  case T_NIL:
2171  if (base != 0) goto arg_error;
2172  rb_raise(rb_eTypeError, "can't convert nil into Integer");
2173  break;
2174 
2175  default:
2176  break;
2177  }
2178  if (base != 0) {
2179  tmp = rb_check_string_type(val);
2180  if (!NIL_P(tmp)) goto string_conv;
2181  arg_error:
2182  rb_raise(rb_eArgError, "base specified for non string value");
2183  }
2184  tmp = convert_type(val, "Integer", "to_int", FALSE);
2185  if (NIL_P(tmp)) {
2186  return rb_to_integer(val, "to_i");
2187  }
2188  return tmp;
2189 
2190 }
2191 
2192 VALUE
2194 {
2195  return rb_convert_to_integer(val, 0);
2196 }
2197 
2198 /*
2199  * call-seq:
2200  * Integer(arg,base=0) -> integer
2201  *
2202  * Converts <i>arg</i> to a <code>Fixnum</code> or <code>Bignum</code>.
2203  * Numeric types are converted directly (with floating point numbers
2204  * being truncated). <i>base</i> (0, or between 2 and 36) is a base for
2205  * integer string representation. If <i>arg</i> is a <code>String</code>,
2206  * when <i>base</i> is omitted or equals to zero, radix indicators
2207  * (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
2208  * In any case, strings should be strictly conformed to numeric
2209  * representation. This behavior is different from that of
2210  * <code>String#to_i</code>. Non string values will be converted using
2211  * <code>to_int</code>, and <code>to_i</code>.
2212  *
2213  * Integer(123.999) #=> 123
2214  * Integer("0x1a") #=> 26
2215  * Integer(Time.new) #=> 1204973019
2216  * Integer("0930", 10) #=> 930
2217  * Integer("111", 2) #=> 7
2218  */
2219 
2220 static VALUE
2222 {
2223  VALUE arg = Qnil;
2224  int base = 0;
2225 
2226  switch (argc) {
2227  case 2:
2228  base = NUM2INT(argv[1]);
2229  case 1:
2230  arg = argv[0];
2231  break;
2232  default:
2233  /* should cause ArgumentError */
2234  rb_scan_args(argc, argv, "11", NULL, NULL);
2235  }
2236  return rb_convert_to_integer(arg, base);
2237 }
2238 
2239 double
2240 rb_cstr_to_dbl(const char *p, int badcheck)
2241 {
2242  const char *q;
2243  char *end;
2244  double d;
2245  const char *ellipsis = "";
2246  int w;
2247  enum {max_width = 20};
2248 #define OutOfRange() ((end - p > max_width) ? \
2249  (w = max_width, ellipsis = "...") : \
2250  (w = (int)(end - p), ellipsis = ""))
2251 
2252  if (!p) return 0.0;
2253  q = p;
2254  while (ISSPACE(*p)) p++;
2255 
2256  if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2257  return 0.0;
2258  }
2259 
2260  d = strtod(p, &end);
2261  if (errno == ERANGE) {
2262  OutOfRange();
2263  rb_warning("Float %.*s%s out of range", w, p, ellipsis);
2264  errno = 0;
2265  }
2266  if (p == end) {
2267  if (badcheck) {
2268  bad:
2269  rb_invalid_str(q, "Float()");
2270  }
2271  return d;
2272  }
2273  if (*end) {
2274  char buf[DBL_DIG * 4 + 10];
2275  char *n = buf;
2276  char *e = buf + sizeof(buf) - 1;
2277  char prev = 0;
2278 
2279  while (p < end && n < e) prev = *n++ = *p++;
2280  while (*p) {
2281  if (*p == '_') {
2282  /* remove underscores between digits */
2283  if (badcheck) {
2284  if (n == buf || !ISDIGIT(prev)) goto bad;
2285  ++p;
2286  if (!ISDIGIT(*p)) goto bad;
2287  }
2288  else {
2289  while (*++p == '_');
2290  continue;
2291  }
2292  }
2293  prev = *p++;
2294  if (n < e) *n++ = prev;
2295  }
2296  *n = '\0';
2297  p = buf;
2298 
2299  if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2300  return 0.0;
2301  }
2302 
2303  d = strtod(p, &end);
2304  if (errno == ERANGE) {
2305  OutOfRange();
2306  rb_warning("Float %.*s%s out of range", w, p, ellipsis);
2307  errno = 0;
2308  }
2309  if (badcheck) {
2310  if (!end || p == end) goto bad;
2311  while (*end && ISSPACE(*end)) end++;
2312  if (*end) goto bad;
2313  }
2314  }
2315  if (errno == ERANGE) {
2316  errno = 0;
2317  OutOfRange();
2318  rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
2319  }
2320  return d;
2321 }
2322 
2323 double
2324 rb_str_to_dbl(VALUE str, int badcheck)
2325 {
2326  char *s;
2327  long len;
2328  double ret;
2329  VALUE v = 0;
2330 
2331  StringValue(str);
2332  s = RSTRING_PTR(str);
2333  len = RSTRING_LEN(str);
2334  if (s) {
2335  if (badcheck && memchr(s, '\0', len)) {
2336  rb_raise(rb_eArgError, "string for Float contains null byte");
2337  }
2338  if (s[len]) { /* no sentinel somehow */
2339  char *p = ALLOCV(v, len);
2340  MEMCPY(p, s, char, len);
2341  p[len] = '\0';
2342  s = p;
2343  }
2344  }
2345  ret = rb_cstr_to_dbl(s, badcheck);
2346  if (v)
2347  ALLOCV_END(v);
2348  return ret;
2349 }
2350 
2351 VALUE
2353 {
2354  switch (TYPE(val)) {
2355  case T_FIXNUM:
2356  return DBL2NUM((double)FIX2LONG(val));
2357 
2358  case T_FLOAT:
2359  return val;
2360 
2361  case T_BIGNUM:
2362  return DBL2NUM(rb_big2dbl(val));
2363 
2364  case T_STRING:
2365  return DBL2NUM(rb_str_to_dbl(val, TRUE));
2366 
2367  case T_NIL:
2368  rb_raise(rb_eTypeError, "can't convert nil into Float");
2369  break;
2370 
2371  default:
2372  return rb_convert_type(val, T_FLOAT, "Float", "to_f");
2373  }
2374 }
2375 
2376 /*
2377  * call-seq:
2378  * Float(arg) -> float
2379  *
2380  * Returns <i>arg</i> converted to a float. Numeric types are converted
2381  * directly, the rest are converted using <i>arg</i>.to_f. As of Ruby
2382  * 1.8, converting <code>nil</code> generates a <code>TypeError</code>.
2383  *
2384  * Float(1) #=> 1.0
2385  * Float("123.456") #=> 123.456
2386  */
2387 
2388 static VALUE
2390 {
2391  return rb_Float(arg);
2392 }
2393 
2394 VALUE
2396 {
2397  if (TYPE(val) == T_FLOAT) return val;
2398  if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
2399  rb_raise(rb_eTypeError, "can't convert %s into Float",
2400  NIL_P(val) ? "nil" :
2401  val == Qtrue ? "true" :
2402  val == Qfalse ? "false" :
2403  rb_obj_classname(val));
2404  }
2405  return rb_convert_type(val, T_FLOAT, "Float", "to_f");
2406 }
2407 
2408 VALUE
2410 {
2411  if (TYPE(val) == T_FLOAT) return val;
2412  if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
2413  return Qnil;
2414  }
2415  return rb_check_convert_type(val, T_FLOAT, "Float", "to_f");
2416 }
2417 
2418 double
2420 {
2421  switch (TYPE(val)) {
2422  case T_FLOAT:
2423  return RFLOAT_VALUE(val);
2424 
2425  case T_STRING:
2426  rb_raise(rb_eTypeError, "no implicit conversion to float from string");
2427  break;
2428 
2429  case T_NIL:
2430  rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
2431  break;
2432 
2433  default:
2434  break;
2435  }
2436 
2437  return RFLOAT_VALUE(rb_Float(val));
2438 }
2439 
2440 VALUE
2442 {
2443  VALUE tmp = rb_check_string_type(val);
2444  if (NIL_P(tmp))
2445  tmp = rb_convert_type(val, T_STRING, "String", "to_s");
2446  return tmp;
2447 }
2448 
2449 
2450 /*
2451  * call-seq:
2452  * String(arg) -> string
2453  *
2454  * Converts <i>arg</i> to a <code>String</code> by calling its
2455  * <code>to_s</code> method.
2456  *
2457  * String(self) #=> "main"
2458  * String(self.class) #=> "Object"
2459  * String(123456) #=> "123456"
2460  */
2461 
2462 static VALUE
2464 {
2465  return rb_String(arg);
2466 }
2467 
2468 VALUE
2470 {
2471  VALUE tmp = rb_check_array_type(val);
2472 
2473  if (NIL_P(tmp)) {
2474  tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
2475  if (NIL_P(tmp)) {
2476  return rb_ary_new3(1, val);
2477  }
2478  }
2479  return tmp;
2480 }
2481 
2482 /*
2483  * call-seq:
2484  * Array(arg) -> array
2485  *
2486  * Returns <i>arg</i> as an <code>Array</code>. First tries to call
2487  * <i>arg</i><code>.to_ary</code>, then <i>arg</i><code>.to_a</code>.
2488  *
2489  * Array(1..5) #=> [1, 2, 3, 4, 5]
2490  */
2491 
2492 static VALUE
2494 {
2495  return rb_Array(arg);
2496 }
2497 
2498 /*
2499  * Document-class: Class
2500  *
2501  * Classes in Ruby are first-class objects---each is an instance of
2502  * class <code>Class</code>.
2503  *
2504  * When a new class is created (typically using <code>class Name ...
2505  * end</code>), an object of type <code>Class</code> is created and
2506  * assigned to a global constant (<code>Name</code> in this case). When
2507  * <code>Name.new</code> is called to create a new object, the
2508  * <code>new</code> method in <code>Class</code> is run by default.
2509  * This can be demonstrated by overriding <code>new</code> in
2510  * <code>Class</code>:
2511  *
2512  * class Class
2513  * alias oldNew new
2514  * def new(*args)
2515  * print "Creating a new ", self.name, "\n"
2516  * oldNew(*args)
2517  * end
2518  * end
2519  *
2520  *
2521  * class Name
2522  * end
2523  *
2524  *
2525  * n = Name.new
2526  *
2527  * <em>produces:</em>
2528  *
2529  * Creating a new Name
2530  *
2531  * Classes, modules, and objects are interrelated. In the diagram
2532  * that follows, the vertical arrows represent inheritance, and the
2533  * parentheses meta-classes. All metaclasses are instances
2534  * of the class `Class'.
2535  * +---------+ +-...
2536  * | | |
2537  * BasicObject-----|-->(BasicObject)-------|-...
2538  * ^ | ^ |
2539  * | | | |
2540  * Object---------|----->(Object)---------|-...
2541  * ^ | ^ |
2542  * | | | |
2543  * +-------+ | +--------+ |
2544  * | | | | | |
2545  * | Module-|---------|--->(Module)-|-...
2546  * | ^ | | ^ |
2547  * | | | | | |
2548  * | Class-|---------|---->(Class)-|-...
2549  * | ^ | | ^ |
2550  * | +---+ | +----+
2551  * | |
2552  * obj--->OtherClass---------->(OtherClass)-----------...
2553  *
2554  */
2555 
2556 
2575 /* Document-class: BasicObject
2576  *
2577  * BasicObject is the parent class of all classes in Ruby. It's an explicit
2578  * blank class.
2579  *
2580  * BasicObject can be used for creating object hierarchies independent of
2581  * Ruby's object hierarchy, proxy objects like the Delegator class, or other
2582  * uses where namespace pollution from Ruby's methods and classes must be
2583  * avoided.
2584  *
2585  * To avoid polluting BasicObject for other users an appropriately named
2586  * subclass of BasicObject should be created instead of directly modifying
2587  * BasicObject:
2588  *
2589  * class MyObjectSystem < BasicObject
2590  * end
2591  *
2592  * BasicObject does not include Kernel (for methods like +puts+) and
2593  * BasicObject is outside of the namespace of the standard library so common
2594  * classes will not be found without a using a full class path.
2595  *
2596  * A variety of strategies can be used to provide useful portions of the
2597  * standard library to subclasses of BasicObject. A subclass could
2598  * <code>include Kernel</code> to obtain +puts+, +exit+, etc. A custom
2599  * Kernel-like module could be created and included or delegation can be used
2600  * via #method_missing:
2601  *
2602  * class MyObjectSystem < BasicObject
2603  * DELEGATE = [:puts, :p]
2604  *
2605  * def method_missing(name, *args, &block)
2606  * super unless DELEGATE.include? name
2607  * ::Kernel.send(name, *args, &block)
2608  * end
2609  *
2610  * def respond_to_missing?(name, include_private = false)
2611  * DELGATE.include?(name) or super
2612  * end
2613  * end
2614  *
2615  * Access to classes and modules from the Ruby standard library can be
2616  * obtained in a BasicObject subclass by referencing the desired constant
2617  * from the root like <code>::File</code> or <code>::Enumerator</code>.
2618  * Like #method_missing, #const_missing can be used to delegate constant
2619  * lookup to +Object+:
2620  *
2621  * class MyObjectSystem < BasicObject
2622  * def self.const_missing(name)
2623  * ::Object.const_get(name)
2624  * end
2625  * end
2626  */
2627 
2628 /* Document-class: Object
2629  *
2630  * Object is the root of Ruby's class hierarchy. Its methods are available
2631  * to all classes unless explicitly overridden.
2632  *
2633  * Object mixes in the Kernel module, making the built-in kernel functions
2634  * globally accessible. Although the instance methods of Object are defined
2635  * by the Kernel module, we have chosen to document them here for clarity.
2636  *
2637  * In the descriptions of Object's methods, the parameter <i>symbol</i> refers
2638  * to a symbol, which is either a quoted string or a Symbol (such as
2639  * <code>:name</code>).
2640  */
2641 
2642 void
2644 {
2645  int i;
2646 
2648 
2649 #if 0
2650  // teach RDoc about these classes
2651  rb_cBasicObject = rb_define_class("BasicObject", Qnil);
2653  rb_cModule = rb_define_class("Module", rb_cObject);
2654  rb_cClass = rb_define_class("Class", rb_cModule);
2655 #endif
2656 
2657 #undef rb_intern
2658 #define rb_intern(str) rb_intern_const(str)
2659 
2666 
2667  rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1);
2668  rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1);
2669  rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
2670 
2671  rb_mKernel = rb_define_module("Kernel");
2676  rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
2677  rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
2678  rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
2679 
2680  rb_define_method(rb_mKernel, "nil?", rb_false, 0);
2681  rb_define_method(rb_mKernel, "===", rb_equal, 1);
2687 
2689  rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
2692  rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
2693  rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
2694  rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1);
2695 
2697  rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
2698  rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
2699  rb_define_method(rb_mKernel, "untrust", rb_obj_untrust, 0);
2700  rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0);
2702  rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
2704 
2706  rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
2707  rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */
2708  rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
2709  rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
2710  rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */
2711  rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */
2712  rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
2713  rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
2714  rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
2715  rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
2716  rb_define_private_method(rb_mKernel, "remove_instance_variable",
2717  rb_obj_remove_instance_variable, 1); /* in variable.c */
2718 
2719  rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
2723 
2724  rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */
2725  rb_define_global_function("format", rb_f_sprintf, -1); /* in sprintf.c */
2726 
2727  rb_define_global_function("Integer", rb_f_integer, -1);
2729 
2730  rb_define_global_function("String", rb_f_string, 1);
2732 
2733  rb_cNilClass = rb_define_class("NilClass", rb_cObject);
2734  rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
2735  rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
2736  rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
2737  rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
2738  rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
2742 
2743  rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
2746  /*
2747  * An alias of +nil+
2748  */
2749  rb_define_global_const("NIL", Qnil);
2750 
2751  rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
2759  rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
2761  rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
2762  rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
2763  rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */
2764  rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
2765 
2770 
2772  rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
2773  rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
2774  rb_define_method(rb_cModule, "public_instance_methods",
2775  rb_class_public_instance_methods, -1); /* in class.c */
2776  rb_define_method(rb_cModule, "protected_instance_methods",
2777  rb_class_protected_instance_methods, -1); /* in class.c */
2778  rb_define_method(rb_cModule, "private_instance_methods",
2779  rb_class_private_instance_methods, -1); /* in class.c */
2780 
2781  rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
2782  rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
2783  rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
2784  rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
2785  rb_define_private_method(rb_cModule, "remove_const",
2786  rb_mod_remove_const, 1); /* in variable.c */
2787  rb_define_method(rb_cModule, "const_missing",
2788  rb_mod_const_missing, 1); /* in variable.c */
2789  rb_define_method(rb_cModule, "class_variables",
2790  rb_mod_class_variables, 0); /* in variable.c */
2791  rb_define_method(rb_cModule, "remove_class_variable",
2792  rb_mod_remove_cvar, 1); /* in variable.c */
2793  rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
2794  rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
2795  rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
2796  rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1);
2797  rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1);
2798 
2799  rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
2801  rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
2802  rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
2804  rb_undef_method(rb_cClass, "extend_object");
2805  rb_undef_method(rb_cClass, "append_features");
2806 
2807  rb_cData = rb_define_class("Data", rb_cObject);
2809 
2810  rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
2817  /*
2818  * An alias of +true+
2819  */
2820  rb_define_global_const("TRUE", Qtrue);
2821 
2822  rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
2829  /*
2830  * An alias of +false+
2831  */
2832  rb_define_global_const("FALSE", Qfalse);
2833 
2834  id_eq = rb_intern("==");
2835  id_eql = rb_intern("eql?");
2836  id_match = rb_intern("=~");
2837  id_inspect = rb_intern("inspect");
2838  id_init_copy = rb_intern("initialize_copy");
2839  id_init_clone = rb_intern("initialize_clone");
2840  id_init_dup = rb_intern("initialize_dup");
2841 
2842  for (i=0; conv_method_names[i].method; i++) {
2844  }
2845 }
void rb_define_global_const(const char *, VALUE)
Definition: variable.c:1937
#define RSTRING_LEN(string)
Definition: generator.h:45
static long NUM2LONG(VALUE x)
Definition: ruby.h:510
VALUE rb_check_to_float(VALUE val)
Definition: object.c:2409
VALUE rb_cvar_get(VALUE, ID)
Definition: variable.c:2071
#define T_OBJECT
Definition: ruby.h:413
static VALUE rb_obj_ivar_defined(VALUE obj, VALUE iv)
Definition: object.c:1943
static VALUE rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
Definition: object.c:1759
static VALUE rb_mod_cmp(VALUE mod, VALUE arg)
Definition: object.c:1454
#define FL_EXIVAR
Definition: ruby.h:927
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Definition: class.c:762
#define FALSE
Definition: nkf.h:185
void rb_check_inheritable(VALUE super)
Ensures a class can be derived from super.
Definition: class.c:94
VALUE rb_mod_name(VALUE)
Definition: variable.c:180
VALUE rb_obj_id(VALUE obj)
Definition: gc.c:3248
int i
Definition: win32ole.c:776
#define RCLASS_CONST_TBL(c)
Definition: internal.h:36
#define T_FIXNUM
Definition: ruby.h:425
Definition: st.h:77
VALUE rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1062
VALUE rb_inspect(VALUE obj)
Definition: object.c:372
static VALUE rb_convert_to_integer(VALUE val, int base)
Definition: object.c:2148
#define NUM2INT(x)
Definition: ruby.h:536
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:345
double rb_cstr_to_dbl(const char *p, int badcheck)
Definition: object.c:2240
double rb_str_to_dbl(VALUE str, int badcheck)
Definition: object.c:2324
VALUE rb_f_sprintf(int, const VALUE *)
Definition: sprintf.c:433
#define DBL_DIG
Definition: numeric.c:58
VALUE rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:976
#define FL_TAINT
Definition: ruby.h:925
#define CLASS_OF(v)
Definition: ruby.h:376
#define T_MODULE
Definition: ruby.h:416
#define FIXNUM_MAX
Definition: ruby.h:222
static ID id_eql
Definition: object.c:36
#define Qtrue
Definition: ruby.h:366
#define RFLOAT_VALUE(val)
Definition: generator.h:32
VALUE rb_mod_class_variables(VALUE)
Definition: variable.c:2165
st_index_t rb_hash_end(st_index_t)
static VALUE rb_mod_ge(VALUE mod, VALUE arg)
Definition: object.c:1411
static VALUE rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
Definition: object.c:1996
VALUE rb_equal(VALUE obj1, VALUE obj2)
Definition: object.c:49
VALUE rb_mod_ancestors(VALUE mod)
Definition: class.c:792
const int id
Definition: nkf.c:209
static VALUE rb_mod_lt(VALUE mod, VALUE arg)
Definition: object.c:1391
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1222
#define FIXNUM_MIN
Definition: ruby.h:223
VALUE rb_eTypeError
Definition: error.c:467
VALUE rb_obj_tap(VALUE obj)
Definition: object.c:564
static VALUE rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
Definition: object.c:1703
VALUE rb_obj_taint(VALUE obj)
Definition: object.c:791
void rb_cvar_set(VALUE, ID, VALUE)
Definition: variable.c:2038
VALUE rb_singleton_class_clone(VALUE obj)
Definition: class.c:225
static void init_copy(VALUE dest, VALUE obj)
Definition: object.c:206
VALUE rb_convert_type(VALUE val, int type, const char *tname, const char *method)
Definition: object.c:2079
static VALUE false_and(VALUE obj, VALUE obj2)
Definition: object.c:1133
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
Definition: class.c:178
static VALUE nil_inspect(VALUE obj)
Definition: object.c:1017
VALUE rb_obj_trust(VALUE obj)
Definition: object.c:862
static VALUE rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
Definition: object.c:1733
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:638
static ID id_match
Definition: object.c:36
static VALUE rb_obj_ivar_get(VALUE obj, VALUE iv)
Definition: object.c:1883
#define RSTRING_PTR(string)
Definition: generator.h:42
void Init_class_hierarchy(void)
Definition: class.c:371
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1574
double rb_num2dbl(VALUE val)
Definition: object.c:2419
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1026
static struct conv_method_tbl conv_method_names[]
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4057
static VALUE rb_obj_cmp(VALUE obj1, VALUE obj2)
Definition: object.c:1241
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
int rb_const_defined(VALUE, ID)
Definition: variable.c:1847
VALUE rb_cClass
Definition: object.c:29
VALUE rb_ary_new3(long n,...)
Definition: array.c:347
VALUE rb_eSecurityError
Definition: error.c:476
VALUE rb_cModule
Definition: object.c:28
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:663
VALUE rb_to_float(VALUE val)
Definition: object.c:2395
static VALUE rb_obj_match(VALUE obj1, VALUE obj2)
Definition: object.c:1212
static VALUE rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
Definition: object.c:1784
VALUE rb_Integer(VALUE val)
Definition: object.c:2193
#define T_ARRAY
Definition: ruby.h:420
st_data_t st_index_t
Definition: st.h:63
double rb_big2dbl(VALUE x)
Definition: bignum.c:1391
#define st_lookup
Definition: regint.h:149
#define ROBJECT_NUMIV(o)
Definition: ruby.h:614
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1372
#define ISDIGIT(c)
VALUE rb_to_int(VALUE val)
Definition: object.c:2142
#define FIXNUM_P(f)
Definition: ruby.h:338
VALUE rb_obj_untaint(VALUE obj)
Definition: object.c:810
static VALUE nil_to_i(VALUE obj)
Definition: object.c:959
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1228
VALUE rb_mod_module_exec(int, VALUE *, VALUE)
Definition: vm_eval.c:1436
VALUE rb_ivar_defined(VALUE, ID)
Definition: variable.c:1112
static VALUE rb_obj_not_match(VALUE obj1, VALUE obj2)
Definition: object.c:1226
static VALUE rb_obj_dummy(void)
Definition: object.c:761
VALUE rb_mod_remove_cvar(VALUE, VALUE)
Definition: variable.c:2196
#define OBJ_TAINTED(x)
Definition: ruby.h:963
void rb_ivar_foreach(VALUE, int(*)(ANYARGS), st_data_t)
Definition: variable.c:1177
const char * rb_obj_classname(VALUE)
Definition: variable.c:318
#define OutOfRange()
void rb_name_error(ID id, const char *fmt,...)
Definition: error.c:793
static VALUE rb_f_array(VALUE obj, VALUE arg)
Definition: object.c:2493
Win32OLEIDispatch * p
Definition: win32ole.c:778
static VALUE rb_mod_cvar_get(VALUE obj, VALUE iv)
Definition: object.c:1968
#define FL_SINGLETON
Definition: ruby.h:921
#define strtod(s, e)
Definition: util.h:76
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1316
int rb_is_const_id(ID id)
Definition: ripper.c:16403
VALUE rb_obj_class(VALUE obj)
Definition: object.c:177
int rb_is_instance_id(ID id)
Definition: ripper.c:16415
VALUE rb_obj_dup(VALUE obj)
Definition: object.c:315
#define FL_UNTRUSTED
Definition: ruby.h:926
static VALUE true_or(VALUE obj, VALUE obj2)
Definition: object.c:1077
VALUE rb_obj_not(VALUE obj)
Definition: object.c:134
#define FL_TEST(x, f)
Definition: ruby.h:956
static VALUE false_or(VALUE obj, VALUE obj2)
Definition: object.c:1149
static ID id_inspect
Definition: object.c:36
VALUE rb_cvar_defined(VALUE, ID)
Definition: variable.c:2098
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition: class.c:443
static VALUE rb_to_integer(VALUE val, const char *method)
Definition: object.c:2112
#define ROBJECT_IVPTR(o)
Definition: ruby.h:618
VALUE rb_class_name(VALUE)
Definition: variable.c:305
VALUE rb_dbl2big(double d)
Definition: bignum.c:1315
VALUE rb_class_new_instance(int argc, VALUE *argv, VALUE klass)
Definition: object.c:1639
#define ALLOC_N(type, n)
Definition: ruby.h:1034
int rb_block_given_p(void)
Definition: eval.c:604
static VALUE rb_class_allocate_instance(VALUE klass)
Definition: object.c:1619
void rb_gc_copy_finalizer(VALUE dest, VALUE obj)
Definition: gc.c:2968
static VALUE nil_to_f(VALUE obj)
Definition: object.c:974
static VALUE rb_true(VALUE obj)
Definition: object.c:1181
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
Definition: bignum.c:765
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:558
#define T_NIL
Definition: ruby.h:412
VALUE rb_str_cat2(VALUE, const char *)
Definition: string.c:1908
VALUE rb_obj_as_string(VALUE)
Definition: string.c:854
VALUE rb_mod_attr(int argc, VALUE *argv, VALUE klass)
Definition: object.c:1714
static VALUE rb_mod_freeze(VALUE mod)
Definition: object.c:1318
static int inspect_i(ID id, VALUE value, VALUE str)
Definition: object.c:378
VALUE rb_check_to_integer(VALUE val, const char *method)
Definition: object.c:2128
VALUE rb_mod_private_constant(int argc, VALUE *argv, VALUE obj)
Definition: variable.c:1982
VALUE rb_iv_get(VALUE, const char *)
Definition: variable.c:2220
#define NIL_P(v)
Definition: ruby.h:374
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:469
VALUE rb_class_get_superclass(VALUE klass)
Definition: object.c:1687
static VALUE rb_obj_singleton_class(VALUE obj)
Definition: object.c:200
#define RCLASS_IV_TBL(c)
Definition: internal.h:35
VALUE rb_obj_freeze(VALUE obj)
Definition: object.c:902
static VALUE rb_class_initialize(int argc, VALUE *argv, VALUE klass)
Definition: object.c:1556
#define NEWOBJ(obj, type)
Definition: ruby.h:580
#define OBJ_FROZEN(x)
Definition: ruby.h:969
#define T_FLOAT
Definition: ruby.h:417
#define OBJ_UNTRUST(x)
Definition: ruby.h:966
#define TYPE(x)
Definition: ruby.h:441
int argc
Definition: ruby.c:120
#define Qfalse
Definition: ruby.h:365
VALUE rb_Float(VALUE val)
Definition: object.c:2352
static VALUE false_xor(VALUE obj, VALUE obj2)
Definition: object.c:1168
#define T_BIGNUM
Definition: ruby.h:423
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1053
VALUE rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1047
#define ID_ALLOCATOR
Definition: intern.h:54
arg
Definition: ripper.y:1287
#define OBJ_FREEZE(x)
Definition: ruby.h:970
VALUE rb_mod_constants(int, VALUE *, VALUE)
Definition: variable.c:1790
#define ALLOCV_END(v)
Definition: ruby.h:1050
VALUE rb_obj_equal(VALUE obj1, VALUE obj2)
Definition: object.c:95
VALUE rb_cFalseClass
Definition: object.c:34
static VALUE rb_f_float(VALUE obj, VALUE arg)
Definition: object.c:2389
int rb_obj_basic_to_s_p(VALUE obj)
Definition: class.c:1404
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:1635
#define FL_FINALIZE
Definition: ruby.h:924
#define rb_intern(str)
static VALUE rb_mod_eqq(VALUE mod, VALUE arg)
Definition: object.c:1335
static VALUE rb_mod_initialize(VALUE module)
Definition: object.c:1517
VALUE rb_yield(VALUE)
Definition: vm_eval.c:781
#define RCLASS_M_TBL(c)
Definition: internal.h:37
int errno
#define TRUE
Definition: nkf.h:186
VALUE rb_obj_untrusted(VALUE obj)
Definition: object.c:828
#define T_DATA
Definition: ruby.h:428
VALUE rb_check_funcall(VALUE, ID, int, VALUE *)
Definition: vm_eval.c:312
static ID id_init_clone
Definition: object.c:37
#define OBJ_UNTRUSTED(x)
Definition: ruby.h:965
VALUE rb_obj_hash(VALUE obj)
Definition: object.c:112
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1203
static VALUE true_to_s(VALUE obj)
Definition: object.c:1040
VALUE rb_class_superclass(VALUE klass)
Definition: object.c:1669
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1416
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1038
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:3913
unsigned long ID
Definition: ruby.h:89
#define Qnil
Definition: ruby.h:367
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:1885
VALUE rb_mod_public_constant(int argc, VALUE *argv, VALUE obj)
Definition: variable.c:1996
int type
Definition: tcltklib.c:107
static ID id_init_dup
Definition: object.c:37
VALUE rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1077
#define BUILTIN_TYPE(x)
Definition: ruby.h:438
#define OBJ_TAINT(x)
Definition: ruby.h:964
unsigned long VALUE
Definition: ruby.h:88
static VALUE nil_to_s(VALUE obj)
Definition: object.c:987
static VALUE result
Definition: nkf.c:40
#define RBASIC(obj)
Definition: ruby.h:904
RUBY_EXTERN VALUE rb_cInteger
Definition: ruby.h:1261
#define bad(x)
Definition: _sdbm.c:129
VALUE rb_make_metaclass(VALUE obj, VALUE unused)
Definition: class.c:400
register unsigned int len
Definition: name2ctype.h:22210
VALUE rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
Definition: object.c:2094
VALUE rb_obj_untrust(VALUE obj)
Definition: object.c:843
VALUE rb_String(VALUE val)
Definition: object.c:2441
RUBY_EXTERN VALUE rb_cNumeric
Definition: ruby.h:1268
VALUE rb_obj_remove_instance_variable(VALUE, VALUE)
Definition: variable.c:1299
VALUE rb_str_dup(VALUE)
Definition: string.c:905
void Init_Object(void)
Initializes the world of objects and classes.
Definition: object.c:2643
long st_data_t
Definition: syck.h:69
VALUE rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:938
VALUE rb_cTrueClass
Definition: object.c:33
void xfree(void *)
VALUE rb_class_real(VALUE cl)
Definition: object.c:154
#define FL_UNSET(x, f)
Definition: ruby.h:960
VALUE rb_cNilClass
Definition: object.c:32
#define ROBJECT(obj)
Definition: ruby.h:905
void rb_free_const_table(st_table *tbl)
Definition: gc.c:1728
static VALUE rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
Definition: object.c:1914
static VALUE true_xor(VALUE obj, VALUE obj2)
Definition: object.c:1093
#define recur(fmt)
VALUE rb_obj_alloc(VALUE klass)
Definition: object.c:1601
VALUE rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:953
VALUE rb_usascii_str_new2(const char *)
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:1853
const char * method
Definition: object.c:2033
VALUE rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:991
#define INT2FIX(i)
Definition: ruby.h:225
#define RCLASS_SUPER(c)
Definition: classext.h:16
VALUE rb_module_new(void)
Definition: class.c:566
static VALUE convert_type(VALUE val, const char *tname, const char *method, int raise)
Definition: object.c:2049
VALUE rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1116
#define st_init_numtable
Definition: regint.h:142
#define ROBJECT_EMBED_LEN_MAX
Definition: ruby.h:601
static VALUE true_and(VALUE obj, VALUE obj2)
Definition: object.c:1055
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:472
static VALUE rb_f_string(VALUE obj, VALUE arg)
Definition: object.c:2463
static VALUE rb_obj_inspect(VALUE obj)
Definition: object.c:434
static VALUE nil_to_a(VALUE obj)
Definition: object.c:1004
VALUE rb_check_string_type(VALUE)
Definition: string.c:1450
static int rb_special_const_p(VALUE obj)
Definition: ruby.h:1375
#define LONG2FIX(i)
Definition: ruby.h:226
VALUE rb_obj_init_dup_clone(VALUE obj, VALUE orig)
Definition: object.c:343
#define ALLOCV(v, n)
Definition: ruby.h:1047
#define RTEST(v)
Definition: ruby.h:373
#define T_STRING
Definition: ruby.h:418
VALUE rb_mod_remove_const(VALUE, VALUE)
Definition: variable.c:1675
#define OBJ_INFECT(x, s)
Definition: ruby.h:967
VALUE rb_obj_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1016
v
Definition: win32ole.c:790
VALUE rb_mod_const_missing(VALUE, VALUE)
Definition: variable.c:1403
static VALUE inspect_obj(VALUE obj, VALUE str, int recur)
Definition: object.c:404
VALUE rb_obj_is_kind_of(VALUE obj, VALUE c)
Definition: object.c:525
void rb_obj_infect(VALUE obj1, VALUE obj2)
Definition: object.c:873
VALUE rb_obj_init_copy(VALUE obj, VALUE orig)
Definition: object.c:331
VALUE rb_Array(VALUE val)
Definition: object.c:2469
static VALUE rb_mod_cvar_defined(VALUE obj, VALUE iv)
Definition: object.c:2022
static VALUE rb_class_s_alloc(VALUE klass)
Definition: object.c:1485
static VALUE rb_mod_to_s(VALUE klass)
Definition: object.c:1286
#define st_insert
Definition: regint.h:148
#define FL_MARK
Definition: ruby.h:922
VALUE rb_ary_new2(long capa)
Definition: array.c:332
int rb_is_class_id(ID id)
Definition: ripper.c:16409
#define T_CLASS
Definition: ruby.h:414
static VALUE rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
Definition: object.c:1816
#define rb_safe_level()
Definition: tcltklib.c:90
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:1641
#define ROBJECT_EMBED
Definition: ruby.h:613
static ID id_eq
Definition: object.c:36
VALUE rb_obj_tainted(VALUE obj)
Definition: object.c:774
static VALUE rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
Definition: object.c:1843
const char * name
Definition: nkf.c:208
const char * rb_id2name(ID id)
Definition: ripper.c:16362
VALUE rb_obj_not_equal(VALUE obj1, VALUE obj2)
Definition: object.c:147
VALUE rb_mod_included_modules(VALUE mod)
Definition: class.c:729
#define FL_FREEZE
Definition: ruby.h:928
Definition: ruby.h:602
static VALUE rb_false(VALUE obj)
Definition: object.c:1196
#define st_free_table
Definition: regint.h:152
static VALUE rb_f_integer(int argc, VALUE *argv, VALUE obj)
Definition: object.c:2221
void rb_warning(const char *fmt,...)
Definition: error.c:212
void rb_secure(int)
Definition: safe.c:79
#define rb_check_frozen(obj)
Definition: intern.h:242
VALUE rb_mKernel
Definition: object.c:26
int rb_eql(VALUE obj1, VALUE obj2)
Definition: object.c:60
static VALUE false_to_s(VALUE obj)
Definition: object.c:1117
void rb_copy_generic_ivar(VALUE, VALUE)
Definition: variable.c:959
#define SPECIAL_CONST_P(x)
Definition: ruby.h:953
VALUE rb_define_module(const char *name)
Definition: class.c:587
VALUE rb_usascii_str_new(const char *, long)
Definition: string.c:416
static VALUE rb_mod_gt(VALUE mod, VALUE arg)
Definition: object.c:1436
VALUE rb_obj_instance_variables(VALUE)
Definition: variable.c:1267
#define mod(x, y)
static st_table * immediate_frozen_tbl
Definition: object.c:878
VALUE rb_obj_clone(VALUE obj)
Definition: object.c:279
#define st_copy
Definition: regint.h:154
#define NULL
Definition: _sdbm.c:107
#define FIX2LONG(x)
Definition: ruby.h:336
#define Qundef
Definition: ruby.h:368
#define T_ICLASS
Definition: ruby.h:415
#define OBJSETUP(obj, c, t)
Definition: ruby.h:581
void rb_obj_call_init(VALUE obj, int argc, VALUE *argv)
Definition: eval.c:881
VALUE rb_class_inherited_p(VALUE mod, VALUE arg)
Definition: object.c:1353
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1210
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2047
VALUE rb_cObject
Definition: object.c:27
void rb_invalid_str(const char *str, const char *type)
Definition: error.c:1011
ID rb_to_id(VALUE)
Definition: string.c:7740
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition: class.c:75
VALUE rb_obj_frozen_p(VALUE obj)
Definition: object.c:931
VALUE rb_eArgError
Definition: error.c:468
static ID cmp
Definition: compar.c:16
#define T_MASK
Definition: md5.c:131
st_index_t rb_hash_start(st_index_t)
Definition: random.c:1330
VALUE rb_any_to_s(VALUE obj)
Definition: object.c:360
char ** argv
Definition: ruby.c:121
#define DBL2NUM(dbl)
Definition: ruby.h:647
#define ISSPACE(c)
Definition: ruby.h:1453
#define StringValue(v)
Definition: ruby.h:466
VALUE rb_obj_is_instance_of(VALUE obj, VALUE c)
Definition: object.c:480
VALUE rb_cBasicObject
Definition: object.c:25
static VALUE rb_module_s_alloc(VALUE klass)
Definition: object.c:1476
VALUE rb_cData
Definition: object.c:30
static ID id_init_copy
Definition: object.c:37