Skip to content

Instantly share code, notes, and snippets.

@carllerche
Forked from tenderlove/gist:180387
Created September 3, 2009 18:38
Show Gist options
  • Save carllerche/180448 to your computer and use it in GitHub Desktop.
Save carllerche/180448 to your computer and use it in GitHub Desktop.
# 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