Explains the meaning of Ruby's obscure $, (output field separator) and $\ (output record separator) global variables, sparked by a LinkedIn question about a trailing comma spotted in the Rails source. The default nil value means no separator, but setting these variables lets you generate CSV-like output from the command line without requiring the CSV library. Notes that this mnemonic global variable is deprecated in Ruby 4.0, with the English module alias $OUTPUT_RECORD_SEPARATOR recommended instead for non-one-liner code.
Questions this post answers
What does the $, global variable mean in Ruby?
It is the output field separator, borrowed from Perl and AWK conventions, where comma is a common separator mnemonic. Its default value is nil, meaning no separator is inserted between values printed with methods like print. Setting it to a comma lets print output comma-separated values without requiring the CSV library. Developers puzzling over cryptic Ruby globals can find explainers like this through daily.dev.
Is Ruby's $, global variable deprecated?
Yes, the mnemonic global variable $, (along with its counterpart $\, the output record separator) is deprecated starting in Ruby 4.0. The recommended replacement in non-one-liner code is the English module alias $OUTPUT_RECORD_SEPARATOR, which is more readable than the cryptic punctuation-based name. Ruby developers tracking upcoming 4.0 deprecations can follow changes like this on daily.dev.
Why does Rails set sep to $, instead of nil in its source code?
Rails sets sep to the current global output record separator rather than hardcoding nil, so that the behavior complies with whatever separator convention a programmer has already configured via $,. Since $,'s default value is nil anyway, this preserves expected output formatting instead of overriding a developer's custom separator setting. Understanding quirky Rails source decisions like this helps developers debugging similar code via daily.dev.