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

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 meta_def name, &blk
  meta_eval { 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 meta_eval &blk; metaclass.instance_eval &blk; end

#metaclassObject

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 metaclass
  if !self.metaclass?
    raise NoMetaClass, self
  else
    class << self; self; end; 
  end
end

#metaclass?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/attic.rb', line 20

def metaclass?
  !NOMETACLASS.member?(self.class)
end

#metameta_def(name, &blk) ⇒ Object



66
67
68
# File 'lib/attic.rb', line 66

def metameta_def name, &blk
  metameta_eval { define_method name, &blk }
end

#metameta_eval(&blk) ⇒ Object



64
# File 'lib/attic.rb', line 64

def metameta_eval &blk; metametaclass.instance_eval &blk; end

#metametaclassObject

A convenient method for getting the metaclass of the metaclass i.e.

self.metaclass.metaclass


62
# File 'lib/attic.rb', line 62

def metametaclass; self.metaclass.metaclass; end

#nometaclass?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/attic.rb', line 16

def nometaclass?
  NOMETACLASS.member?(self)
end