Skip to content

Instantly share code, notes, and snippets.

@mklbtz
Last active April 16, 2017 22:46
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 mklbtz/ad1020cb7303f9c018350d6e32ff0828 to your computer and use it in GitHub Desktop.
Save mklbtz/ad1020cb7303f9c018350d6e32ff0828 to your computer and use it in GitHub Desktop.
A method for opening the `source_location` of a method or proc in your preferred editor
# A method for opening the `source_location`
# of a method or proc in your preferred editor.
# Depends on the presence of $EDITOR in your environment.
module OpenSource
def open_source
return false unless
loc = source_location and
loc.first != '(irb)'
path = loc.join(':') # append the line number to the file path.
system("eval \"$EDITOR #{path}\"")
end
end
UnboundMethod.include OpenSource
Method.include OpenSource
Proc.include OpenSource
# Example Usage
# opens /path/to/lib/ruby/2.3.0/cmath.rb to line 281 in Sublime
# works for Gems, your own sources, etc.
Math.method(:cos).open_source
#=> true
# methods defined in (irb) will not be opened
Proc.method(:open_source).open_source
#=> false
# methods defined in Ruby's native code will not be opened
Object.method(:to_s).open_source
#=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment