-
Notifications
You must be signed in to change notification settings - Fork 0
/
09_heredocs.rb
62 lines (51 loc) · 1.18 KB
/
09_heredocs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
some_text = <<HEREDOC
Multiple
lines
of
text
HEREDOC
puts some_text
def indented_heredoc
<<-INDENTED
The '-' after the '<<' allows you to
indent the ending identifier like so:
INDENTED
end
puts indented_heredoc
# Heredocs allow interpolation by default
name = "Hector"
interpolated_heredoc = <<INTERPOLATED
Hello, my name is #{name}
What's your name?
INTERPOLATED
puts interpolated_heredoc
# But if you don't want interpolation, you can
# prevent it
uninterpolated_heredoc = <<'UNINTERPOLATED'
Hello, my name is #{name}
What's your name?
UNINTERPOLATED
puts uninterpolated_heredoc
# As you may have guessed, the starting and
# ending tokens can be anything, really. The
# ending one should exist on a line all by
# itself
puts <<GEORGE
My name isn't actually GEORGE.
I'm a Heredoc, lol!
GEORGE
# And finally, my personal fav: there's a
# variant which allows you to intent the
# _contents_ of the heredoc, and not just
# the ending token.
def nicely_indented_heredoc
<<~NICE
This heredoc doesn't break the
flow of this Ruby code as much.
Notice that when you run this,
you won't have two spaces at
the beginning of each line of
output!
NICE
end
puts nicely_indented_heredoc