开发者

Ruby as a scripting language for web server

Is it possible to use Ruby as a scripting language with a HTTP server ? I'd like to be able to simply put some Ruby files in a web directory and be able to execute them from my browser - just l开发者_如何学JAVAike I did with PHP.

I have absolutely nothing against frameworks such as RoR, but I was told that I should first learn Ruby and only then move on with higher level frameworks. Of course, I could write some Ruby scripts and run them in the console, but I would prefer getting the input/output from my browser :)

Is that possible at all ? Otherwise, how hard would it be for me to build a quick and simple web framework ?


Take a look at Sinatra - not your plain ol' CGI/PHP style, but really really simple web framework to get started with, elegant so it won't get in your way while learning Ruby, and powerful enough to make quite useful web apps.


ERB is similar in spirit to PHP (in the sense you need).

You need to setup apache to parse rhtml files with erb, here is a guide for OSX.


Depends on your server, but any language can be used with CGI Programming, including Ruby.

Generally speaking - to find out - put a basic "Hello World" .rb or .cgi file on your web server and chmod it's permissions so it is executable to "others":

chmod 755 YourScript.rb

When writing CGI scripts, you have to ensure that you have the appropriate shebang at the top of your file. For Ruby you'd probably use something like...

#!/usre/bin/ruby

(no promises this is the one)

Then write yourself a little hello world CGI script. The output of a CGI script is kind of a partial HTTP Response: (I'm not really a Ruby coder so this might be completely arse-backwards)

puts "Content-Type:text/plain\n"
puts "Content-Length:12\n"
puts "\n"
puts "Hello World!"

Visit the file the same way you would a PHP or Perl script and you'll see if it works. Again; it depends on your server.


You can do that configuring your server for CGI.

You can even write web applications with assembler if you want to, using CGI.

(In the dawn of time, they used C/C++ to write web applications, go figure).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