Created
March 9, 2013 12:44
-
-
Save mjtko/5124059 to your computer and use it in GitHub Desktop.
Generate a warning if `#respond_to?` or `#respond_to_missing?` might be doing something unexpected after upgrading to Ruby 2.0.0.
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
if RUBY_VERSION >= '2.0.0' | |
class Object | |
alias :_respond_to? :respond_to? | |
def respond_to?(symbol, include_all = nil) | |
_respond_to?(symbol, include_all || false).tap do |b| | |
if b == false && include_all.nil? && _respond_to?(symbol, true) | |
warn "WARNING! #{self.class.name} responds to #{symbol}, but it's protected or private (from: #{caller[2].inspect})" | |
end | |
end | |
end | |
alias :_respond_to_missing? :respond_to_missing? | |
def respond_to_missing?(symbol, include_all = nil) | |
_respond_to_missing?(symbol, include_all || false).tap do |b| | |
if b == false && include_all.nil? && _respond_to_missing?(symbol, true) | |
warn "WARNING! #{self.class.name} responds to missing #{symbol}, but it's protected or private (from: #{caller[2].inspect})" | |
end | |
end | |
end | |
end | |
end | |