Posted: Sat May 30, 2009 4:28 pm Post subject: Ruby as CGI Script
Is the speed decent? I've heard that Ruby is somewhat slow compared to other high level languages, and cgi causes problems because it needs to start a new process for each page request, but will it run at decent speeds for small sites, comparable to PHP?
Would CGI be better than SHTML with separate scripts generating each part of the page that need to be dynamic?
Sponsor Sponsor
rdrake
Posted: Sat May 30, 2009 4:36 pm Post subject: Re: Ruby as CGI Script
There's no getting around it, Ruby is ass slow compared to a lot of things out there. You can, however, use things like caching in order to improve performance by not rendering the page on every request.
You can...
Write CGI scripts in Ruby, it has built-in support for that.
Embed Ruby snippets in regular HTML, check out ERB.
Use FastCGI to host something like Mongrel so it doesn't need to spawn a new process every time.
Hopefully that gets you started.
wtd
Posted: Sat May 30, 2009 7:58 pm Post subject: RE:Ruby as CGI Script
Also, keep in mind your actual speed requirements, and that you may well be able to develop your CGI app significantly faster in Ruby than in another language.
For small sites, check out framework-ish stuff like Camping.
DtY
Posted: Sat May 30, 2009 7:58 pm Post subject: RE:Ruby as CGI Script
If I did go with plain ruby on just CGI, would it generally run as fast as PHP? People complain about php being too slow, however it's still used by virtually everyone on the internet, so it can't be a big deal.
I checked out fastCGI, but I didn't understand what it was doing differently than normal cgi, will it run the same cgi scripts, or do they need to be modified?
[edit] I'm checking out camping, that looks interesting, I haven't looked at all, but is hosting for camping easy to find? On the same note, is hosting that provides CGI and Ruby easily available (without renting/buying a virtual computer)
rdrake
Posted: Sat May 30, 2009 10:51 pm Post subject: Re: RE:Ruby as CGI Script
DtY @ Sat May 30, 2009 7:58 pm wrote:
If I did go with plain ruby on just CGI, would it generally run as fast as PHP?
Likely not, especially with plain CGI.
DtY @ Sat May 30, 2009 7:58 pm wrote:
People complain about php being too slow, however it's still used by virtually everyone on the internet, so it can't be a big deal.
It's easy to use, that's why it's popular. The fact that it can easily be hosted off of every free OS known to mankind also means it's cheap for hosting companies to provide hosting for it. It's everywhere, it's easy, and it's dirty - three things people love.
DtY @ Sat May 30, 2009 7:58 pm wrote:
I checked out fastCGI, but I didn't understand what it was doing differently than normal cgi, will it run the same cgi scripts, or do they need to be modified?
CGI spawns a new process for every request, FastCGI does not. It keeps a pool of processes which do the work, bypassing much of the overhead regular CGI has.
DtY @ Sat May 30, 2009 7:58 pm wrote:
is hosting that provides CGI and Ruby easily available (without renting/buying a virtual computer)
A lot of hosts now support Ruby. Look for hosting for Ruby on Rails.
gianni
Posted: Sat Jun 13, 2009 6:32 pm Post subject: RE:Ruby as CGI Script
You may want to check out some cool little ruby frameworks like sinatra. Which you could run with an Apache/Nginx + Passenger setup. Ruby will be more than fast enough for anything you want to do.
rizzix
Posted: Thu Jun 18, 2009 12:04 am Post subject: RE:Ruby as CGI Script
It's proven to be faster; however I don't think you can use JRuby in a CGI envorinment.
JRuby still has some compatibility issues too (e.g. gems, etc...).
Sponsor Sponsor
rdrake
Posted: Thu Jun 18, 2009 9:37 am Post subject: RE:Ruby as CGI Script
If you decide to use a Rack-compatible framework (Rails, Sinatra, etc.), you can also serve your scripts through Apache using mod_passenger.
If you need help setting it up just post back here.
DtY
Posted: Thu Jun 18, 2009 9:54 am Post subject: Re: RE:Ruby as CGI Script
gianni @ Sat Jun 13, 2009 6:32 pm wrote:
You may want to check out some cool little ruby frameworks like sinatra. Which you could run with an Apache/Nginx + Passenger setup. Ruby will be more than fast enough for anything you want to do.
Sinatra's looking pretty cool, I think that's what I'll use.
Thanks for the help everyone
rdrake: Thanks, I probably wont set it up on apache for a while, but I'll post if I can't figure it out
DtY
Posted: Thu Jun 18, 2009 5:05 pm Post subject: RE:Ruby as CGI Script
Okay, I've got some more questions now (though not specifically concerning Sinatra)
1) Is it safe to use Mysql? Do I create a new connection object in each handler?
I know there are libraries that handle databases for you, but I don't like those, it's extra stuff to learn when I already know sql much better than I'd ever know a database wrapper.
2) Can I make ERB templates include other files? Like for a standard header and footer I want on every page. (I'm used to PHP, so maybe there's a completely better way?)
gianni
Posted: Thu Jun 18, 2009 5:41 pm Post subject: RE:Ruby as CGI Script
I highly recommend using an ORM such as DataMapper to interface with your database. It will save you tons of time in the future (you have to resist your PHP temptations .
A better solution would be to do it in reverse. Have your layout file (with header and footer) and render your template in the middle. That prevents you from having to include the header and footer in each action. See the section "Named Templates" in the docs.
DtY
Posted: Thu Jun 18, 2009 8:13 pm Post subject: RE:Ruby as CGI Script
When I learned TurboGears (python framework) a few years ago, SQLObject looked like a good idea, but I never had any idea what I needed to do to get the stuff I need, whereas I knew exactly how to do it with an SQL Query. (I do know the security risks of a back door into an SQL query, but I do know how to prevent it (prepared expressions))
So do you mean something like this? (template contains the filename of the template to use):
Posted: Thu Jun 18, 2009 9:31 pm Post subject: RE:Ruby as CGI Script
Yea, it explains how to do it in the "Named Templates" section of the docs.
The cool thing about ORM's, is that you don't need to even think about databases, SQL, or other such things. You are basically working with ruby objects that can be persisted. So it's much more natural to interact with these objects. You'll also see the magic once you start defining relationships between these objects. For example, without even thinking about sql, you could do something like the following:
Ruby:
# You have a user object loaded from your database
user
# You want to find all the blog posts for the given user
user.posts
# Now you want to find the total number of comments on all of the user's posts
user.posts.inject(0){|m, p| p.comments.count + m }
DtY
Posted: Thu Jun 18, 2009 9:57 pm Post subject: RE:Ruby as CGI Script
Ah, I see, I'll see if I can do something similar with erb (I'm not liking haml)