Skip to content

Instantly share code, notes, and snippets.

@jordan-brough
Last active July 16, 2020 20:39
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 jordan-brough/f24a5071ef8a8e910a23 to your computer and use it in GitHub Desktop.
Save jordan-brough/f24a5071ef8a8e910a23 to your computer and use it in GitHub Desktop.
module PutsMethods
module_function
# prints out methods in a pretty way. e.g.:
# >> PutsMethods "asdf"
# String
# <=>, ==, ===, eql?, hash, casecmp, ...
# Comparable
# >, >=, <, <=, between?
# ...
def puts(object, type: 'public')
method_names = object.public_send("#{type}_methods")
method_objects = method_names.map do |name|
# Use Kernel.method in case the object has its own method named "method"
Kernel.method(:method).unbind.bind(object).call(name)
end
method_mapping = method_objects.group_by(&:owner)
rows = method_mapping.map do |owner, methods|
"#{owner}\n " + methods.map(&:name).join(", ")
end
$stdout.puts rows.join("\n\n")
end
end
module Kernel
module_function
def PutsMethods(object, type: 'public')
PutsMethods.puts(object, type: type)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment