DEV Community

Herrington Darkholme
Herrington Darkholme

Posted on

A list of Ruby String Literal Creations

There are three main categories of string literals in Ruby: quotation, percentage and heredoc. And every category has its own sub-variants.

Quotation

This category uses single or double quotes to enclose a string.

  • Single-quoted strings do NOT allow escape sequences or interpolation.
  • double-quoted strings do allow escape sequences and interpolation.
# Single-quoted string
str = 'This is a single-quoted string'
# => "This is a single-quoted string"

# Double-quoted string
str = "This is a double-quoted string with interpolation: #{2 + 2}"
# => "This is a double-quoted string with interpolation: 4"
Enter fullscreen mode Exit fullscreen mode

Percentage:

This category uses the % notation to create different types of string literals.

  • Available modifiers are
    • Non-interpolable
    • interpolable
    • array of strings
    • array of symbols
    • regular expression
    • symbol
    • backtick
  • You can use any non-alphanumeric character as a delimiter, such as parentheses, brackets, braces, etc.
# Non-interpolable string
str = %q(This is a single-quoted string)
# => "This is a single-quoted string"

# Interpolable string
str = %Q(This is a double-quoted string with interpolation: #{2 + 2})
# => "This is a double-quoted string with interpolation: 4"

# Array of strings
arr = %w(one two three)
# => ["one", "two", "three"]

# Array of symbols
arr = %i(one two three)
# => [:one, :two, :three]

# Regular expression
reg = %r(\d+)
# => /\d+/

# Symbol
sym = %s(hello)
# => :hello

# Backtick
cmd = %x(date)
# => "Wed Dec 27 04:07:07 UTC 2023\n"
Enter fullscreen mode Exit fullscreen mode

Heredoc

This category allows you to create multi-line strings.

  • You can a delimiter of your choice, such as EOS, END, etc.
  • You can use single quotation in the delimiter 'EOF' to disable interpolation in heredoc.
  • You can use <<~ to remove leading whitespace from each line, or <<- to allow an indented closing delimiter.
  • You can call a method on a heredoc by placing it after the opening identifier.
  • Opening multiple heredocs on the same line is possible by using different identifiers and delimiters.
# Heredoc syntax
str = <<EOS
This is a multi-line string
with heredoc syntax.
EOS
# => "This is a multi-line string\nwith heredoc syntax.\n"

# Heredoc without interpolation
name = "John"
str = <<-'HEREDOC'
My name is #{name}.
HEREDOC
# => "My name is #{name}.\n"

# Heredoc syntax with leading whitespace removed
str = <<~END
  This is another multi-line string
  with leading whitespace removed.
END
# => "This is another multi-line string\nwith leading whitespace removed.\n"

# Heredoc syntax with an indented closing delimiter
str = <<-TEXT
  This is a multi-line string
  with an indented closing delimiter.
  TEXT
# => "This is a multi-line string\nwith an indented closing delimiter.\n"

# Heredoc syntax with a method call
str = <<-TEXT.chomp
  This is a multi-line string
  with a method call.
  TEXT
# => "This is a multi-line string\nwith a method call."

# Multiple heredoc syntax
puts (<<-ONE, <<-TWO)
content for heredoc one
ONE
content for heredoc two
TWO
# => "content for heredoc one\ncontent for heredoc two"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)