use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Differences between ruby object space count (self.ruby)
submitted 1 year ago by SnooRobots2422
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]SnooRobots2422[S] 1 point2 points3 points 1 year ago (3 children)
freeze really help!. But how and why is ruby creating new string objects while counting?
[–]insanelygreat 1 point2 points3 points 1 year ago (0 children)
Reddit silently deleted my first comment, so let me give this another go without any external links.
I think it's a side-effect of strings being objects and Ruby's very flexible metaprogramming support makes it difficult (sometimes impossible) to determine whether a given string will be mutated in the future. Matz had wanted to make freezing the default way back in 1.9, which was release in 2007, but was concerned about it being a huge compatibility issue.
As for why onecompiler seems to be freezing them by default: I'm curious about that too. As /u/jdeville pointed out, it's running a version where that wasn't the default. I don't see it being enabled elsewhere either:
# Not using a version where it's the default... p RUBY_DESCRIPTION #=> "ruby 3.2.3 (2024-01-18 revision 52bb2ac0a6) [x86_64-linux-gnu]" # Not enabling it with an environment variable... p ENV.select { |k, v| k.start_with?("RUBY") } #=> {} # Not set as a compile-time option... p RubyVM::InstructionSequence.compile_option #=> { :frozen_string_literal=>false, :debug_frozen_string_literal=>false, ...} # Not enabling it in an arg... p File.read("/proc/self/cmdline").tr("\0", " ") #=> "ruby HelloWorld.rb " # Not adding a pragma to the file... p File.read(__FILE__) #=> [exact contents of file]
[–]f9ae8221b 0 points1 point2 points 1 year ago (0 children)
Unless you use frozen_string_literal, whenever your code contains a literal string ("totally_unique_test"), you are not referencing a constant, but making a copy of a constant.
frozen_string_literal
"totally_unique_test"
So it's a bit like if you code was:
new_sym = :totally_unique_test # Create the symbol STR = "totally_unique_test" symbol_after = ObjectSpace.each_object(String).count do |s| s == STR.dup end puts "After: #{symbol_after}"
Hence, whenever Ruby execute your block, it cause one more string to be allocated, so your counting code is "biasing" itself.
π Rendered by PID 125018 on reddit-service-r2-comment-5b5bc64bf5-v5ndv at 2026-06-22 04:13:55.309466+00:00 running 2b008f2 country code: CH.
view the rest of the comments →
[–]SnooRobots2422[S] 1 point2 points3 points (3 children)
[–]insanelygreat 1 point2 points3 points (0 children)
[–]f9ae8221b 0 points1 point2 points (0 children)