diff options
author | Stan Lo <[email protected]> | 2024-02-13 13:33:33 +0000 |
---|---|---|
committer | git <[email protected]> | 2024-02-13 13:33:36 +0000 |
commit | ec26786b1a37350c0ff21666b028ec5e2aa6a318 (patch) | |
tree | 74898ff938d7736be88fc25ded1d1fe7f3bd1896 /lib/irb/context.rb | |
parent | b5327647c2a49c36621631a06527837e99b009e5 (diff) |
[ruby/irb] Refactor eval_path and `SourceFinder::Source`
(https://github.com/ruby/irb/pull/870)
* Assign `@eval_path` through `irb_path=` method
This simplifies the original caching logic for the `eval_path` method
and makes it easier to understand.
* Refactor SourceFinder::Source
https://github.com/ruby/irb/commit/c63e4c4035
Diffstat (limited to 'lib/irb/context.rb')
-rw-r--r-- | lib/irb/context.rb | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/lib/irb/context.rb b/lib/irb/context.rb index 814a8bd4ad..e50958978f 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -77,7 +77,7 @@ module IRB else @irb_name = IRB.conf[:IRB_NAME]+"#"+IRB.JobManager.n_jobs.to_s end - @irb_path = "(" + @irb_name + ")" + self.irb_path = "(" + @irb_name + ")" case input_method when nil @@ -121,11 +121,11 @@ module IRB when '-' @io = FileInputMethod.new($stdin) @irb_name = '-' - @irb_path = '-' + self.irb_path = '-' when String @io = FileInputMethod.new(input_method) @irb_name = File.basename(input_method) - @irb_path = input_method + self.irb_path = input_method else @io = input_method end @@ -246,9 +246,27 @@ module IRB # Can be either name from <code>IRB.conf[:IRB_NAME]</code>, or the number of # the current job set by JobManager, such as <code>irb#2</code> attr_accessor :irb_name - # Can be either the #irb_name surrounded by parenthesis, or the - # +input_method+ passed to Context.new - attr_accessor :irb_path + + # Can be one of the following: + # - the #irb_name surrounded by parenthesis + # - the +input_method+ passed to Context.new + # - the file path of the current IRB context in a binding.irb session + attr_reader :irb_path + + # Sets @irb_path to the given +path+ as well as @eval_path + # @eval_path is used for evaluating code in the context of IRB session + # It's the same as irb_path, but with the IRB name postfix + # This makes sure users can distinguish the methods defined in the IRB session + # from the methods defined in the current file's context, especially with binding.irb + def irb_path=(path) + @irb_path = path + + if File.exist?(path) + @eval_path = "#{path}(#{IRB.conf[:IRB_NAME]})" + else + @eval_path = path + end + end # Whether multiline editor mode is enabled or not. # @@ -557,7 +575,7 @@ module IRB if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty? last_proc = proc do - result = @workspace.evaluate(line, eval_path, line_no) + result = @workspace.evaluate(line, @eval_path, line_no) end IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) do |chain, item| _name, callback, arg = item @@ -568,20 +586,12 @@ module IRB end end.call else - result = @workspace.evaluate(line, eval_path, line_no) + result = @workspace.evaluate(line, @eval_path, line_no) end set_last_value(result) end - private def eval_path - # We need to use differente path to distinguish source_location of method defined in the actual file and method defined in irb session. - if !defined?(@irb_path_existence) || @irb_path_existence[0] != irb_path - @irb_path_existence = [irb_path, File.exist?(irb_path)] - end - @irb_path_existence[1] ? "#{irb_path}(#{IRB.conf[:IRB_NAME]})" : irb_path - end - def inspect_last_value # :nodoc: @inspect_method.inspect_value(@last_value) end |