What are general delimited inputs in Ruby?

What is a general delimited input?

General delimited input (GDI) is a way to create literal strings, arrays, and regular expressions and run shell commands in Ruby. GDI expressions start with a modifier consisting of a % character followed by another character representing that modifier’s type. The table below gives a bird-eye view of some general delimited input modifiers and their purposes.

General Delimited Input

Modifier

Purpose

%q

String (no interpolation support)

%Q

String (interpolation support)

%w

Array of tokens

%i

Array of symbols

%r

Regular expression

%x

Shell Command

Syntax

The general format for using GDI is as follows:

%<modifier> <open-delimiter> <content> <close-delimiter>
  • The %<modifier> defines the operation to be performed.

  • The <content> enclosed with the delimiters represents characters on which the operation is to be performed.

  • The <open-delimiter> can be one of the characters  [, {, ( or <, with the matching bracket ],},) or > serving as the <close-delimiter>. Other characters can also be used identically as delimiters in both places.

The modifiers %q and %Q

The modifiers %q and %Q turn the content into a string.

#Using %q
puts %q/They did their best!/;
puts %q(It is good to save some amount!);
#Using %Q
account_balance = 500
puts %Q{Your balance is $#{account_balance}}

Note: You can see that different kinds of delimiters like /, (), and {} can be used as is evident in the playground above.

The difference between %q and %Q is that the former %q does not perform string interpolation when converting the content to string, while the latter %Q does.

# %Q is an interpolated modifier
puts %Q(Alize, you owe me $#{400+100}, Remember?)
# %q is a non-interpolated modifier
puts %q(Alize, you owe me $#{400+100}, Remember?)

Explanation

  • Line 2: We used an interpolation modifier %Q, which evaluates the expression #{400+100} to 500 before rendering the final string.
  • Line 5: We used a modifier %q here. It treats the expression #{400+100} as part of the string. This is evident from the output of the code snippet above.

The modifier %w

The modifier %w extracts the tokens from space-separated data and creates an array of strings. Consider the playground below:

#Using %w
print %w( Fred Wilma Barney Betty Great\ Gazoo )

The modifier %i

The modifier %i converts the content into an array of symbols.

print %i( Each symbol starts with a colon)

The modifier %r

The modifier %r defines a regular expression. The regular expression can be used for matching patterns in a string using the =~ operator. Consider the playground below:

landline = "123-111-25-01"
landline =~ %r{(\d+)(?:-|:)(\d+)(?:-|:)(\d+)(?:-|:)(\d+)}
puts [$1,$2,$3,$4]

Explanation

  • Line 1: We’ll define a landline number.
  • Line 2: We’ll define a regular expression to detect the numbers and store them in $1, $2, $3, and $4.

The modifier %x

The modifier %x runs shell commands in Ruby. For example, echo 'Hello world' is a shell command for printing Hello world. We run this command in Ruby as follows:

puts %x(echo 'Hello World')

In this Answer, we tried to explain Ruby's general delimited inputs.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved