Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/attic.rb
Overview
Object
These methods are copied directly from _why’s metaid.rb. See: whytheluckystiff.net/articles/seeingMetaclassesClearly.html
Constant Summary collapse
- NOMETACLASS =
An Array of classes which do not have metaclasses.
[Symbol, Fixnum].freeze
Instance Method Summary collapse
-
#class_def(name, &blk) ⇒ Object
Add a class method called
name
for the current object’s class. -
#meta_def(name, &blk) ⇒ Object
Add an instance method called
name
to metaclass for the current object. -
#meta_eval(&blk) ⇒ Object
Execute a block &blk within the metaclass of the current object.
-
#metaclass ⇒ Object
A convenient method for getting the metaclass of the current object.
- #metaclass? ⇒ Boolean
- #metameta_def(name, &blk) ⇒ Object
- #metameta_eval(&blk) ⇒ Object
-
#metametaclass ⇒ Object
A convenient method for getting the metaclass of the metaclass i.e.
- #nometaclass? ⇒ Boolean
Instance Method Details
#class_def(name, &blk) ⇒ Object
Add a class method called name
for the current object’s class. This isn’t so special but it maintains consistency with meta_def.
52 53 54 |
# File 'lib/attic.rb', line 52 def class_def name, &blk class_eval { define_method name, &blk } end |
#meta_def(name, &blk) ⇒ Object
Add an instance method called name
to metaclass for the current object. This is useful because it will be available as a singleton method to all subclasses too.
46 47 48 |
# File 'lib/attic.rb', line 46 def name, &blk { define_method name, &blk } end |
#meta_eval(&blk) ⇒ Object
Execute a block &blk within the metaclass of the current object.
41 |
# File 'lib/attic.rb', line 41 def &blk; .instance_eval &blk; end |
#metaclass ⇒ Object
A convenient method for getting the metaclass of the current object. i.e.
class << self; self; end;
NOTE: Some Ruby class do not have meta classes (see: NOMETACLASS). For these classes, this method returns the class itself. That means the instance variables will stored in the class itself.
32 33 34 35 36 37 38 |
# File 'lib/attic.rb', line 32 def if !self. raise NoMetaClass, self else class << self; self; end; end end |
#metaclass? ⇒ Boolean
20 21 22 |
# File 'lib/attic.rb', line 20 def !NOMETACLASS.member?(self.class) end |
#metameta_def(name, &blk) ⇒ Object
66 67 68 |
# File 'lib/attic.rb', line 66 def name, &blk { define_method name, &blk } end |
#metameta_eval(&blk) ⇒ Object
64 |
# File 'lib/attic.rb', line 64 def &blk; .instance_eval &blk; end |
#metametaclass ⇒ Object
A convenient method for getting the metaclass of the metaclass i.e.
self..
62 |
# File 'lib/attic.rb', line 62 def ; self..; end |