2

In PHP world you can just create index.php file, put some inline code and raw HTML, run Apache and it just works.

There are a lot af talks about bad practices of using inline code an so on. So we won't discuss this theme here, please.

How can I start my Ruby application same way? I want to use ERB for code messing, so it will look this way

# index.rb
<h1>Hello world!</h1>
<div>
  1 + 1 = <%= 1 + 1 %>
</div>

So my questions are:

  1. What makes PHP just work.
    AFAIU(nderstand) There is native support for HTTP in PHP, so I have to use Rack to support it with Ruby
  2. Some basic knowledge for creating my on "microframework": working with application/http servers (Mongrel, nginx, binding on http port and all such kind of job), working with HTTP requests: sessions, params, GET/POST etc (Rack?), sending responses (ERB templating).

So I can make my own (in educational puprposes) microframework for PHP-style web development with Ruby :D

UPD

What really I want to do is an application wich will just get request url, run just that file and return HTML as a response. Also this application should be allowed to be binded on some port

index.rb
info/about.rb
info/contacts.rb
products/product.rb

so It should parse url localhost/index.rb and run index.rb, localhost/products/product.rb?product_id=10 and run products/product.rb and pass product_id=10 as a params hash.

UPD 2

I think good point to start is to dig into Camping microframework source:

https://github.com/camping/camping

It is about 5Kb weight so I shouldn't be confused in it

2
  • 1
    Why don't you just use Rails or another Ruby based framework? What is it you are trying to do? Commented Apr 25, 2011 at 7:51
  • @Mike, because I want to know how it works, how can I make my own, how can I patch it and so on. It is about learning. Of course in production I use Rails, Sinatra, Camping and other cool Ruby frameworks. But I want to not just use them but understand them. And I am trying to do my own microframework wich will work with separate "*.rb" files as pure PHP site do. Commented Apr 25, 2011 at 9:00

3 Answers 3

3

It is possible to write CGI scripts with Ruby but this is generally not done because we have better solutions.

One file per page is not a very useful abstraction, it's just the one that PHP supports. Ruby has better ways to abstract a web application (like Sinatra, Rails, or even just Rack) so we prefer to use them.

Putting the file name in the url is another unfortunate side effect of PHP's design. It is implementation revealing and unnecessary (you're not getting a Ruby page, you're getting an HTML page), so we choose not to do that either.

CGI and FCGI in Ruby are also slower than the other solutions. This is not because of some limit on how performant they can be; it's mostly just because the effort to make Ruby web applications faster has been spent in more useful areas like Rack and Rails. No one really uses CGI so no one really cares to make it fast. mod_ruby makes CGI scripts somewhat faster if you really want to go this route, but again: there are better ways.

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks, @Rein, but it is not about good and bad, white and black, it is about howto. I understand all those side-effects and I know all those greate frameworks and I use them in my work. But I need to understand it from inside by writing my own dummy microframework. It can be small and with those side effects but it isn't importaint now: it is education, you know
CGI scripts are not a microframework. They are just an ad-hoc collection of CGI scripts. There is nothing "framework" about that. If you want to learn how to do something, why not learn the best way?
This is like training yourself to use an abacus. Sure, the abacus was a great calculating tool at one point, but we have better ones now.
No, you dodn't understand me. I am not just about CGI. I am about how to make it with Ruby. I can use CGI, or Rack or whatever. But I have got a goal and I need a start point.
Then I think you would be better served using an actual microframework like Sinatra.
|
2

Apache can run PHP by loading in the mod_php module.

I believe to run ruby you will need to have it installed on the server and have mod_ruby loaded into apache. take a look at: http://www.modruby.net/en/

Comments

0

You are looking for CGI. The Apache modules like mod_php or mod_ruby are just packaging provided for CGI scripts written in PHP or Ruby.

1 Comment

No, mod_php does not just wrap the PHP CGI binary. It's an apache module and works internally different than CGI/FastCGI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.