Ruby heredoc tricks


/ Published in: Ruby
Save to your folder(s)

Following a


Copy this code and paste it in your HTML
  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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.