Ruby 3.4 takes the first step in a multi-version transition to frozen string literals by default. Your Rails app will continue working exactly as before, but Ruby now provides opt-in warnings to help you prepare. Here’s what you need to know. The Three-Phase Transition Plan Ruby is implementing frozen string literals gradually over three releases: Ruby 3.4 (Now): Opt-in warnings when you enable deprecation warnings Ruby 3.7 (Future): Warnings enabled by default Ruby 4.0 (Future): Frozen string literals become the default What Actually Changes in Ruby 3.4 By default, nothing changes. Your code runs exactly as before. But when you enable deprecation warnings: 1 2 3 4 5 6 # Enable warnings to see what will break in the future Warning[:deprecated] = true # Now string mutations emit warnings csv_row = "id,name,email" csv_row << ",created_at" # warning: literal string will be frozen in the future Important: These warnings are opt-in. You won’t see them unless you explicitly enable them. Why Should You Care? 1. Performance Gains Are Real Frozen strings enable Ruby to deduplicate identical string literals: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 # Ruby 3.3 - Each method call creates a new String object def log_action(action) prefix = "[ACTION]" # New String object every time puts "#{prefix} #{action}" end # Ruby 3.4 with frozen strings - Same object reused # frozen_string_literal: true def log_action(action) prefix = "[ACTION]" # Same frozen String object ID every time puts "#{prefix} #{action}" end # You can verify this: 3.times.map { "[ACTION]".object_id }.uniq.size # => 3 (different objects) # With frozen_string_literal: true 3.times.map { "[ACTION]".object_id }.uniq.size # => 1 (same object) Performance improvements vary by application, but benchmarks have shown: Up to 20% reduction in garbage collection for string-heavy code Memory savings from string deduplication Faster execution in hot paths that create many identical strings For more Rails performance optimizatio...
First seen: 2025-07-09 10:34
Last seen: 2025-07-10 03:37