Skip to content

Instantly share code, notes, and snippets.

@bingxie
Last active June 12, 2020 03:53
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 bingxie/8c04254af981a0db876ab6a4b052d2a7 to your computer and use it in GitHub Desktop.
Save bingxie/8c04254af981a0db876ab6a4b052d2a7 to your computer and use it in GitHub Desktop.
Quickly find ruby method and open the file
# put this code into your .pryrc and .irbrc
def source_for(object, method_sym, from_super = nil)
if object.respond_to?(method_sym, true)
method = object.method(method_sym)
elsif object.is_a?(Module)
method = object.instance_method(method_sym)
end
if from_super
location = method.super_method.source_location
else
location = method.source_location
end
`code #{location[0]}:#{location[1]}` if location # code for vscode, subl for sublime
if location
puts "#{location[0]}:#{location[1]}"
else
puts "Couldn't find the source file!"
end
rescue
nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment