Module: RDF::Util::Aliasing::LateBound

Included in:
Countable, Durable, Enumerable, Mutable, Readable, Reader, Writable, Writer
Defined in:
lib/rdf/util/aliasing.rb

Overview

Helpers for late-bound instance method aliasing.

Anything that extends this module will obtain an alias_method class method that creates late-bound instance method aliases instead of the default early-bound aliases created by Ruby's Module#alias_method.

This is useful because RDF.rb mixins typically alias a number of overridable methods. For example, RDF::Enumerable#count has the aliases #size and #length. Normally if implementing classes were to override the default method, the aliased methods would still be bound to the mixin's original reference implementation rather than the new overridden method. Mixing in this module into the implementing class fixes this problem.

Examples:

Using late-bound aliasing in a module

module MyModule
  extend RDF::Util::Aliasing::LateBound
end

Using late-bound aliasing in a class

class MyClass
  extend RDF::Util::Aliasing::LateBound
end

See Also:

Since:

Instance Method Summary (collapse)

Instance Method Details

- alias_method(new_name, old_name)

This method returns an undefined value.

Makes new_name a late-bound alias of the method old_name.

Examples:

Aliasing the #count method to #size and #length

alias_method :size,   :count
alias_method :length, :count

Parameters:

  • (Symbol, #to_sym) new_name
  • (Symbol, #to_sym) old_name

See Also:

Since:

  • 0.2.0



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rdf/util/aliasing.rb', line 42

def alias_method(new_name, old_name)
  new_name, old_name = new_name.to_sym, old_name.to_sym

  class_eval("def \#{new_name}(*args, &block)\n\#{old_name}(*args, &block)\nend\n")

  # NOTE: the following eval-less (and hence slightly less evil)
  # implementation only works on Ruby 1.8.7+ due to the |&block|
  # syntax that was introduced in 1.9 and then backported to 1.8.7;
  # it is a syntax error in earlier versions of Ruby:
  #self.__send__(:define_method, new_name) do |*args, &block|
  #  __send__(old_name, *args, &block)
  #end

  return self
end