Created
February 3, 2017 21:51
-
-
Save vnegrisolo/ccedd2cba3cfd9177e21e88c93e56c7a to your computer and use it in GitHub Desktop.
ruby-code-reader.rb
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
class Code | |
class << self | |
def print(clazz, class_method: nil, instance_method: nil) | |
method = class_method ? clazz.method(class_method) : clazz.instance_method(instance_method) | |
puts read_method(*method.source_location) | |
end | |
private | |
def read_method(path, line) | |
lines = File.readlines(path)[(line-1)..-1] | |
padding = lines.first.scan(/^(\s+)/).first.first | |
index = lines.index { |line| line.scan(/^#{padding}end?$/).first } || -1 | |
lines[0..index] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment