erbio.rb 794 B

12345678910111213141516171819202122232425262728293031323334353637
  1. require 'erb'
  2. ##
  3. # A subclass of ERB that writes directly to an IO. Credit to Aaron Patterson
  4. # and Masatoshi SEKI.
  5. #
  6. # To use:
  7. #
  8. # erbio = RDoc::ERBIO.new '<%= "hello world" %>', nil, nil
  9. #
  10. # open 'hello.txt', 'w' do |io|
  11. # erbio.result binding
  12. # end
  13. #
  14. # Note that binding must enclose the io you wish to output on.
  15. class RDoc::ERBIO < ERB
  16. ##
  17. # Defaults +eoutvar+ to 'io', otherwise is identical to ERB's initialize
  18. def initialize str, safe_level = nil, trim_mode = nil, eoutvar = 'io'
  19. super
  20. end
  21. ##
  22. # Instructs +compiler+ how to write to +io_variable+
  23. def set_eoutvar compiler, io_variable
  24. compiler.put_cmd = "#{io_variable}.write"
  25. compiler.insert_cmd = "#{io_variable}.write"
  26. compiler.pre_cmd = []
  27. compiler.post_cmd = []
  28. end
  29. end