[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Webrick, erb, and .rhtml

Belorion

1/12/2005 4:06:00 PM

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.


13 Answers

dblack

1/12/2005 4:13:00 PM

0

Belorion

1/12/2005 4:19:00 PM

0

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
>
>


Robert Klemme

1/12/2005 4:25:00 PM

0


"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
>
> 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
> >
> >
>
>

Belorion

1/12/2005 4:25:00 PM

0

> 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/rub...) 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?


Robert Klemme

1/12/2005 4:27:00 PM

0


"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/er...

Kind regards

robert

>
> 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
> >
> >
>
>

Bill Atkins

1/12/2005 4:29:00 PM

0

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"


Bill Atkins

1/12/2005 4:33:00 PM

0

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/rub...) 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"


Nicholas Van Weerdenburg

1/12/2005 4:43:00 PM

0

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/rub...) 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


Belorion

1/12/2005 4:58:00 PM

0

> 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...


GOTOU Yuuzou

1/12/2005 8:41:00 PM

0