Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@voxik
Copy link
Contributor

@voxik voxik commented Dec 2, 2025

source_location method in Ruby 4.0+ provides more details [1], such as end of method and offsets of stars and ends. This following test failure, which is fixed by this patch, nicely exhibits the difference:

$ rspec -rspec_helper spec/commands/edit_spec.rb:794 Run options: include {locations: {"./spec/commands/edit_spec.rb" => [794]}}

Randomized with seed 8752
F

Failures:

  1) edit --method flag should edit method context
     Failure/Error: expect([file, line]).to eq BinkyWink.instance_method(:m2).source_location

       expected: ["/builddir/build/BUILD/rubygem-pry-0.15.0-build/pry-0.15.0/usr/share/gems/gems/pry-0.15.0/spec/commands/edit_spec.rb", 783, 8, 786, 11]
            got: ["/builddir/build/BUILD/rubygem-pry-0.15.0-build/pry-0.15.0/usr/share/gems/gems/pry-0.15.0/spec/commands/edit_spec.rb", 783]

       (compared using ==)
     # ./spec/commands/edit_spec.rb:796:in 'block (4 levels) in <top (required)>'
     # ./lib/pry/editor.rb:63:in 'Pry::Editor#build_editor_invocation_string'
     # ./lib/pry/editor.rb:47:in 'Pry::Editor#invoke_editor'
     # ./lib/pry/commands/edit.rb:142:in 'Pry::Command::Edit#file_edit'
     # ./lib/pry/commands/edit.rb:62:in 'Pry::Command::Edit#process'
     # ./lib/pry/class_command.rb:82:in 'Pry::ClassCommand#call'
     # ./lib/pry/command.rb:495:in 'Pry::Command#call_with_hooks'
     # ./lib/pry/command.rb:431:in 'block in Pry::Command#call_safely'
     # ./lib/pry/command.rb:439:in 'Pry::Command#use_unpatched_symbol'
     # ./lib/pry/command.rb:430:in 'Pry::Command#call_safely'
     # ./lib/pry/command.rb:404:in 'Pry::Command#process_line'
     # ./lib/pry/command_set.rb:354:in 'Pry::CommandSet#process_line'
     # ./lib/pry/pry_instance.rb:326:in 'Pry#process_command'
     # ./lib/pry/testable/pry_tester.rb:62:in 'Pry::Testable::PryTester#process_command'
     # ./spec/commands/edit_spec.rb:801:in 'block (3 levels) in <top (required)>'

Finished in 0.01534 seconds (files took 0.11079 seconds to load) 1 example, 1 failure

Failed examples:

rspec ./spec/commands/edit_spec.rb:794 # edit --method flag should edit method context

Randomized with seed 8752

`source_location` method in Ruby 4.0+ provides more details [[1]], such
as end of method and offsets of stars and ends. This following test
failure, which is fixed by this patch, nicely exhibits the difference:

~~~
$ rspec -rspec_helper spec/commands/edit_spec.rb:794
Run options: include {locations: {"./spec/commands/edit_spec.rb" => [794]}}

Randomized with seed 8752
F

Failures:

  1) edit --method flag should edit method context
     Failure/Error: expect([file, line]).to eq BinkyWink.instance_method(:m2).source_location

       expected: ["/builddir/build/BUILD/rubygem-pry-0.15.0-build/pry-0.15.0/usr/share/gems/gems/pry-0.15.0/spec/commands/edit_spec.rb", 783, 8, 786, 11]
            got: ["/builddir/build/BUILD/rubygem-pry-0.15.0-build/pry-0.15.0/usr/share/gems/gems/pry-0.15.0/spec/commands/edit_spec.rb", 783]

       (compared using ==)
     # ./spec/commands/edit_spec.rb:796:in 'block (4 levels) in <top (required)>'
     # ./lib/pry/editor.rb:63:in 'Pry::Editor#build_editor_invocation_string'
     # ./lib/pry/editor.rb:47:in 'Pry::Editor#invoke_editor'
     # ./lib/pry/commands/edit.rb:142:in 'Pry::Command::Edit#file_edit'
     # ./lib/pry/commands/edit.rb:62:in 'Pry::Command::Edit#process'
     # ./lib/pry/class_command.rb:82:in 'Pry::ClassCommand#call'
     # ./lib/pry/command.rb:495:in 'Pry::Command#call_with_hooks'
     # ./lib/pry/command.rb:431:in 'block in Pry::Command#call_safely'
     # ./lib/pry/command.rb:439:in 'Pry::Command#use_unpatched_symbol'
     # ./lib/pry/command.rb:430:in 'Pry::Command#call_safely'
     # ./lib/pry/command.rb:404:in 'Pry::Command#process_line'
     # ./lib/pry/command_set.rb:354:in 'Pry::CommandSet#process_line'
     # ./lib/pry/pry_instance.rb:326:in 'Pry#process_command'
     # ./lib/pry/testable/pry_tester.rb:62:in 'Pry::Testable::PryTester#process_command'
     # ./spec/commands/edit_spec.rb:801:in 'block (3 levels) in <top (required)>'

Finished in 0.01534 seconds (files took 0.11079 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/commands/edit_spec.rb:794 # edit --method flag should edit method context

Randomized with seed 8752
~~~

[1]: https://bugs.ruby-lang.org/issues/6012
@voxik
Copy link
Contributor Author

voxik commented Dec 2, 2025

Let me provide a few additional notes about the proposed changes.

  • I have decided to keep more verbose comment in lib/pry/wrapped_module/candidate.rb mainly because this patch is slightly changing the implementation. It should allow to fetch bigger chunk of the source code without any further checks. Of course the code could be changed just to last_method_source_location[1] and keep going. Maybe it would be worth of doing this in stages, because actually also the method description would likely deserve some update.

  • I have decided to update the test cases in spec/class_command_spec.rb / spec/commands/edit_spec.rb, but maybe they should be special cased to Ruby 4.0? Maybe the whole Pry should somehow (conditionally) try to benefit from the additional information provided by the source_location? But that would require bigger effort which is out of scope for me.

@voxik
Copy link
Contributor Author

voxik commented Dec 2, 2025

There is #2349 which tries to address the head test failures I believe.

@andrehjr
Copy link
Member

andrehjr commented Dec 2, 2025

  • I have decided to keep more verbose comment in lib/pry/wrapped_module/candidate.rb mainly because this patch is slightly changing the implementation. It should allow to fetch bigger chunk of the source code without any further checks. Of course the code could be changed just to last_method_source_location[1] and keep going. Maybe it would be worth of doing this in stages, because actually also the method description would likely deserve some update.

πŸ‘

  • I have decided to update the test cases in spec/class_command_spec.rb / spec/commands/edit_spec.rb, but maybe they should be special cased to Ruby 4.0? Maybe the whole Pry should somehow (conditionally) try to benefit from the additional information provided by the source_location? But that would require bigger effort which is out of scope for me.

It would be interesting to add further information for Ruby 4.0+, but for now, it's ok.

My plan it to make sure ruby-head is green before the next Ruby release. But any help is appreciated :)

Thanks for the PR and the context!

@andrehjr andrehjr merged commit 7a551ae into pry:master Dec 2, 2025
18 of 22 checks passed
@andrehjr andrehjr mentioned this pull request Dec 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants