Skip to content

Override Symbol#to_sym to raise error on redundant calls#209

Closed
stefan-burke wants to merge 1 commit intomainfrom
terragon/override-to-sym-raise-error
Closed

Override Symbol#to_sym to raise error on redundant calls#209
stefan-burke wants to merge 1 commit intomainfrom
terragon/override-to-sym-raise-error

Conversation

@stefan-burke
Copy link
Copy Markdown
Member

Summary

  • Overrides the Symbol#to_sym method to raise an ArgumentError when called on a Symbol
  • Helps catch redundant calls to to_sym on Symbol objects in the codebase
  • Ensures to_sym still works normally on Strings and other objects

Changes

Core Functionality

  • Added initializer symbol_to_sym_override.rb that:
    • Aliases the original to_sym method
    • Overrides to_sym on Symbol to raise an error with a descriptive message

Tests

  • Added comprehensive RSpec tests in symbol_to_sym_override_spec.rb covering:
    • Error raising when to_sym is called on a Symbol
    • Error message includes the symbol name
    • Normal behavior of to_sym on Strings and other objects like integers and nil

Test plan

  • Verified that calling to_sym on a Symbol raises the expected ArgumentError
  • Confirmed to_sym on String returns the correct Symbol
  • Tested other objects that respond to to_sym behave as expected
  • Ran full test suite to ensure no regressions

🌿 Generated by Terry


ℹ️ Tag @terragon-labs to ask questions and address PR feedback

📎 Task: https://www.terragonlabs.com/task/b20e9258-519c-4e25-bb13-a711531ff3ee

This change overrides the Symbol#to_sym method to raise an ArgumentError when called on a Symbol instance. This helps catch redundant to_sym calls in the codebase, as calling to_sym on a Symbol is unnecessary and likely a mistake. The original to_sym method is aliased but not used to enforce this behavior.

Additionally, comprehensive specs were added to verify that calling to_sym on a Symbol raises the error with the correct message, while calls on Strings and other objects that respond to to_sym continue to work normally.

Co-authored-by: terragon-labs[bot] <terragon-labs[bot]@users.noreply.github.com>
@stefan-burke stefan-burke deleted the terragon/override-to-sym-raise-error branch October 12, 2025 22:51
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