Peter Black Peter Black - 6 months ago 16
Ruby Question

What does $/ mean in Ruby?

I was reading about Ruby serialization (http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/) and came across the following code. What does $/ mean? I assume $ refers to an object?

array = []
$/="\n\n"
File.open("/home/alan/tmp/blah.yaml", "r").each do |object|
array << YAML::load(object)
end

Answer

$/ is a pre-defined variable. It's used as the input record separator, and has a default value of "\n".

Functions like gets uses $/ to determine how to separate the input. For example:

$/="\n\n"
str = gets
puts str

So you have to enter ENTER twice to end the input for str.

Reference: Pre-defined variables