Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/attic/mixins.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.
49 50 51 |
# File 'lib/attic/mixins.rb', line 49 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.
43 44 45 |
# File 'lib/attic/mixins.rb', line 43 def name, &blk { define_method name, &blk } end |
#meta_eval(&blk) ⇒ Object
Execute a block &blk within the metaclass of the current object.
38 |
# File 'lib/attic/mixins.rb', line 38 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.
29 30 31 32 33 34 35 |
# File 'lib/attic/mixins.rb', line 29 def if !self. raise NoMetaClass, self else class << self; self; end; end end |
#metaclass? ⇒ Boolean
17 18 19 |
# File 'lib/attic/mixins.rb', line 17 def !NOMETACLASS.member?(self.class) end |
#metameta_def(name, &blk) ⇒ Object
63 64 65 |
# File 'lib/attic/mixins.rb', line 63 def name, &blk { define_method name, &blk } end |
#metameta_eval(&blk) ⇒ Object
61 |
# File 'lib/attic/mixins.rb', line 61 def &blk; .instance_eval &blk; end |
#metametaclass ⇒ Object
A convenient method for getting the metaclass of the metaclass i.e.
self..
59 |
# File 'lib/attic/mixins.rb', line 59 def ; self..; end |
#nometaclass? ⇒ Boolean
13 14 15 |
# File 'lib/attic/mixins.rb', line 13 def NOMETACLASS.member?(self) end |