当前位置: 动力学知识库 > 问答 > 编程问答 >

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

网友答案:

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.

网友答案:

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.

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: