Autorun .rb like a .php

Hi ! im new to ruby and i was wondering if there is possible (i know it
must be) to autorun my scripts .rb.

For example, when i create a .php i only upload to my home-server and
search url local-ip/home/web/My_script.php and i can test it.

Another question… ruby compiles line by line ?

Thanks !! :slight_smile:

Excerpts from cristian d.'s message of Fri May 25 02:41:14 +0200 2012:

For example, when i create a .php i only upload to my home-server and
search url local-ip/home/web/My_script.php and i can test it.
Try googling for “apache howto ruby module” or such.

Another question… ruby compiles line by line ?
Ruby does not compile, its interpreted (the jruby version should JIT to
some extend)

Marc W.

I would terminate your ruby file with .cgi instead of .rb.

Then to get ruby to “run like php” you just ensure the file is
executable and “AddHandler cgi-script .cgi” to your apache config.

I’m sure you realize ruby is little like PHP in this regard. PHP embeds
itself in an HTML file. Ruby executes on the server, and whatever it
puts will be sent to the client as … whatever you declare your
content-type to be.

So before you start putting HTML, you need to:
puts “Content-type: text/html\n”
before putting plain text, it’d be:
puts “Content-type: text/plain\n”
and json:
puts “Content-type: application/json\n”

cristian d. wrote in post #1062052:

Hi ! im new to ruby and i was wondering if there is possible (i know it
must be) to autorun my scripts .rb.

For example, when i create a .php i only upload to my home-server and
search url local-ip/home/web/My_script.php and i can test it.

Although it can be done, and is OK just for playing, it’s not a good way
to run ruby apps.

You can embed a ruby interpreter into Apache using mod_ruby (which is
very old and unmaintained). There is even erb/rhtml which is ruby
embedded in HTML. This has the same problem as PHP in that all pages and
applications are sharing the same interpreter instance - however this is
worse for ruby because individual pages can modify core classes which in
turn can interfere with the operation of every other page.

You can write ruby scripts which are CGIs, and there is a ruby CGI
library with helper methods. However this will start up a fresh ruby
interpreter for every single request which comes in, which well then
have to load from scratch all the libraries you are using, make a fresh
connection to a database if you have one, and so on. This will give very
poor performance except for small scripts with few dependencies (and for
this, you definitely don’t want to be running jruby!)

These days, everyone writes ruby web applications using an API called
‘Rack’ which adapts HTTP requests to ruby method calls in a persistent
process. Coding directly to the native rack API is rather like CGI
programming but without the helpers, so people use some sort of
framework on top, from the simple and lightweight Sinatra to the huge
and featureful Rails.

You then run instances of your app as persistent Ruby webservers and
proxy to them from a HTTP frontend - or you use Phusion Passenger which
embeds Rack support into Apache or Nginx.

If you administer your own webserver then Phusion Passenger is pretty
easy to add (there are step-by-step instructions) and it will
dynamically run your app after it has been uploaded.