Skip to content

Simplify block default passing in cattr and mattr - #325

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

trans merged 1 commit into
mainfrom
claude/github-connector-capabilities-4qcre6

Conversation

@trans

@trans trans commented Sep 26, 2026

Copy link
Copy Markdown
Member

This is the follow-up promised in #317.

Summary

cattr and mattr split the writers into writers - readers and writers & readers so the default block wouldn't run a second time for an attribute that already has a reader. That split isn't needed. *_reader and *_writer only set the default when class_variable_defined? is false, so once the reader has set it, the writer skips the block anyway.

Both methods now just pass &block through:

cattr_reader(*readers, &block)
cattr_writer(*writers, &block)

The change is in lib/core/facets/module/mattr.rb: 2 lines added, 13 removed.

Testing

I checked cattr and mattr with a Ruby 3.3 script. The #317 behavior is unchanged:

  • An existing class variable is kept, not replaced by the default.
  • In cattr(:y, :y=) { … } / mattr(:y, :y=) { … } the block runs exactly once.

I didn't run the repo's ruby-test suite locally; CI runs it.

🤖 Generated with Claude Code

https://claude.ai/code/session_01E2Njj4j1r9twkVcXsMySLW


Generated by Claude Code

The class_variable_defined? guard in the reader and writer already
keeps the default block from running twice, so cattr and mattr can
pass the block straight through instead of splitting writers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E2Njj4j1r9twkVcXsMySLW

trans commented Sep 27, 2026

Copy link
Copy Markdown
Member Author

CI note: the only red check here is Ruby jruby on ubuntu, and it isn't caused by this PR.

  • It fails in test/standard/test_uri.rb:45 with private method 'hash_to_query' called for module URI (from lib/standard/facets/uri.rb:28). This PR only touches lib/core/facets/module/mattr.rb.
  • The JRuby job is marked experimental (continue-on-error), so the workflow run itself passes. All the CRuby versions and TruffleRuby pass.
  • The likely cause is alias_module_function :hash_to_query, :query, which is called inside a bare module_function section in uri/query.rb. JRuby 10.1 appears to leave the singleton alias private. I don't have a verified fix yet, so this should be a separate change rather than part of this PR.

Generated by Claude Code

@trans
trans merged commit 92394b6 into main Sep 27, 2026
12 of 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