Posted By

cczona on 03/14/08


Tagged

ruby String heredoc


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

karindalziel
webstic


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.