Webrick, erb, and .rhtml

I am trying to get a basic Webrick server running to serve up .rhtml
files on a WinXP machine. I saw that you need to install erb to get
this to work (BTW, the erb link on the Webrick site is broken), so I
installed erb 2.0.4.

I startup a Webrick server using:

#!/usr/local/bin/ruby
require 'webrick'
include WEBrick

s = HTTPServer.new( :Port=> 2000 )

## mount subdirectories
s.mount("/~test",
        HTTPServlet::FileHandler, "c:\\documents and
settings\\user\\desktop\\test",
        true) #<= allow to show directory index.

trap("INT"){ s.shutdown }
s.start

Serving up html works fine. However, serving up .rhtml does not work.
If I visit my test.rhtml, whose contents are:

<html><head></head><body>
testing, 1234
<%
10.times{ |i| puts i }
%>
</body></html>

All I get as output on my browser is "testing, 1234", whose source looks like
<html><head></head><body>
testing, 1234
</body></html>

At the command prompt, if I ruby "erb test.rhtml" I get:

0
1
2
3
4
5
6
7
8
9
<html>
<head>
</head>
<body>
testing, 1234

</body>
</html>

Which is obviously incorrect. What am I doing wrong here? I am using
ruby 1.8.2 (2004-07-29), and erb 2.0.4.

Hi --

Serving up html works fine. However, serving up .rhtml does not work.
If I visit my test.rhtml, whose contents are:

<html><head></head><body>
testing, 1234
<%
10.times{ |i| puts i }
%>
</body></html>

All I get as output on my browser is "testing, 1234", whose source looks like
<html><head></head><body>
testing, 1234
</body></html>

At the command prompt, if I ruby "erb test.rhtml" I get:

0
1
2
3
4
5
6
7
8
9
<html>
<head>
</head>
<body>
testing, 1234

</body>
</html>

Which is obviously incorrect. What am I doing wrong here? I am using
ruby 1.8.2 (2004-07-29), and erb 2.0.4.

You need <%= %> rather than <% %>. But keep in mind that what erb
will show you is the result of evaluating the expression. The result
of evaluating 10.times {...} is 10 -- and the result of evaluating
puts is nil. So if you want to display all the numbers, you'd want
something like:

   <%= (1..10).to_a.join(", ") %>

David

···

On Thu, 13 Jan 2005, Belorion wrote:

--
David A. Black
dblack@wobblini.net

Thank you for the quick response!

Does there exist Webrick support for handling .rhtml files the way
eruby/mod_ruby does?

Matt

···

On Thu, 13 Jan 2005 01:12:55 +0900, David A. Black <dblack@wobblini.net> wrote:

Hi --

On Thu, 13 Jan 2005, Belorion wrote:

> Serving up html works fine. However, serving up .rhtml does not work.
> If I visit my test.rhtml, whose contents are:
>
> <html><head></head><body>
> testing, 1234
> <%
> 10.times{ |i| puts i }
> %>
> </body></html>
>
> All I get as output on my browser is "testing, 1234", whose source looks like
> <html><head></head><body>
> testing, 1234
> </body></html>
>
> At the command prompt, if I ruby "erb test.rhtml" I get:
>
> 0
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> <html>
> <head>
> </head>
> <body>
> testing, 1234
>
>
> </body>
> </html>
>
> Which is obviously incorrect. What am I doing wrong here? I am using
> ruby 1.8.2 (2004-07-29), and erb 2.0.4.

You need <%= %> rather than <% %>. But keep in mind that what erb
will show you is the result of evaluating the expression. The result
of evaluating 10.times {...} is 10 -- and the result of evaluating
puts is nil. So if you want to display all the numbers, you'd want
something like:

   <%= (1..10).to_a.join(", ") %>

David

--
David A. Black
dblack@wobblini.net

You need <%= %> rather than <% %>. But keep in mind that what erb
will show you is the result of evaluating the expression. The result
of evaluating 10.times {...} is 10 -- and the result of evaluating
puts is nil. So if you want to display all the numbers, you'd want
something like:

   <%= (1..10).to_a.join(", ") %>

David

