Skip to content

Instantly share code, notes, and snippets.

@alexdean
Created May 10, 2019 20:32
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 alexdean/1bab1d52ab0ec4f4ad23d6574b8f18be to your computer and use it in GitHub Desktop.
Save alexdean/1bab1d52ab0ec4f4ad23d6574b8f18be to your computer and use it in GitHub Desktop.
video = Video.first
save_method = video.method(:save)

save_method.class
# => Method

save_method.source_location 
# => an Array with [file, line]

save_method.source 
# => String

save_method.source.display
# => String, with line breaks evaluated

save_method.super_method
# => another Method, or nil

# print a method's source & location
# recurse into super definitions if they exist
def show_method(method)
  if method.super_method
    show_method(method.super_method)
  end

  puts method.source_location.inspect
  puts method.source.display
  puts
end

video = Video.first
show_method(video.method(:save))
$ bundle show activerecord
$ bundle open activerecord
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment