Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow 'edit self' and 'reload-code self'

This didn't work due to a quirk in Pry::CodeObject.lookup() which purposely ignores 'expressions'
as they can be methods, which are dangerous to lookup. We now make an exception when the expression is 'self'

This code needs to be refactored as we have ridiculosu method names like  `def variable_or_constant_or_self?`
and we have methods like this in a couple of places. Refactor pls.
  • Loading branch information...
commit f1654d86037a61f2de6a6ec31bc2a1e500f050b9 1 parent 6254b8a
John Mair banister authored
10 lib/pry/code_object.rb
View
@@ -65,9 +65,9 @@ def binding_lookup
end
end
- # lookup variables and constants that are not modules
+ # lookup variables and constants and `self` that are not modules
def default_lookup
- if variable_or_constant?(str)
+ if variable_or_constant_or_self?(str)
obj = target.eval(str)
# restrict to only objects we KNOW for sure support the full API
@@ -107,12 +107,12 @@ def sourcable_object?(obj)
[::Proc, ::Method, ::UnboundMethod].any? { |o| obj.is_a?(o) }
end
- # Whether `str` represents a variable (or constant) when looked up
+ # Whether `str` represents `self` or a variable (or constant) when looked up
# in the context of the `target` binding. This is used to
# distinguish it from methods or expressions.
# @param [String] str The string to lookup
- def variable_or_constant?(str)
- str !~ /\S#\S/ && target.eval("defined? #{str} ") =~ /variable|constant/
+ def variable_or_constant_or_self?(str)
+ str.strip == "self" || str !~ /\S#\S/ && target.eval("defined? #{str} ") =~ /variable|constant/
end
def target_self
2  lib/pry/commands/reload_code.rb
View
@@ -19,7 +19,7 @@ def reload_code_object(code_object)
end
def obj_name
- @obj_name ||= args.empty? ? nil : args.join(" ")
+ @obj_name ||= args.empty? ? "self" : args.join(" ")
end
def check_for_reloadability(code_object)
10 lib/pry/wrapped_module.rb
View
@@ -27,7 +27,7 @@ class WrappedModule
# @example
# Pry::WrappedModule.from_str("Pry::Code")
def self.from_str(mod_name, target=TOPLEVEL_BINDING)
- if variable_or_constant_from_binding_is_a_module?(target, mod_name)
+ if variable_or_constant_or_self_from_binding_is_a_module?(target, mod_name)
Pry::WrappedModule.new(target.eval(mod_name))
else
nil
@@ -45,7 +45,9 @@ class << self
# @param [Binding] target
# @param [String] mod_name The string to lookup in the binding.
# @return [Boolean] Whether the string represents a variable or constant.
- def variable_or_constant?(target, mod_name)
+ def variable_or_constant_or_self?(target, mod_name)
+ return true if mod_name.strip == "self"
+
kind = target.eval("defined?(#{mod_name})")
kind == "constant" || kind =~ /variable/
end
@@ -55,8 +57,8 @@ def variable_or_constant?(target, mod_name)
# @param [Binding] target
# @param [String] mod_name The string to look up in the binding.
# @return [Boolean] Whether the string represents a module.
- def variable_or_constant_from_binding_is_a_module?(target, mod_name)
- if variable_or_constant?(target, mod_name)
+ def variable_or_constant_or_self_from_binding_is_a_module?(target, mod_name)
+ if variable_or_constant_or_self?(target, mod_name)
target.eval(mod_name).is_a?(Module)
else
nil
Please sign in to comment.
Something went wrong with that request. Please try again.