-
-
Save carllerche/180448 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# heredocs can continue the code on the same line: | |
class SomeController | |
def render opts | |
p opts | |
end | |
def setup; false; end | |
def action_a | |
### | |
# | |
# render :status => 404, :text => <<-EOH and return unless setup | |
# | |
# The "and" in this statement implies that render will always return true. | |
# We can remove the "and" keyword, maintain the flow of execution, and not | |
# have to worry about the return value of "render": | |
return render :status => 404, :text => <<-EOH unless setup | |
article not found #{(['pew'] * 3).join ' '}<br/> | |
I, as a server, have failed<br/> | |
https? | |
EOH | |
puts "other things" | |
end | |
def action_b | |
### | |
# Single quote here docs mean we explicitly do *not* want to interpolate: | |
return render :status => 404, :text => <<-'EOH' unless setup | |
article not found #{(['pew'] * 3).join ' '}<br/> | |
I, as a server, have failed<br/> | |
https? | |
EOH | |
puts "other things" | |
end | |
def action_c | |
### | |
# Multiple heredocs on the same ine? | |
render :text_a => <<-EOH, :text_b => <<-HOE | |
hello | |
EOH | |
w0t? | |
HOE | |
end | |
end | |
sc = SomeController.new | |
sc.action_a | |
sc.action_b | |
sc.action_c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment