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

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 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.



38
# File 'lib/attic/mixins.rb', line 38

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.



29
30
31
32
33
34
35
# File 'lib/attic/mixins.rb', line 29

def metaclass
  if !self.metaclass?
    raise NoMetaClass, self
  else
    class << self; self; end; 
  end
end

#metaclass?Boolean

Returns:

  • (Boolean)


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

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

#metameta_def(name, &blk) ⇒ Object



63
64
65
# File 'lib/attic/mixins.rb', line 63

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

#metameta_eval(&blk) ⇒ Object



61
# File 'lib/attic/mixins.rb', line 61

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


59
# File 'lib/attic/mixins.rb', line 59

def metametaclass; self.metaclass.metaclass; end

#nometaclass?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/attic/mixins.rb', line 13

def nometaclass?
  NOMETACLASS.member?(self)
end