Created
December 17, 2014 01:13
-
-
Save bernerdschaefer/7e8d6b5ebbf00039b15e to your computer and use it in GitHub Desktop.
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 << self | |
alias mess_with_bernerd define_method | |
end | |
eval(<<-EOS) | |
mess_with_bernerd(:foo) do | |
puts "but can you handle string eval???" | |
end | |
mess_with_bernerd(:bar) do | |
puts "with multiple methods?!?!?!" | |
end | |
EOS | |
def find_method(file, line) | |
f = File.readlines(__FILE__)[0...(__LINE__ - 2)].join | |
f = f.gsub("eval(<<-EOS)", "eval <<-EOS, binding, __FILE__, __LINE__") | |
eval f, binding, "<eval>" | |
methods. | |
map { |m| method(m) }. | |
reject { |m| !m.source_location }. | |
select { |m| m.source_location[0] == "<eval>" }. | |
select { |m| m.source_location[1] <= line }. | |
sort_by { |m| m.source_location[1] }. | |
last. | |
name | |
end | |
if $0 == __FILE__ | |
puts "Don't mess with Bernerd" | |
puts find_method(__FILE__, 7) | |
puts find_method(__FILE__, 11) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment