Skip to content

Instantly share code, notes, and snippets.

@balinterdi
Created January 2, 2012 11:29
Show Gist options
  • Save balinterdi/1550350 to your computer and use it in GitHub Desktop.
Save balinterdi/1550350 to your computer and use it in GitHub Desktop.
require 'forwardable'
class X
def foo
puts "foo!"
end
alias_method :bar, :foo
end
class Y < X
def foo
puts "foo! in Y"
end
end
# alias_method makes a copy of the aliased method, so:
Y.new.foo # => foo! in Y
Y.new.bar # => foo!
puts "#" * 20
class A
extend Forwardable
def foo
puts "foo!"
end
def_delegator :self, :foo, :bar
end
class B < A
def foo
puts "foo! in B"
end
end
# def_delegator just forwards calls to the last argument to first.second
B.new.foo # foo! in B
B.new.bar # foo! in B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment