3 ms·
What a timely post. I had a similar issue that I was tracing last week that did end up being my Ruby code...and it turns out I was modifying a constant like in
by busterarm 10y ago
What a timely post.
I had a similar issue that I was tracing last week that did end up being my Ruby code...and it turns out I was modifying a constant like in the example.
What a fun read! I've been learning more about Ruby's GC since 2.1 and this got me looking even deeper -- definitely picked up a couple of new tricks/tools from this. Thank you be9!
- mwpmaybe 10y agoFreeze your constants!
- busterarm 10y agoI used to do this with strings (now you don't have to), but this is great advice!
- mwpmaybe 10y agoYeah, it's one of those rare Ruby gotchas. If you assign some kind of a data structure to a constant, you'll get a warning if you try to reassign the constant, but the data structure itself will silently mutable. To disallow this behavior, simply freeze the data structure. If you have nested data structures, you must freeze deeply. ENUM = [0, 1, 2].freeze LOOKUP = { :A => [1, 2, 3].freeze, :B => [4, 5, 6].freeze }.freeze # or: LOOKUP = { :A => [1, 2, 3], :B => [4, 5, 6] }.tap { |s| s.values.each(&:freeze) }.freeze