Repository navigation
Set evaluation result to _ local variable - #1159
HeyNonster wants to merge 1 commit into
Conversation
This PR makes a change to the thread client to set the `_` local variable to the result of the last evaluation similar to the behavior in irb[^1]. The reason `_` doesn't work even when using the `irb` console is because we leave the `irb` evaluation early if the input should be handled by the debugger[^2]. Otherwise, we would end up in IRB::Context#evaluate` and we would call `set_last_value`[^3]. There's a bit of a quirk here when using the `irb` console. I was hoping in `thread_client` to evaluate the result then set the `_` local variable and the `@last_value` ivar all in the same method, similar to how `irb` does it[^1]. However, because we're using the `irb` console we use a new `irb` `Workspace` which nils out `_` when initialized[^4]. To get around that, we set the local variable just before evaluating. [^1]:(https://github.com/ruby/irb/blob/d43c3d764ae439706aa1b26a3ec299cc45eaed5b/lib/irb/context.rb#L460-L465) [^2]:(https://github.com/ruby/irb/blob/d43c3d764ae439706aa1b26a3ec299cc45eaed5b/lib/irb.rb#L195-L198) [^3]:(https://github.com/ruby/irb/blob/d43c3d764ae439706aa1b26a3ec299cc45eaed5b/lib/irb/context.rb#L550-L558) [^4]:(https://github.com/ruby/irb/blob/d43c3d764ae439706aa1b26a3ec299cc45eaed5b/lib/irb/workspace.rb#L81) This PR was created by Zendesk's Ruby infrastructure team: Co-authored-by: Benjamin Quorning <[email protected]> Co-authored-by: Edyta Rozczypała <[email protected]> Co-authored-by: Jury Razumau <[email protected]> Co-authored-by: Leonid Batizhevskii <[email protected]> Co-authored-by: Luis Manotas <[email protected]> Co-authored-by: Thomas Countz <[email protected]>
❌ 1/689 Tests Failed/home/runner/work/debug/debug/test/protocol/catch_raw_dap_test.rb#test_catching_any_exception_works_correctly |
|
how to handle |
@ko1 We can try to make this not clobber an existing As an example: irb
irb(main):001> _ = "foo"
=> "foo"
irb(main):002> "bar"
=> "bar"
irb(main):003> _
=> "bar"
irb(main):004> def _;"baz";end
=> :_
irb(main):006> "bar"
=> "bar"
irb(main):007> _
=> "bar" |
|
Hi! I filed the original issue for this, #1069 (#1069), along with the IRB side of it, ruby/irb#891 (ruby/irb#891), where On @ko1's question about an existing def m; _ = 1; b = binding.dup; b.local_variable_set(:_, 99); p _; end; m
# => 99
I opened #1188, which builds on this PR (your commit is cherry-picked with authorship and co-authors kept) and adds:
Happy to close #1188 and have those changes folded into this PR instead, whichever is easier for everyone. |
This resolves #1069.
This PR makes a change to the thread client to set the
_local variable to the result of the last evaluation similar to the behavior in irb1.The reason
_doesn't work even when using theirbconsole is because we leave theirbevaluation early if the input should be handled by the debugger2. Otherwise, we would end up inIRB::Context#evaluateand we would callset_last_value3.There's a bit of a quirk here when using the
irbconsole. I was hoping inthread_clientto evaluate the result then set the_local variable and the@last_valueivar all in the same method, similar to howirbdoes it1. However, because we're using theirbconsole we use a newirbWorkspacewhich nils out_when initialized4. To get around that, we set the local variable just before evaluating.Footnotes
(https://github.com/ruby/irb/blob/d43c3d764ae439706aa1b26a3ec299cc45eaed5b/lib/irb/context.rb#L460-L465) ↩ ↩2
(https://github.com/ruby/irb/blob/d43c3d764ae439706aa1b26a3ec299cc45eaed5b/lib/irb.rb#L195-L198) ↩
(https://github.com/ruby/irb/blob/d43c3d764ae439706aa1b26a3ec299cc45eaed5b/lib/irb/context.rb#L550-L558) ↩
(https://github.com/ruby/irb/blob/d43c3d764ae439706aa1b26a3ec299cc45eaed5b/lib/irb/workspace.rb#L81) ↩