6

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
Peter Black
  • 1,142
  • 1
  • 11
  • 29
  • 1
    That's bad code btw. Just switching to [`IO.foreach`](http://ruby-doc.org/core/IO.html#method-c-foreach) would be a major improvement: `File.foreach("/home/alan/tmp/blah.yaml", "\n\n") do |object|`. That doesn't use (obscure) global variables and also closes the file. – cremno May 20 '16 at 13:52
  • 1
    This is not a duplicate of the [nominated exemplar](http://stackoverflow.com/questions/1042384/how-do-you-use-global-variables-or-constant-values-in-ruby): That question is about how to use global variables in Ruby, but this question is about the meaning of a specific pre-defined global variable. – Wayne Conrad May 20 '16 at 23:23

4 Answers4

10

$/ 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

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
2

This code is trying to read each object into an array element, so you need to tell it where one ends and the next begins. The line $/="\n\n" is setting what ruby uses to to break apart your file into.

$/ is known as the "input record separator" and is the value used to split up your file when you are reading it in. By default this value is set to new line, so when you read in a file, each line will be put into an array. What setting this value, you are telling ruby that one new line is not the end of a break, instead use the string given.

For example, if I have a comma separated file, I can write $/="," then if I do something like your code on a file like this:

foo, bar, magic, space

I would create an array directly, without having to split again:

["foo", " bar", " magic", " space"]

So your line will look for two newline characters, and split on each group of two instead of on every newline. You will only get two newline characters following each other when one line is empty. So this line tells Ruby, when reading files, break on empty lines instead of every line.

tanius
  • 14,003
  • 3
  • 51
  • 63
Tormyst
  • 187
  • 6
1

I found in this page something probably interesting: http://www.zenspider.com/Languages/Ruby/QuickRef.html#18

$/ # The input record separator (eg #gets). Defaults to newline.

GondraKkal
  • 87
  • 2
  • 16
1

The $ means it is a global variable.

This one is however special as it is used by Ruby. Ruby uses that variable as a input record separator

For a full list with the special global variables see: http://www.rubyist.net/~slagell/ruby/globalvars.html