Skip to content

Add recommendation to avoid variable reflection methods#973

Open
andyw8 wants to merge 1 commit intorubocop:masterfrom
andyw8:avoid-instance-variable-accessors
Open

Add recommendation to avoid variable reflection methods#973
andyw8 wants to merge 1 commit intorubocop:masterfrom
andyw8:avoid-instance-variable-accessors

Conversation

@andyw8
Copy link
Contributor

@andyw8 andyw8 commented Feb 1, 2026

Summary

Adds a new guideline recommending against the use of Ruby's variable reflection methods (instance_variable_get, instance_variable_set, and related methods) as they break encapsulation.

  • Lists all relevant instance and class variable reflection methods
  • Includes code examples showing the bad practice vs. proper accessor usage
  • Notes acceptable use cases (framework/library code, testing utilities, serialization)

Context

I'm planning to add a new RuboCop cop to enforce this guideline. My motivation is that I often see AI agents use these methods instead of fixing the design.

🤖 Generated with Claude Code

Adds guidance to avoid instance_variable_get, instance_variable_set,
and related reflection methods as they break encapsulation. Includes
note about acceptable use cases in framework/library code.

Planning to add a new RuboCop cop to enforce this guideline.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@andyw8 andyw8 marked this pull request as ready for review February 1, 2026 00:34
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.

1 participant