Skip to content

Make Kernel#try! the lenient one; keep #try strict - #332

Merged
trans merged 2 commits into
mainfrom
claude/github-connector-capabilities-4qcre6
Sep 27, 2026
Merged

trans merged 2 commits into
mainfrom
claude/github-connector-capabilities-4qcre6

Conversation

@trans

@trans trans commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

In Ruby the bang marks the more dangerous version of a method. Quietly turning a missing method into nil hides bugs, so that's now what try! does. try is strict: it behaves exactly like a normal call or &..

Call try before try now try! before try! now
o.try(:name) "bob" "bob" "bob" "bob"
o.try(:nmae) (missing) raises raises raises nil
o.try(:secret) (private) calls it raises raises nil
nil.try(:name) nil nil nil nil
nil.try.name (Functor) nil nil nil nil

Why this differs from Rails

Since Rails 4.0, ActiveSupport has it the other way round: try returns nil for a missing method and try! raises. That puts the bug-hiding behavior on the plain method, so user.try(:nmae) quietly returns nil.

Facets keeps try strict, 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 for try).

try also used __send__, so it could call private methods, which neither a normal call nor &. can do. It now uses public_send (1bb2395).

Breaking changes

  • try! used to raise NoMethodError for a missing method; it now returns nil.
  • try no longer calls private methods; it raises NoMethodError for them.

Nothing inside Facets calls either with a private or missing method. Both changes are listed under Breaking in HISTORY.md.

Testing

  • New tests: try raises for missing and private methods, and try! returns nil for missing, private and nil receivers. Against the old code, the try! 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

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
@trans
trans merged commit 292a9a8 into main Sep 27, 2026
14 checks passed
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