564
578
* New subclass: Baz
581
/* Document-method: method_added
584
* method_added(method_name)
586
* Invoked as a callback whenever an instance method is added to the
590
* def self.method_added(method_name)
591
* puts "Adding #{method_name.inspect}"
593
* def self.some_class_method() end
594
* def some_instance_method() end
599
* Adding :some_instance_method
603
/* Document-method: method_removed
606
* method_removed(method_name)
608
* Invoked as a callback whenever an instance method is removed from the
612
* def self.method_removed(method_name)
613
* puts "Removing #{method_name.inspect}"
615
* def self.some_class_method() end
616
* def some_instance_method() end
618
* remove_method :some_class_method
620
* remove_method :some_instance_method
625
* Removing :some_instance_method
568
630
* Document-method: singleton_method_added
1448
* Class.new(super_class=Object) -> a_class
1508
* Class.new(super_class=Object) -> a_class
1509
* Class.new(super_class=Object) { |mod| ... } -> a_class
1450
1511
* Creates a new anonymous (unnamed) class with the given superclass
1451
1512
* (or <code>Object</code> if no parameter is given). You can give a
1452
1513
* class a name by assigning the class object to a constant.
1515
* If a block is given, it is passed the class object, and the block
1516
* is evaluated in the context of this class using
1517
* <code>class_eval</code>.
1519
* fred = Class.new do
1528
* a = fred.new #=> #<#<Class:0x100381890>:0x100376b98>
1529
* a.meth1 #=> "hello"
1532
* Assign the class to a constant (name starting uppercase) if you
1533
* want to treat it like a regular class.
1666
1752
* mod.const_get(sym, inherit=true) -> obj
1668
* Returns the value of the named constant in <i>mod</i>.
1754
* Checks for a constant with the given name in <i>mod</i>
1755
* If +inherit+ is set, the lookup will also search
1756
* the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
1758
* The value of the constant is returned if a definition is found,
1759
* otherwise a +NameError+ is raised.
1670
1761
* Math.const_get(:PI) #=> 3.14159265358979
1672
* If the constant is not defined or is defined by the ancestors and
1673
* +inherit+ is false, +NameError+ will be raised.
1722
1810
* mod.const_defined?(sym, inherit=true) -> true or false
1724
* Returns <code>true</code> if a constant with the given name is
1725
* defined by <i>mod</i>, or its ancestors if +inherit+ is not false.
1812
* Checks for a constant with the given name in <i>mod</i>
1813
* If +inherit+ is set, the lookup will also search
1814
* the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
1816
* Returns whether or not a definition is found:
1727
1818
* Math.const_defined? "PI" #=> true
1728
* IO.const_defined? "SYNC" #=> true
1729
* IO.const_defined? "SYNC", false #=> false
1819
* IO.const_defined? :SYNC #=> true
1820
* IO.const_defined? :SYNC, false #=> false
2458
2553
* \ingroup class
2463
* <code>BasicObject</code> is the parent class of all classes in Ruby.
2464
* It's an explicit blank class. <code>Object</code>, the root of Ruby's
2465
* class hierarchy is a direct subclass of <code>BasicObject</code>. Its
2466
* methods are therefore available to all objects unless explicitly
2469
* <code>Object</code> mixes in the <code>Kernel</code> module, making
2470
* the built-in kernel functions globally accessible. Although the
2471
* instance methods of <code>Object</code> are defined by the
2472
* <code>Kernel</code> module, we have chosen to document them here for
2556
/* Document-class: BasicObject
2558
* BasicObject is the parent class of all classes in Ruby. It's an explicit
2561
* BasicObject can be used for creating object hierarchies independent of
2562
* Ruby's object hierarchy, proxy objects like the Delegator class, or other
2563
* uses where namespace pollution from Ruby's methods and classes must be
2566
* To avoid polluting BasicObject for other users an appropriately named
2567
* subclass of BasicObject should be created instead of directly modifying
2570
* class MyObjectSystem < BasicObject
2573
* BasicObject does not include Kernel (for methods like +puts+) and
2574
* BasicObject is outside of the namespace of the standard library so common
2575
* classes will not be found without a using a full class path.
2577
* A variety of strategies can be used to provide useful portions of the
2578
* standard library to subclasses of BasicObject. A subclass could
2579
* <code>include Kernel</code> to obtain +puts+, +exit+, etc. A custom
2580
* Kernel-like module could be created and included or delegation can be used
2581
* via #method_missing:
2583
* class MyObjectSystem < BasicObject
2584
* DELEGATE = [:puts, :p]
2586
* def method_missing(name, *args, &block)
2587
* super unless DELEGATE.include? name
2588
* ::Kernel.send(name, *args, &block)
2591
* def respond_to_missing?(name, include_private = false)
2592
* DELGATE.include?(name) or super
2596
* Access to classes and modules from the Ruby standard library can be
2597
* obtained in a BasicObject subclass by referencing the desired constant
2598
* from the root like <code>::File</code> or <code>::Enumerator</code>.
2599
* Like #method_missing, #const_missing can be used to delegate constant
2600
* lookup to +Object+:
2602
* class MyObjectSystem < BasicObject
2603
* def self.const_missing(name)
2604
* ::Object.const_get(name)
2609
/* Document-class: Object
2611
* Object is the root of Ruby's class hierarchy. Its methods are available
2612
* to all classes unless explicitly overridden.
2614
* Object mixes in the Kernel module, making the built-in kernel functions
2615
* globally accessible. Although the instance methods of Object are defined
2616
* by the Kernel module, we have chosen to document them here for clarity.
2475
2618
* In the descriptions of Object's methods, the parameter <i>symbol</i> refers
2476
* to a symbol, which is either a quoted string or a
2477
* <code>Symbol</code> (such as <code>:name</code>).
2619
* to a symbol, which is either a quoted string or a Symbol (such as
2620
* <code>:name</code>).
2481
2624
Init_Object(void)
2483
extern void Init_class_hierarchy(void);
2486
2628
Init_class_hierarchy();
2631
// teach RDoc about these classes
2632
rb_cBasicObject = rb_define_class("BasicObject", Qnil);
2633
rb_cObject = rb_define_class("Object", rb_cBasicObject);
2634
rb_cModule = rb_define_class("Module", rb_cObject);
2635
rb_cClass = rb_define_class("Class", rb_cModule);
2488
2638
#undef rb_intern
2489
2639
#define rb_intern(str) rb_intern_const(str)
2491
rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, -1);
2641
rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0);
2492
2642
rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
2493
2643
rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
2494
2644
rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
2621
2774
rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
2622
2775
rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
2623
2776
rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
2777
rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1);
2778
rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1);
2625
2780
rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
2626
2781
rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);