Repository navigation
Make Kernel#try! the lenient one; keep #try strict - #332
Merged
Merged
Conversation
In Ruby the bang marks the more dangerous version of a method. Quietly turning a missing method into nil hides bugs (user.try!(:nmae) => nil), so that is now what #try! does: it calls the method if the receiver responds to it and returns nil otherwise. #try is unchanged and stays strict, raising NoMethodError for a missing method, like a normal call or &., while still returning nil for a nil receiver. This is deliberately the opposite of ActiveSupport, which since Rails 4.0 has #try lenient and #try! strict. The docs now say so, in capitals, and no longer claim ActiveSupport compatibility. Adds tests for #try raising on a missing method and for #try! on missing, private and nil receivers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E2Njj4j1r9twkVcXsMySLW
Strict #try should behave exactly like a normal method call or &., neither of which can reach a private method. It used __send__, so try(:secret) called private methods. It now uses public_send and raises NoMethodError for them. #try! is unaffected and still returns nil. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E2Njj4j1r9twkVcXsMySLW
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In Ruby the bang marks the more dangerous version of a method. Quietly turning a missing method into
nilhides bugs, so that's now whattry!does.tryis strict: it behaves exactly like a normal call or&..trybeforetrynowtry!beforetry!nowo.try(:name)"bob""bob""bob""bob"o.try(:nmae)(missing)nilo.try(:secret)(private)nilnil.try(:name)nilnilnilnilnil.try.name(Functor)nilnilnilnilWhy this differs from Rails
Since Rails 4.0, ActiveSupport has it the other way round:
tryreturnsnilfor a missing method andtry!raises. That puts the bug-hiding behavior on the plain method, souser.try(:nmae)quietly returnsnil.Facets keeps
trystrict, like a normal call or&., and puts the lenient behavior behind the bang. Both methods' docs now explain this, IN CAPITALS, and no longer claim ActiveSupport compatibility (which was never accurate fortry).tryalso used__send__, so it could call private methods, which neither a normal call nor&.can do. It now usespublic_send(1bb2395).Breaking changes
try!used to raiseNoMethodErrorfor a missing method; it now returnsnil.tryno longer calls private methods; it raisesNoMethodErrorfor them.Nothing inside Facets calls either with a private or missing method. Both changes are listed under Breaking in
HISTORY.md.Testing
tryraises for missing and private methods, andtry!returnsnilfor missing, private andnilreceivers. Against the old code, thetry!cases fail.rake test: 1,263 tests pass on CRuby 3.3 and on JRuby 10.1.2 (4 pending).rake qed: 157 demos pass.🤖 Generated with Claude Code
https://claude.ai/code/session_01E2Njj4j1r9twkVcXsMySLW