From 5a0f8adcd94a0598459db062f0c4dee38f308707 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Sat, 14 Apr 2012 14:12:32 -0700 Subject: [PATCH] Re-use Pry::Method.safe_send (todo, move to helper?) [Fixes #529] --- lib/pry/default_commands/ls.rb | 6 ++++-- lib/pry/method.rb | 1 + test/test_default_commands/test_ls.rb | 4 ++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/pry/default_commands/ls.rb b/lib/pry/default_commands/ls.rb index a9a912e5..cc20109d 100644 --- a/lib/pry/default_commands/ls.rb +++ b/lib/pry/default_commands/ls.rb @@ -87,8 +87,10 @@ class Pry if show_ivars klass = (Module === obj ? obj : obj.class) - output_section("instance variables", format_variables(:instance_var, grep[Object.instance_method(:instance_variables).bind(obj).call])) - output_section("class variables", format_variables(:class_var, grep[Class.instance_method(:class_variables).bind(klass).call])) + ivars = Pry::Method.safe_send(obj, :instance_variables) + kvars = Pry::Method.safe_send(klass, :class_variables) + output_section("instance variables", format_variables(:instance_var, ivars)) + output_section("class variables", format_variables(:class_var, kvars)) end if show_locals diff --git a/lib/pry/method.rb b/lib/pry/method.rb index 21290651..9ea55f65 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -188,6 +188,7 @@ class Pry def safe_send(obj, method, *args, &block) (Module === obj ? Module : Object).instance_method(method).bind(obj).call(*args, &block) end + public :safe_send # Get the singleton classes of superclasses that could define methods on # the given class object, and any modules they include. diff --git a/test/test_default_commands/test_ls.rb b/test/test_default_commands/test_ls.rb index 84753115..b2f978a4 100644 --- a/test/test_default_commands/test_ls.rb +++ b/test/test_default_commands/test_ls.rb @@ -58,6 +58,10 @@ describe "ls" do it "should still work" do mock_pry("cd Module.new{ def foobie; end }", "ls -M").should =~ /foobie/ end + + it "should work for ivars" do + mock_pry("module StigmaT1sm; def foobie; @@gharble = 456; end; end", "Object.new.tap{ |o| o.extend(StigmaT1sm) }.foobie", "cd StigmaT1sm", "ls -i").should =~ /@@gharble/ + end end describe "constants" do