Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# My take on Mike's source_for method.
# (see
# (1) I named it 'src' rather than source_for (ok, I'm a lazy typer).
# (2) The edit function was broken out as a separate function.
# (3) The edit function is for emacs
# (4) If the method is not defined on the object, and the object
# is a class, then see if it is an instance method on the class.
# The fourth point allows my to say:
# src(Person, :update)
# or src(, :update)
def edit(file, line)
`emacsclient -n +#{line} #{file}`
def src(object, method)
if object.respond_to?(method)
meth = object.method(method)
elsif object.is_a?(Class)
meth = object.instance_method(method)
location = meth.source_location
edit(*location) if location
rescue NameError => ex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment