Learned a useful one in metaprogramming scenarios with Ruby. Sometimes, we want to see the all instance methods on a class, including its inherited ones from the parent. But at the same time, when we call # instance_methods on a class, that will return all of the ones inherited from Object like # nil?, # class, # freeze, etc. too.
Somewhat useful -the false parameter can be optionally passed in, but it misses methods inherited from parent classes which could be key when debugging. For example:
require 'uri'
irb(main):001> URI::HTTP::instance_methods
# => Returns 100+ methods including Object methods
irb(main):002> URI::HTTP::instance_methods(false)
=> [:origin, :request_uri, :authority]
We can actually return the difference by subtracting Object methods using the Array set complement operator (-).
irb(main):003> URI::HTTP.instance_methods - Object.instance_methods
=>
[:authority,
:origin,
:request_uri,
:normalize,
:component_ary,
:coerce,
:scheme,
:find_proxy,
:port,
:component,
:path,
:+,
:-,
:default_port,
:scheme=,
:userinfo=,
:hostname=,
:port=,
:path,
:query=,
:opaque=,
:set_scheme,
:set_userinfo,
:set_host,
:set_port,
:set_path,
:set_opaque,
:password,
:host,
:user=,
:password=,
:set_password,
:set_user,
:decoded_user,
:user,
:decoded_password,
:set_registry,
:host=,
:registry=,
:hierarchical?,
:opaque,
:userinfo,
:registry,
:query,
:fragment,
:relative?,
:absolute?,
:absolute,
:route_from,
:fragment=,
:parser,
:merge!,
:merge,
:select,
:route_to,
:normalize!,
:hostname]