summaryrefslogtreecommitdiff
path: root/lib/method_source
diff options
context:
space:
mode:
authorJohn Mair <jrmair@gmail.com>2011-06-01 23:52:49 +1200
committerJohn Mair <jrmair@gmail.com>2011-06-01 23:52:49 +1200
commit51e58be34c4f893efcc74b54412917dec2b657b6 (patch)
treeb722d91b890d1db35b75c3d924f0635e8503a967 /lib/method_source
parentc111c0e64b646bcd75ce2ed1bd58be3eb9bd63cf (diff)
downloadmethod_source-51e58be34c4f893efcc74b54412917dec2b657b6.tar.gz
added source_location to procs for rbx, all tests passing on rbx
Diffstat (limited to 'lib/method_source')
-rw-r--r--lib/method_source/source_location.rb24
1 files changed, 18 insertions, 6 deletions
diff --git a/lib/method_source/source_location.rb b/lib/method_source/source_location.rb
index cc7026b..4e078f7 100644
--- a/lib/method_source/source_location.rb
+++ b/lib/method_source/source_location.rb
@@ -4,14 +4,14 @@ module MethodSource
def trace_func(event, file, line, id, binding, classname)
return unless event == 'call'
- set_trace_func nil
-
+ set_trace_func nil
+
@file, @line = file, line
raise :found
end
private :trace_func
-
+
# Return the source location of a method for Ruby 1.8.
# @return [Array] A two element array. First element is the
# file, second element is the line in the file where the
@@ -19,16 +19,28 @@ module MethodSource
def source_location
if @file.nil?
args =[*(1..(arity<-1 ? -arity-1 : arity ))]
-
+
set_trace_func method(:trace_func).to_proc
call(*args) rescue nil
- set_trace_func nil
+ set_trace_func nil
@file = File.expand_path(@file) if @file && File.exist?(File.expand_path(@file))
end
return [@file, @line] if File.exist?(@file.to_s)
end
end
+ # Rubinius only
+ module ProcExtensions
+
+ # Return the source location for a Proc (Rubinius only)
+ # @return [Array] A two element array. First element is the
+ # file, second element is the line in the file where the
+ # proc definition is found.
+ def source_location
+ [block.file.to_s, block.line]
+ end
+ end
+
module UnboundMethodExtensions
# Return the source location of an instance method for Ruby 1.8.
@@ -57,7 +69,7 @@ module MethodSource
when klass == NilClass
return nil.method(name).source_location
end
-
+
begin
klass.allocate.method(name).source_location
rescue TypeError