Last active
June 12, 2020 03:53
-
-
Save bingxie/8c04254af981a0db876ab6a4b052d2a7 to your computer and use it in GitHub Desktop.
Quickly find ruby method and open the file
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
# 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