Weird Ruby: Heredoc Delimiters
Remember how I thought that the syntax for single-quoted heredocs was weird? I’ve got something even weirder for you today!
Turns out that when using single-quoted heredoc delimiters you can have pretty much anything in them, including spaces!
<<'END HTML'
<html>
...
</html>
END HTML
<<'\n!"£$%^&*()-=_+[];#{}:@~,./<>?|`\'
hello
\n!"£$%^&*()-=_+[];#{}:@~,./<>?|`\
It never crossed my mind that something like this would be possible until I came across this RuboCop ticket, asking for spaces in delimiters to be allowed. As most editors are unlikely to handle this properly1, and it looks pretty… weird, I think that you should restrain yourselves from using such heredoc delimiters.
That’s all I have for you today. Keep Ruby Weird!
Articles in the Series
- Weird Ruby: Pure Object-Oriented Negation
- Weird Ruby: Positive and Negative Strings
- Weird Ruby: Double Negation
- Weird Ruby: Single-quoted Heredocs
- Weird Ruby: Block Comments
- Weird Ruby: Zeroing in on a Couple of Numeric Predicates
- Weird Ruby: Invoking Lambdas
- Weird Ruby: For Loops
- Weird Ruby: Array Multiplication
- Weird Ruby: Heredoc Delimiters
- Weird Ruby: Mixing Code and Data
- Weird Ruby: A Weird Way to Filter Out Elements
- Weird Ruby: Nil Conversions
- Weird Ruby: Fun with String#split
- Weird Ruby: Incrementing Strings
- Weird Ruby: The Double Aliased Enumerable Method