Comment line in Erb

A line starting with # in an Erb program is not
interpreted as a comment and displayed.

This is annoying, since the first line of a CGI program
must start with a #! line. Is the only solution to use
the Erb Class within a ruby program?

I use erb.bat in a Ruby 1.8 win32 installation.

A simple example is:

#!E:/ruby/bin/erb.bat
<%print "Content-type: text/html\r\n\r\n"%>
<HTML>
<HEAD>
<TITLE>Erb Test</TITLE>
<CENTER><H1>Erb Test</H1><b>
<%="The current time is #{Time.now}" %> </CENTER>
#This is a comment
</HEAD>
</HTML>

Takao Ueda wrote:

A line starting with # in an Erb program is not
interpreted as a comment and displayed.

This is annoying, since the first line of a CGI program
must start with a #! line. Is the only solution to use
the Erb Class within a ruby program?

I use erb.bat in a Ruby 1.8 win32 installation.

A simple example is:

#!E:/ruby/bin/erb.bat
<%print "Content-type: text/html\r\n\r\n"%>
<HTML>
<HEAD>
<TITLE>Erb Test</TITLE>
<CENTER><H1>Erb Test</H1><b>
<%="The current time is #{Time.now}" %> </CENTER>
#This is a comment
</HEAD>
</HTML>

We can use the HTML tags <!...> and the Erb tags <%#...%>
for comments between <HTML> and </HTML>.
So the only problem is for the first line.

[Takao Ueda <takaoueda@juno.com>, 2004-11-19 16.23 CET]

A line starting with # in an Erb program is not
interpreted as a comment and displayed.

This is annoying, since the first line of a CGI program
must start with a #! line. Is the only solution to use
the Erb Class within a ruby program?

How about this patch? (Option "--skip-first" skips first line if starts with
"#!").

--- /usr/local/bin/erb~ 2004-11-19 23:15:59.000000000 +0100
+++ /usr/local/bin/erb 2004-11-19 23:15:59.000000000 +0100
@@ -43,7 +43,7 @@

     def run(factory=ERB)
       trim_mode = 0
- disable_percent = false
+ disable_percent = skip_first = false
       begin
   while switch = ARGV.switch
     case switch
@@ -88,6 +88,8 @@
       end
     when '-P'
       disable_percent = true
+ when '--skip-first'
+ skip_first = true
     when '--help'
       raise "print this help"
     else
@@ -113,6 +115,7 @@
       end

       src = $<.read
+ src.sub!(/\A#!.*\n/, "") if skip_first
       exit 2 unless src
       trim = trim_mode_opt(trim_mode, disable_percent)
       erb = factory.new(src.untaint, safe_level, trim)

Thank you Carlos,

When I changed \ruby\bin\erb following your advice
and changed \ruby\bin\erb.bat to
       @echo off
       E:\ruby\bin\ruby.exe "E:\ruby\bin\erb"
    "--skip-first" %2 %3 %4 %5 %6 %7 %8 %9
then src = $<.read became an endless loop.

Therefore, I simply added
      src.sub!(/\A#!.*\n/, "")
to the original \ruby\bin\erb and kept others intact.
Then it worked.

I should have changed the first line of my erb program into
#!E:/ruby/bin/erbnew.bat --skip-first
instead of altering the erb.bat.

Takao

I mean

···

#!E:/ruby/bin/erb.bat --skip-first

Takao