Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active January 23, 2021 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/ca6dd099293315ad13f6ba60a34f0f06 to your computer and use it in GitHub Desktop.
Save JoshCheek/ca6dd099293315ad13f6ba60a34f0f06 to your computer and use it in GitHub Desktop.
Ruby's 20 string literals
# Backticks and %x make a string literal and implicitly pass it to the backticks method,
# so if we define backticks to be identity, then that makes then equal to normal string literals.
# Though in practice, I've always taken advantage of the implicit invocation instead of nooping it.
def `(str)
str
end
# I'm ignoring different delimiters, on the percent literals.
# eg I consider %q(str) and %q[str] to be equivalent, because otherwise there'd
# probably be another 500 possibilities.
#
# For the heredocs, you generally first decide you want a heredoc, then decide
# which indentation style you want, then what delimiter you want to use, and then
# what escaping style you want, and depending on these decisions, you'll arrive
# at one of the the different syntaxes. So I'm considering each to be unique.
p ['a', "b", ?c, `d`, %(e), %q(f), %Q(g), %x(h), <<I, <<'J', <<"K", <<`L`, <<-M, <<-'N', <<-"O", <<-`P`, <<~Q, <<~'R', <<~"S", <<~`T`]
i
I
j
J
k
K
l
L
m
M
n
N
o
O
p
P
q
Q
r
R
s
S
t
T
# >> ["a", "b", "c", "d", "e", "f", "g", "h", "i\n", "j\n", "k\n", "l\n", "m\n", "n\n", "o\n", "p\n", "q\n", "r\n", "s\n", "t\n"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment