Skip to content

Instantly share code, notes, and snippets.

@bernerdschaefer
Created December 17, 2014 01:13
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 bernerdschaefer/7e8d6b5ebbf00039b15e to your computer and use it in GitHub Desktop.
Save bernerdschaefer/7e8d6b5ebbf00039b15e to your computer and use it in GitHub Desktop.
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