Skip to content

Instantly share code, notes, and snippets.

@vinhnglx
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinhnglx/b77a21a8cad5d5cdf990 to your computer and use it in GitHub Desktop.
Save vinhnglx/b77a21a8cad5d5cdf990 to your computer and use it in GitHub Desktop.
Pros of missing_method()
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
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