1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Enable show-source on procs/lambdas

This commit is contained in:
Reginald Tan 2012-07-16 01:45:39 -04:00
parent be98279f7b
commit 98e32b1e11
2 changed files with 45 additions and 0 deletions

View file

@ -32,11 +32,19 @@ class Pry
mod = target_self.is_a?(Module) ? target_self : target_self.class
end
def proc?(name)
target.eval(name).is_a? Proc
rescue TypeError, NameError
false
end
def process(name)
if module?(name)
code_or_doc = process_module
elsif method?
code_or_doc = process_method
elsif proc?(name)
code_or_doc = process_proc
else
command_error("method or module for '#{name}' could not be found or derived", false)
end
@ -305,6 +313,24 @@ class Pry
result
end
def process_proc
name = args.first
target_proc = target.eval(name)
file_name, line = target_proc.source_location
source = Pry::Code.from_file(file_name).expression_at(line)
code = Pry::Code.new(source).with_line_numbers(use_line_numbers?).to_s
#code = Pry::Code.new(target_proc.source, line).with_line_numbers(use_line_numbers?).to_s
result = ""
result << "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{line}:\n"
result << "#{Pry::Helpers::Text.bold('Number of lines:')} #{code.lines.count}\n\n"
result << code
result << "\n"
end
def use_line_numbers?
opts.present?(:b) || opts.present?(:l)
end

View file

@ -194,6 +194,25 @@ if !mri18_and_no_real_source_location?
end
end
describe "on procs/lambdas" do
if RUBY_VERSION =~ /1.9/
it "should output source defined inside pry" do
redirect_pry_io(InputTester.new("hello = proc { puts 'hello world!' }", "show-source hello"), @str_output) do
TOPLEVEL_BINDING.pry
end
@str_output.string.should =~ /proc { puts 'hello world!' }/
end
end
it "should output source" do
hello = proc { puts 'hello world!' }
mock_pry(binding, "show-source hello").should =~ /proc { puts 'hello world!' }/
end
end
describe "on modules" do
before do
class ShowSourceTestClass