Skip to content

Instantly share code, notes, and snippets.

@jah2488
Created June 22, 2016 21: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 jah2488/2cde6c7312e67697cbabd13d02860aa6 to your computer and use it in GitHub Desktop.
Save jah2488/2cde6c7312e67697cbabd13d02860aa6 to your computer and use it in GitHub Desktop.
def inspect_class(klass)
klass.new.methods.sort.each do |meth|
just = klass.new.methods.sort.max_by(&:length).length
hjust = just / 2
klass_meth = klass.new.method(meth)
label = "#{klass}.new.#{meth.to_s.ljust(just)}"
location = "is defined in #{klass_meth.owner.to_s.ljust(hjust)}"
response = [label, location]
next if meth =~ /(pry|binding)/
begin
return_value = klass_meth.call
return_klass = return_value.class
if return_klass.to_s =~ /^[AEIOU]/
response << "returns an #{return_klass}"
else
response << "returns a #{return_klass}"
end
rescue StandardError => e
response << "throws #{e.class}"
if e.is_a?(ArgumentError)
response << "because #{meth.to_sym.inspect.ljust(hjust)} requires #{klass_meth.arity} argument#{"s" if klass_meth.arity > 1}"
end
ensure
puts response.join(", ")
end
end
end
inspect_class(Array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment