Created
June 22, 2016 21:39
-
-
Save jah2488/2cde6c7312e67697cbabd13d02860aa6 to your computer and use it in GitHub Desktop.
This file contains 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
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