CHAPTER 3

image

Strings

String Literals

A string within Ruby is a sequence of one of more bytes, typically a set of characters. These can be created, manipulated, and output when required. Strings are generally wrapped in single or double quotation marks:

2.1.0 :001 > puts "Hello World"Hello World2.1.0 :001 > puts 'Hello World'Hello World

However if the string contains another double/single quote, these will need to be escaped in order for the string to be complete, and the code to compile:

2.1.0 :001'> puts 'Welcome to John's website'SyntaxError: (irb):5: syntax error, unexpected tIDENTIFIER, expecting end-of-inputputs 'Welcome to John's website' # ...

Get Ruby Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.