here-documents

Some ways of using here-docs:

name = "bob"
# interpolation
doc = <<DOC
  Yo #{name},
  what's up
DOC
p doc
#=> "  Yo bob,\n  what'
s up\n"
# indent the end marker
doc = <<-DOC
  Yo #{name},
  what's up
  DOC
p doc
# => "  Yo bob,\n  what'
s up\n"
# no interpolation
doc = <<-'DOC'
  Yo #{name},
  what's up      
  DOC
p doc
# => "  Yo \#{name},\n  what'
s up\n"
# replacing spaces with underscores
doc = <<DOC.gsub(' ', '_')
  Yo #{name},
  what's up      
DOC
p doc
# => "__Yo_bob,\n__what'
s_up\n"

Leave a Comment