Further research shows me that on the Erb website
(http://www2a.biglobe.ne.jp/~seki/ruby/erb.html\) that

Hello <% print "world" %>

Should give me:

Hello world

But this is not the case. Rather, I get worldHello. ??? Does anyone
else have this behaviour with Erb?

"Belorion" <belorion@gmail.com> schrieb im Newsbeitrag
news:a48d774d0501120818630531b4@mail.gmail.com...

Thank you for the quick response!

Here's another way to do it

Does there exist Webrick support for handling .rhtml files the way
eruby/mod_ruby does?

Matt

> Hi --
>
>
> > Serving up html works fine. However, serving up .rhtml does not

work.

> > If I visit my test.rhtml, whose contents are:
> >
> > <html><head></head><body>
> > testing, 1234
> > <%
> > 10.times{ |i| puts i }
> > %>
> > </body></html>
> >
> > All I get as output on my browser is "testing, 1234", whose source

looks like

> > <html><head></head><body>
> > testing, 1234
> > </body></html>
> >
> > At the command prompt, if I ruby "erb test.rhtml" I get:
> >
> > 0
> > 1
> > 2
> > 3
> > 4
> > 5
> > 6
> > 7
> > 8
> > 9
> > <html>
> > <head>
> > </head>
> > <body>
> > testing, 1234
> >
> >
> > </body>
> > </html>
> >
> > Which is obviously incorrect. What am I doing wrong here? I am

using

···

On Thu, 13 Jan 2005 01:12:55 +0900, David A. Black <dblack@wobblini.net> wrote:
> On Thu, 13 Jan 2005, Belorion wrote:
> > ruby 1.8.2 (2004-07-29), and erb 2.0.4.
>
> You need <%= %> rather than <% %>. But keep in mind that what erb
> will show you is the result of evaluating the expression. The result
> of evaluating 10.times {...} is 10 -- and the result of evaluating
> puts is nil. So if you want to display all the numbers, you'd want
> something like:
>
> <%= (1..10).to_a.join(", ") %>
>
> David
>
> --
> David A. Black
> dblack@wobblini.net
>
>

Try mounting HTTPServlet::ERBHandler instead of
HTTPServlet::FileHandler. It should run ERB on anything in the
directory it's mounted on.

Bill

···

On Thu, 13 Jan 2005 01:18:50 +0900, Belorion <belorion@gmail.com> wrote:

Thank you for the quick response!

Does there exist Webrick support for handling .rhtml files the way
eruby/mod_ruby does?

Matt

On Thu, 13 Jan 2005 01:12:55 +0900, David A. Black <dblack@wobblini.net> wrote:
> Hi --
>
> On Thu, 13 Jan 2005, Belorion wrote:
>
> > Serving up html works fine. However, serving up .rhtml does not work.
> > If I visit my test.rhtml, whose contents are:
> >
> > <html><head></head><body>
> > testing, 1234
> > <%
> > 10.times{ |i| puts i }
> > %>
> > </body></html>
> >
> > All I get as output on my browser is "testing, 1234", whose source looks like
> > <html><head></head><body>
> > testing, 1234
> > </body></html>
> >
> > At the command prompt, if I ruby "erb test.rhtml" I get:
> >
> > 0
> > 1
> > 2
> > 3
> > 4
> > 5
> > 6
> > 7
> > 8
> > 9
> > <html>
> > <head>
> > </head>
> > <body>
> > testing, 1234
> >
> >
> > </body>
> > </html>
> >
> > Which is obviously incorrect. What am I doing wrong here? I am using
> > ruby 1.8.2 (2004-07-29), and erb 2.0.4.
>
> You need <%= %> rather than <% %>. But keep in mind that what erb
> will show you is the result of evaluating the expression. The result
> of evaluating 10.times {...} is 10 -- and the result of evaluating
> puts is nil. So if you want to display all the numbers, you'd want
> something like:
>
> <%= (1..10).to_a.join(", ") %>
>
> David
>
> --
> David A. Black
> dblack@wobblini.net
>
>

--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
  ('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"

"Belorion" <belorion@gmail.com> schrieb im Newsbeitrag
news:a48d774d0501120818630531b4@mail.gmail.com...

Thank you for the quick response!

Here's another way to do it (untested though):

<% 10.times do |n| %>
* <%= n %> <br/>
<% end %>

or

% 10.times do |n|
* <%= n %> <br/>
% end

see:
http://www2a.biglobe.ne.jp/~seki/ruby/erbmore.html

Kind regards

    robert

Does there exist Webrick support for handling .rhtml files the way
eruby/mod_ruby does?

Matt

> Hi --
>
>
> > Serving up html works fine. However, serving up .rhtml does not

work.

> > If I visit my test.rhtml, whose contents are:
> >
> > <html><head></head><body>
> > testing, 1234
> > <%
> > 10.times{ |i| puts i }
> > %>
> > </body></html>
> >
> > All I get as output on my browser is "testing, 1234", whose source

looks like

> > <html><head></head><body>
> > testing, 1234
> > </body></html>
> >
> > At the command prompt, if I ruby "erb test.rhtml" I get:
> >
> > 0
> > 1
> > 2
> > 3
> > 4
> > 5
> > 6
> > 7
> > 8
> > 9
> > <html>
> > <head>
> > </head>
> > <body>
> > testing, 1234
> >
> >
> > </body>
> > </html>
> >
> > Which is obviously incorrect. What am I doing wrong here? I am

using

···

On Thu, 13 Jan 2005 01:12:55 +0900, David A. Black <dblack@wobblini.net> wrote:
> On Thu, 13 Jan 2005, Belorion wrote:
> > ruby 1.8.2 (2004-07-29), and erb 2.0.4.
>
> You need <%= %> rather than <% %>. But keep in mind that what erb
> will show you is the result of evaluating the expression. The result
> of evaluating 10.times {...} is 10 -- and the result of evaluating
> puts is nil. So if you want to display all the numbers, you'd want
> something like:
>
> <%= (1..10).to_a.join(", ") %>
>
> David
>
> --
> David A. Black
> dblack@wobblini.net
>
>

print or puts will send their output to STDOUT - anything sent there
will go to the shell that you used to start webrick. ERb is simply
transforming a string, so you have to use <%= to get portions of that
string replaced with something else.

Bill

···

On Thu, 13 Jan 2005 01:25:17 +0900, Belorion <belorion@gmail.com> wrote:

> You need <%= %> rather than <% %>. But keep in mind that what erb
> will show you is the result of evaluating the expression. The result
> of evaluating 10.times {...} is 10 -- and the result of evaluating
> puts is nil. So if you want to display all the numbers, you'd want
> something like:
>
> <%= (1..10).to_a.join(", ") %>
>
> David

Further research shows me that on the Erb website
(http://www2a.biglobe.ne.jp/~seki/ruby/erb.html\) that

Hello <% print "world" %>

Should give me:

Hello world

But this is not the case. Rather, I get worldHello. ??? Does anyone
else have this behaviour with Erb?

--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
  ('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"

You misread the page. It says exactly what you got. Notice it is
showing eruby and erb examples. The erb example gives what you got.

<% print "hello" %> goes to stdout.

<%= "hello" %> is a short-cut for something like <% response.print
"hello" %> which may or may not be/include stdout.

So depending on how stdout is handled during building the template, <%
print "hello" %> may or may not work. In the examples on the erb page,
it shows it working for eruby but not for erb.

Nick

···

On Thu, 13 Jan 2005 01:25:17 +0900, Belorion <belorion@gmail.com> wrote:

> You need <%= %> rather than <% %>. But keep in mind that what erb
> will show you is the result of evaluating the expression. The result
> of evaluating 10.times {...} is 10 -- and the result of evaluating
> puts is nil. So if you want to display all the numbers, you'd want
> something like:
>
> <%= (1..10).to_a.join(", ") %>
>
> David

Further research shows me that on the Erb website
(http://www2a.biglobe.ne.jp/~seki/ruby/erb.html\) that

Hello <% print "world" %>

Should give me:

Hello world

But this is not the case. Rather, I get worldHello. ??? Does anyone
else have this behaviour with Erb?

--
Nicholas Van Weerdenburg

You misread the page. It says exactly what you got. Notice it is
showing eruby and erb examples. The erb example gives what you got.

Now I feel silly :P. Thanks.

<% print "hello" %> goes to stdout.

<%= "hello" %> is a short-cut for something like <% response.print
"hello" %> which may or may not be/include stdout.

So depending on how stdout is handled during building the template, <%
print "hello" %> may or may not work. In the examples on the erb page,
it shows it working for eruby but not for erb.

Nick

That's tough luck for me then. I have a significantly large rhtml
project that uses lots of puts and print because that works fine in
Apache/eRuby/mod_ruby. Guess I have a lot of busy work ahead of me,
changing all my puts/prints to <%= "" %> if I want to support Webrick
serving. Thanks for the help!

One last question... using ERBHanlde instead of FileHanlder, this
works fine (brining up index.html):

localhost:2000/~test/

However, if I use:

locahost:2000/~test/index.html

I get a permission denied error. Why? They are both accessing the same files...

In message <a48d774d05011208585bab21d7@mail.gmail.com>,

···

`Belorion <belorion@gmail.com>' wrote:

That's tough luck for me then. I have a significantly large rhtml
project that uses lots of puts and print because that works fine in
Apache/eRuby/mod_ruby. Guess I have a lot of busy work ahead of me,
changing all my puts/prints to <%= "" %> if I want to support Webrick
serving. Thanks for the help!

You can make a custom handler class using eruby command.

--
require "webrick"

class ERubyHandler < WEBrick::HTTPServlet::ERBHandler
  ERuby = "/usr/local/bin/eruby"

  def initialize(server, name)
    super
    @script_filename = name
  end

  def do_GET(req, res)
    IO.popen("#{ERuby} #{@script_filename}"){|io|
      res.body = io.read
    }
    res["content-type"] = "text/html"
  end
end

# register new handler class
WEBrick::HTTPServlet::FileHandler.add_handler("rhtml", ERubyHandler)

s = WEBrick::HTTPServer.new(:Port=> 2000)
s.mount("/~test", WEBrick::HTTPServlet::FileHandler, Dir.pwd)
trap("INT"){ s.shutdown }
s.start
--

One last question... using ERBHanlde instead of FileHanlder, this
works fine (brining up index.html):

localhost:2000/~test/

However, if I use:

locahost:2000/~test/index.html

I get a permission denied error. Why? They are both accessing the same files...

What arguments did you pass to mount method?

ERBHandler cannot process directories. It is designed to be
used by FileHandler. However, it can be mounted on the
server with a eruby script filename like:

  s.mount("/~test", ERBHandler, "/somewhere/index.html")

--
gotoyuzo

Would simply overriding 'puts' and 'print' to return what they print
work? That way you only need to change <% to <%= .

martin

···

Belorion <belorion@gmail.com> wrote:

That's tough luck for me then. I have a significantly large rhtml
project that uses lots of puts and print because that works fine in
Apache/eRuby/mod_ruby. Guess I have a lot of busy work ahead of me,
changing all my puts/prints to <%= "" %> if I want to support Webrick
serving. Thanks for the help!

Martin DeMello wrote:

That's tough luck for me then. I have a significantly large rhtml
project that uses lots of puts and print because that works fine in
Apache/eRuby/mod_ruby. Guess I have a lot of busy work ahead of me,
changing all my puts/prints to <%= "" %> if I want to support Webrick
serving. Thanks for the help!

Would simply overriding 'puts' and 'print' to return what they print
work? That way you only need to change <% to <%= .

martin

Why is it that erb doesn't override kernel puts and print so they go to ERBOUT instead of STDOUT. That way you wouldn't get weird orderings when you use them and it would make more sense. If you actually want to print output to stdout or anything like that then you explicitly call STDOUT.print or .puts. I think alot of people assume that print and puts output in the same interlaced order as the <% and <%= blocks, but they really don't. Does anyone have a good reason why this should not be the case?
  Charles Comstock

···

Belorion <belorion@gmail.com> wrote:

Is there an ERBOUT? How about doing:

$stdout_default=$stdout # or STDOUT
$stdout=ERBOUT

This works for $stdout=$stderr, but I haven't tried it with erb.

Nick

···

On Wed, 19 Jan 2005 08:36:06 +0900, Charles Comstock <cc1@cec.wustl.edu> wrote:

Martin DeMello wrote:
> Belorion <belorion@gmail.com> wrote:
>
>>That's tough luck for me then. I have a significantly large rhtml
>>project that uses lots of puts and print because that works fine in
>>Apache/eRuby/mod_ruby. Guess I have a lot of busy work ahead of me,
>>changing all my puts/prints to <%= "" %> if I want to support Webrick
>>serving. Thanks for the help!
>
>
> Would simply overriding 'puts' and 'print' to return what they print
> work? That way you only need to change <% to <%= .
>
> martin

Why is it that erb doesn't override kernel puts and print so they go to
ERBOUT instead of STDOUT. That way you wouldn't get weird orderings
when you use them and it would make more sense. If you actually want to
print output to stdout or anything like that then you explicitly call
STDOUT.print or .puts. I think alot of people assume that print and
puts output in the same interlaced order as the <% and <%= blocks, but
they really don't. Does anyone have a good reason why this should not
be the case?
        Charles Comstock

--
Nicholas Van Weerdenburg