Skip to content

Instantly share code, notes, and snippets.

@seanhandley
Created August 12, 2014 15:43
Show Gist options
  • Save seanhandley/3bf9011ac0428bda4b49 to your computer and use it in GitHub Desktop.
Save seanhandley/3bf9011ac0428bda4b49 to your computer and use it in GitHub Desktop.
Ruby Hash Constants are Anything But Constant
irb(main):001:0> I_AM_A_CONSTANT = "I never change"
=> "I never change"
irb(main):002:0> i_am_a_variable = I_AM_A_CONSTANT
=> "I never change"
irb(main):003:0> i_am_a_variable = "I can change"
=> "I can change"
irb(main):004:0> I_AM_A_CONSTANT
=> "I never change"
irb(main):005:0> I_AM_A_CONSTANT_HASH = {contents: "I never change"}
=> {:contents=>"I never change"}
irb(main):006:0> variable_hash = I_AM_A_CONSTANT_HASH
=> {:contents=>"I never change"}
irb(main):007:0> variable_hash[:contents] = "zomg!"
=> "zomg!"
irb(main):008:0> I_AM_A_CONSTANT_HASH
=> {:contents=>"zomg!"}
irb(main):009:0>
@seanhandley
Copy link
Author

Thanks @caius - I'm aware of the underpinnings and the way references work and my production code is already using #dup and #freeze.

What sucks is that these things are referred to as "constants" by all documentation and examples like this, contrived as they are, serve to show why people in functional languages care so much about immutability.

I love Ruby, but I imagine the thousands of bugs introduced to production codebases by this easily misinterpreted behaviour and my mind boggles. They shouldn't be called constants, they need a different name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment