5 ms·
Sinatra - Hyper Fast Mini Webapp Development in Ruby
- tannerburson 17y agoSinatra is great. I've been working with it for the last eight or nine months on various projects and I'm extremely glad about that choice. But I wouldn't call it "hyper fast" in any sense. From a raw performance perspective, it's faster than Rails, but it has some issues with it's design that can cause extremely bloated memory footprints. From a developer sense, it doesn't come with a lot (hardly any) of helpers, generators, or other fancy tools. So depending on your use case you could end up re-inventing some wheels. It's biggest advantage is that it's small, lacks a lot of magic code, and doesn't force you into a specific structure. This makes it great for web services, small apps, or even small components of larger apps. But like anything else it's definitely not a silver-bullet. By design it doesn't have a lot of helpers, generators, or other tools. Depending on your usage this could mean a lot of re-inventing the wheel.
- tibbon 17y agoWhile it doesn't have a lot of helpers or other tools that you're used to from Rails- you can always include ActiveSupport which brings many of those in and away you go!
- tannerburson 17y agoWell that gives you some of the extensions, but it doesn't give you a lot of things on the 'view' end of the equation. You're handling your forms yourself, you're handling URL generation yourself, etc. Of course those are the things that attracted me to Sinatra, so I don't see them as problems so much as things to be aware of.
- cschneid 17y agoI'm curious what issues you've run into with regards to memory footprint. I've not seen anything like that myself.
- tannerburson 17y agoThe issue is with subclass style applications. If you use multiple sublcass apps mounted at different URLs via Rack you incur the full overhead of the framework for each subclass instance. It's not a bug, it's just the way it's designed.
- ichverstehe 17y agoI heard this one before, and for all I can tell it's simply not true. What's your setup? I have tried some simple benchmarks against two apps. One with a single subclass, and another with 3 subclasses. The one with a single subclass used 22M (RSS), the one with 3 subclasses used 23M (RSS). This was running it with thin. The code is here: http://gist.github.com/187475 http://gist.github.com/187475
- tannerburson 17y agoI don't have such a simple example handy, but it's not drastically different from your setup except I subclass an Application class, which subclasses Sinatra::Base. I'm loading about 10 subclasses, not 3. I don't have the numbers handy but when I last tested it, adding subclasses did add several MB of memory per instance.
- tibbon 17y agoI used Sinatra as my learning tool for getting into Ruby instead of Rails. Rails was frankly daunting. I had never done web programming, only some C in high school. I didn't like that typing 'script/generate' threw up 46 files, each with their own purpose and use. Even with books and tutorials I didn't quite know where to start, or how to do what I wanted to do instead of what the tutorial wanted me to do. Sinatra was great however. Everything (by default, I realize you can move beyond this) was in a single file that I could glance at, find my errors and correct. The downside mainly was that when looking for help I kept finding Rails-specific stuff, which wasn't useful (trying looking up Ruby stuff without encountering Rails, or accidentally filtering every decent page out of search). The documentation for Sinatra could certainly be improved and I'd love to see a book on it. One of the first Sinatra tutorials you used to run across on Google (was on http://www.xnot.org/ http://www.xnot.org/ but is now removed) didn't work with current versions of Sinatra and produced very odd errors. Slightly frustrating to a noob when the documentation isn't marked with a date or the fact that it was only to the alpha versions. Overall Sinatra is amazing for bashing out really quick and simple web interfaces in about 10 minutes. Rails is just too heavy oftentimes. I might just need to make a page that allows someone to see something that Ruby is pulling from a database- and that's it!
- tptacek 17y agoOur product is built on Rails, but I do almost all my development in Sinatra now. The biggest win for me is that Sinatra strips away everything that would allow you to procrastinate in Rails (such as figuring out whether your actions are RESTful enough, or making sure you're serving all the content types, or making sure you've balanced code properly between controllers and models). There is nothing to do in Sinatra except to get to work on your application, which is a good thing. I highly recommend Datamapper over ActiveRecord, too; between Sinatra and Datamapper, you can literally pop open a single file and have a complete working application when you exit the editor, with no external setup steps.
- jamesbritt 17y ago"There is nothing to do in Sinatra except to get to work on your application, which is a good thing." What does Sinatra use for layouts and views and partials by default? Last I looked it appeared to be .... nothing. It seems then that for anything non-trivial you're either inventing a view system or tacking one on. Am I missing something about this? Among the things I like about Ramaze is that, like Sinatra, I can do the all-in-one-file web app if I like, but if I decide to refactor to layouts and views and such there's an obvious, ready path. Ramaze lets me evolve smoothly from dead-simple to high sophistication, and I don't see that with Sinatra.
- weavejester 17y agoYou can use whatever view-building libary you wish with Sinatra. Last I looked, Sinatra had convenience functions for using Erb and Haml, but there nothing stopping you from using something less well known, such as Tagz.
- cschneid 17y agoSinatra has built in handlers to render erb, haml, builder, and sass. Layouts are handled by automatically grabbing the file layout.erb (or layout.haml, etc). Partials are a bit of a pain, since you have to write your own helper, but at it's core, you're just calling the same erb helper, but with :layout => false. It's downright trivial to add your own rendering helper for any templating language. And for organization purposes, it's really freeform. Act like you're writing any other ruby app, and organize how you like. If it's small, a single file makes sense, if it's medium sized, maybe a file for controller and one for models, for big, do the split into an app/{routes,models,views}. All of that is easily supported. Check out Monk as a good example of how Sinatra can be adapted to a more complex directory structure. (also, Hi James!).
- cschneid 17y agoI've been using Sinatra for more than a year now. It's the perfect mini framework in Ruby. It helps you get web dev done, without ever getting in your way. I've written a blog with lots of documentation at http://www.gittr.com http://www.gittr.com. In addition, coworkers of mine at Citrusbyte have taken Sinatra and built a minimal, yet more full featured web-stack called Monk (http://www.monkrb.com http://www.monkrb.com). Basically it takes the Sinatra library and wraps it up with convenient conventions, libraries for testing, settings files. I've used Sinatra for small projects like Irclogger (http://www.irclogger.com http://www.irclogger.com) (http://www.github.com/cschneid/irclogger http://www.github.com/cschneid/irclogger). You can take a look at the code and see how simple it is underneath. I've also used Sinatra for larger projects involving many files, user login and authentication, database access, and more. What you'll find when you're writing Sinatra is that almost nothing is provided in the way of helpers, but also that you won't miss them. You write your own helpers where you want, and use normal rubygems to provide other functionality. It's not really a web framework's role to render JSON when there's a perfectly normal JSON gem that does it well. On the other hand, various people have recreated the small helpful stuff in reusable forms, including fairly simple things like partial rendering, up to reverse URL lookup (like Rails provides person_edit_path(), etc). I'm not sure what the other commenter is talking about with bloated memory footprint for Sinatra, it's a fairly minimal piece of Ruby code, and any memory bloat will be the fault of your code, and not the framework itself. A hello world app in Sinatra takes about 10 megs of ram to get up and running (under Thin in my case). Rails takes about 40 last time I checked. Everything from there is related to your app (be careful with ActiveRecord, it'll chew through ram). To sum up: Sinatra is a do it yourself framework toolkit. It doesn't provide much beyond making it easy to build your own helpers and your own application specific code. If you need help or advice, swing by #sinatra on freenode.org. People are always around to help.
- lzell 17y agoWeird, I get redirected to http://www.sinatrarb.com http://www.sinatrarb.com on safari but not FF. Why would that be?
- tome 17y agoBecause safari automatically tries www.domain.ext if domain.ext fails?
- lzell 17y agoOh I wrote that backwards by accident. But I'm sure you are right. That shouldn't be the browser's responsibility (although it is nice that FF does it for you). I thought it was a dead link.
- mr_justin 17y agoI'm on Safari (4.0.3) and I'm getting "Failed to open page". Had to manually add the "www.".
- tvon 17y agoI get a 404 on the submitted link. sinatrarb.com points to 208.67.217.132 which does not seem to have http running (at least not on 80). www.sinatrarb.com points to 65.74.177.129 (which is the sinatra website) That, and sinatra has been around for years, but I guess by the discussion nobody cares if it's not new...
- bittersweet 17y agoIt seems only www.sinatrarb.com has a cname to sinatra.github.com
- idm 17y agoThis reminds me of the Perl Catalyst approach, which states that everything important already exists in CPAN, so the web framework just needs to provide a little glue to stick it together. In other words, there's massive reuse of CPAN components with Catalyst, as the framework itself doesn't provide much for free. The sinatra "hello world" is insanely compact, and really says something... Here is one point where Catalyst is different, to put it politely. On a side note, I got so frustrated with Catalyst that I now use Django.