Created
January 2, 2012 11:29
-
-
Save balinterdi/1550350 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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