Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Enable show-source on procs/lambdas

  • Loading branch information...
commit 98e32b1e11f7568ccfc823cc818f96e0645cf089 1 parent be98279
redgetan redgetan authored
26 lib/pry/default_commands/introspection.rb
View
@@ -32,11 +32,19 @@ def get_module_from_internal_binding
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 @@ def all_modules
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
19 test/test_default_commands/test_show_source.rb
View
@@ -194,6 +194,25 @@ def @o.foo(*bars); end
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
Please sign in to comment.
Something went wrong with that request. Please try again.