Skip to content

Instantly share code, notes, and snippets.

@mjtko
Created March 9, 2013 12:44
Show Gist options
  • Save mjtko/5124059 to your computer and use it in GitHub Desktop.
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.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment