Ruby - String Record separator $/

Introduction

Ruby predefines a variable, $/, as a record separator.

This variable is used by methods such as gets and chomp.

The gets method reads in a string up to and including the record separator.

The chomp method returns a string with the record separator removed from the end if present.

You can redefine the record separator if you want, like this:

$/= "*"        # the "*" character is now the record separator 

When you redefine the record separator, this new character or string will be used by methods such as gets and chomp. Here's an example:

Demo

$/= "world" 
s = "this is a world ... hi hi hi" 
puts( s )

Result

Related Topic