From ed52bfdb741831128e82a12f033a2a0ac158bf51 Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 10 May 2013 02:46:22 +0200 Subject: [PATCH] reload-code (with no args) should reload 'current file' This should fix #920. Also added a guard for !code_object.source_file, this existed on 0.9.12 but for some reason was not on master. --- lib/pry/commands/reload_code.rb | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/pry/commands/reload_code.rb b/lib/pry/commands/reload_code.rb index 6b8767f3..215ef0b5 100644 --- a/lib/pry/commands/reload_code.rb +++ b/lib/pry/commands/reload_code.rb @@ -15,25 +15,44 @@ class Pry BANNER def process - code_object = Pry::CodeObject.lookup(obj_name, _pry_) - check_for_reloadability(code_object) - reload_code_object(code_object) + if obj_name.empty? + # if no parameters were provided then try to reload the + # current file (i.e target.eval("__FILE__")) + reload_current_file + else + code_object = Pry::CodeObject.lookup(obj_name, _pry_) + reload_code_object(code_object) + end end private + def current_file + File.expand_path target.eval("__FILE__") + end + + def reload_current_file + if !File.exists?(current_file) + raise CommandError, "Current file: #{current_file} cannot be found on disk!" + end + + load current_file + output.puts "The current file: #{current_file} was reloaded!" + end + def reload_code_object(code_object) + check_for_reloadability(code_object) load code_object.source_file output.puts "#{obj_name} was reloaded!" end def obj_name - @obj_name ||= args.empty? ? "self" : args.join(" ") + @obj_name ||= args.join(" ") end def check_for_reloadability(code_object) - if !code_object + if !code_object || !code_object.source_file raise CommandError, "Cannot locate #{obj_name}!" elsif !File.exists?(code_object.source_file) raise CommandError, "Cannot reload #{obj_name} as it has no associated file on disk. File found was: #{code_object.source_file}"