Last active
June 13, 2019 14:12
-
-
Save zdennis/edcc6721146ac85da4f3295a28530748 to your computer and use it in GitHub Desktop.
aliasing methods stack level too deep issue when a module is included more than once
This file contains hidden or 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
module M | |
def self.included(base) | |
base.class_eval do | |
alias_method :user_without_name, :user | |
alias_method :user, :user_with_name | |
end | |
end | |
def user | |
puts "in user" | |
end | |
def user_with_name | |
puts "in user_with_name" | |
user_without_name || "foo" | |
end | |
end | |
class A | |
include M | |
end | |
A.new.user | |
A.class_eval { include M } | |
A.new.user # stack-level too deep |
This file contains hidden or 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
module M | |
def self.included(base) | |
base.class_eval do | |
def user | |
puts "in user" | |
end | |
def user_with_name | |
puts "in user_with_name" | |
user_without_name || "foo" | |
end | |
alias_method :user_without_name, :user | |
alias_method :user, :user_with_name | |
end | |
end | |
end | |
class A | |
include M | |
end | |
A.new.user | |
A.class_eval { include M } | |
A.new.user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment