Last active
August 29, 2015 14:01
-
-
Save vinhnglx/b77a21a8cad5d5cdf990 to your computer and use it in GitHub Desktop.
Pros of missing_method()
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
class Question | |
def get_question_title | |
puts "get_question_title called" | |
end | |
def get_question_content | |
puts "get_question_content called" | |
end | |
end | |
class Answer | |
def initialize | |
@question = Question.new | |
end | |
def get_question_title | |
@question.get_question_title | |
end | |
def get_question_content | |
@question.get_question_content | |
end | |
# Methods above is duplicate with methods in class Question. | |
# It look like copy-paste the existing methods and then modifying them | |
end |
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
class Question | |
def get_question_title | |
puts "get_question_title called" | |
end | |
def get_question_content | |
puts "get_question_content called" | |
end | |
end | |
class Answer | |
def initialize | |
@question = Question.new | |
end | |
def method_missing(name, *args) | |
@question.send(name, *args) | |
end | |
# Answer now is Dynamic Proxy, it forward each method inner Question class. If you add any method into Question | |
# then Answer will work without any changes. | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment