We Recommend

Beginning Ruby: From Novice to Professional Beginning Ruby: From Novice to Professional
Beginning Ruby is a thoroughly contemporary guide for every type of reader wanting to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a genuine Ruby application.


Posted By

cczona on 03/14/08


Tagged

ruby String heredoc


Versions (?)


Ruby heredoc tricks


Published in: Ruby 


URL: http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html

Following a

  1. print <<EOF
  2. The price is #{$Price}.
  3. EOF
  4.  
  5. print <<"EOF"; # same as above
  6. The price is #{$Price}.
  7. EOF
  8.  
  9. print <<`EOC` # execute commands
  10. echo hi there
  11. echo lo there
  12. EOC
  13.  
  14. print <<"foo", <<"bar" # you can stack them
  15. I said foo.
  16. foo
  17. I said bar.
  18. bar
  19.  
  20. myfunc(<<"THIS", 23, <<'THAT')
  21. Here's a line
  22. or two.
  23. THIS
  24. and here's another.
  25. THAT
  26.  
  27. if need_define_foo
  28. eval <<-EOS # delimiters can be indented
  29. def foo
  30. print "foo\n"
  31. end
  32. EOS
  33. end

Report this snippet 

You need to login to post a comment.