Single-Quoted Strings
A pair of single quotes (''
) in Ruby is used to define a string that doesn't support interpolation and ignores escaped characters.
To get a whole picture of Ruby string syntaxes, check out double-quoted strings and heredocs.
Here are some basic single-quoted strings:
> 'Hello'
=> "Hello"
> who = 'World'
=> "World"
> 'Hello #{who}!'
=> "Hello \#{who}!"
Here we can see that single-quoted strings do not support escaped characters like \n
where as double-quoted strings do.
> puts '1. One\n2. Two'
1. One\n2. Two
=> nil
> puts "1. One\n2. Two"
1. One
2. Two
=> nil
Single-quoted strings can be used to define non-standard symbols, such as symbols with spaces. As might be expected, interpolation doesn't work in single-quoted symbol literals.
> :'Ruby'
=> :Ruby
> :'Ruby on Rails'
=> :"Ruby on Rails"
> :'Ruby on Rails #{4.0 * 2}'
=> :"Ruby on Rails \#{4.0 * 2}"