Skip to content

Simplify block default passing in cattr and mattr - #325

Open
trans wants to merge 1 commit into
mainfrom
claude/github-connector-capabilities-4qcre6
Open

trans wants to merge 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

This branch has not been deployed

No deployments
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