Hacker News new | past | comments | ask | show | jobs | submit login
Sinatra - Hyper Fast Mini Webapp Development in Ruby (sinatrarb.com)
67 points by jmonegro on Sept 15, 2009 | hide | past | favorite | 44 comments



I'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.

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


What is your opinion on Sinatra's support community? I keep peeking at Sinatra for my new app. I have a Merb app in production and working on a new one. My new app has characteristics quite different than my old one but I still get to borrow my education from Merb. Unfortunately, Merb's google group is a ghost town these days. I worry this is going to get worse and have no interest in counting on Rails 3 being useful to me in the next year.


   be careful with ActiveRecord
What alternative ORM would you recommend that is mature enough?


I've heard good things about sequel, but have never used it personally: http://sequel.rubyforge.org/


I use Sequel when I am doing projects in Sinatra. It's elegant.


The sequel approach is unique. I worked with it a year or so ago and was happy.


Datamapper (http://www.datamapper.org) is a great alternative as well.

It's not as full-featured as ActiveRecord, but then it's also not nearly as heavy!


"I'll come out of the closet about this and just say what everyone knows: If the DataMapper community had more of a focus on addressing known, ticketed, reproducible bugs and less on pouring thousands of man-hours into writing specs it would be usable today. Frankly, I would recommend against considering it for your next project in the strongest terms possible.

Considering I created the project, nursed it through it's first couple years of development, and invented patterns like Strategic Eager Loading and Lazy Loading Contexts, which no other O/RM has to this day to my knowledge, that should say something. It's an epic fail of a project at this point."

-- Sam Smoot

http://robots.thoughtbot.com/post/162764036/why-do-rubyists-...


wow, that's a tough quote from Smoot. I use datamapper for an app in production and have no problems, but to be fair, my RDB access is very straightforward and I don't use any of the relations DSL stuff.

Although I think a lot of Sam's work, his boast of inventing patterns of "Strategic Eager Loading and Lazy Loading Contexts" is hard to take. I wrote stuff like this in Samlltalk in the early 90s and again in Java in the late 90s.

From my perspective, I don't trust any of the ruby ORMs. I constantly watch the SQL they generate thinking any day I'll catch an error. This isn't to berate the developers, but to acknowledge that the approach to solving this problem domain is too "magical" for my tastes. This is one reason with my new app I'm going with mongodb. I get a less trusted DB, but my ruby code accessing it is closer to the metal.


On the "boast", that probably came off a bit strongly. I'm pretty much a dick online. And in person my friends tell me... :-D

Anyways, yes, OK, nothing new under the sun. I don't doubt better programmers than me did this awhile back. I'm just not aware of another OSS or Commercial O/RM that provides it, and I'd never seen it before.

Also, I can't take credit for actually _implementing_ the Lazy Loading Contexts... that was someone else. Really smart guy, but he dropped out of the project eventually.

DM is just crazy sometimes, but with 0.10 Dan says that's changing. I'd trust it again when someone big deploys and raves about it, I just don't have any motivation to investigate it personally. I think there's better things to spend your time on considering Sequel is around. Why fight it?

I took code from Sharon early on (connection pooling), he took code from me (block query syntax), Sequel is kinda the best of both worlds. I've never thought building an O/RM on top of a Table Data Gateway was a good idea, but that's mostly a performance concern, and Sequel's performance is right up there so does it really matter?

My only concern with Sequel is that last I looked it's not as robust as Hibernate when it comes to aliasing tables. But then what is? It's probably a rare enough issue that you can just get around it with the sproc or ad-hoc query support once or twice in a given large app.


Hi Sam!! Hope you didn't find my post critical of you. It wasn't my intent. I'm very grateful for the high quality work that is/was datamapper and for your support while I was learning it.

All good programmers boast about a thing or two they "invented", me included. I wouldn't have it any other way. ;)


Jon, no offense at all.

I just looked at your site and realized you were the guy I've been feeling sorry for all this time since you were one of the early production adopters. I'm glad to hear it's been a pretty smooth experience. I can stop carrying around that guilt. ;-)

Oh, and since this thread is about Web Frameworks, try Harbor (http://wiecklabs.com). :-D

Lesson learned about being to vocal/open too early, but Harbor and the related projects are seriously good stuff if I do say so myself. Also look for a major open-sourcing of a user-management/authorization system built on Harbor in the next day or two at http://github.com/wiecklabs.


Harbor look cool. I'll keep an eye on it!!


There are responses from dkubb and wycats, and the conversation shows it's not that simple and that there are other perspectives, as is often the case.


Wycats is in marketing. His biggest contribution was inventing the term "Strategic Eager Loading". He made no significant code contribution.

True, he did generate a SWIG wrapper to create the first prototype of DataObjects after a conversation we had where I suggested Ruby would be a better place with an ADO.NET-like database interface... but none of that was really usable since you had to have the exact same MySQL version down to revision, on the same system. The only other major contributor to DataObjects outside of the Wieck team I recall (and apologies if I missed anyone) was Dirjan Bussink fixing my broken Connection Pool. Later contributors (and Dirjan) ended up rewriting a lot of it for Asynchronous Execution, more driver support, etc. But that's the reason the thing looks so much like ADO.NET.

Oh, and Command#set_types was me too. Which is the reason DO is the fastest (last I bothered to look) database API for Ruby.

The marketing contribution was HUGE. It always surprised me how many new people showed up after one of Yehuda's presentations. DM wouldn't be what it is today without Yehuda. But your own opinion on DM's code is about as valid as Yehuda's. Just look at the commit-history.


I love that Datamapper will build a working database from nothing just given the class definitions; no migrations, no schema definitions, just a URL to the database instance. Database access with Datamapper is completely frictionless, unlike with AR.


It really depends on your needs. My comment was in regards to the memory footprint. There was an Engineyard blog post about that recently. Basically if you do some non-obvious things, you can just explode out the size of ram, and because of how ruby works, it'll never get given back to the OS.

Edit: http://www.engineyard.com/blog/2009/thats-not-a-memory-leak-...


Poor use of an ORM (or pretty much any robust full-featured library) has its risks. I'm not sure how you provide features that AR includes without putting your memory at risk of overuse.

Model.find(:all) is going to be problematic across the board, there will always be a risk when you start playing with things like including children (there's a great benefit as well). You need to know what you're doing with any ORM.


Sinatra is a great micro framework for when you don't need the whole Rails stack, I used it for building http://tweetarium.com along with tokyo cabinet.


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


I'm curious what issues you've run into with regards to memory footprint. I've not seen anything like that myself.


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


I 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


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


While 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!


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


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


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


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


I use Haml and Sass. I had to "roll my own" partials --- which is to say, I wrote a 1 line convenience function to set "layout => false" on the call to haml().

By default, Sinatra will read views out of the views/ directory, but I try to keep things in the files, so each area of functionality in the app is self contained.


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


Another thing I love about Sinatra is that instead of controller/model/view files, I can do users/posts/comments, with one file for each functional area.


Interesting.

So far I see nothing that makes Sinatra more appealing or useful than Ramaze, and some thing that make it less so.

The curious thing is that on the one hand Sinatra gets described as a "framework", and on other other hand there's a great deal of BYOC (bring your own code).

That's fine, and something I tedn to prefer, but if I really want lightweight microstuff I'd start with a plain Rack app, and if I need more I'd convert it into a Ramaze app.

I do like how Sinatra auto-maps the REST verbs, but basically it comes off more as Rack middleware with a cheering section than an actual Web framework, micro or otherwise.

(also, Hey Chris!)


I have no horse in this race, I'm just using and liking Sinatra as a "sandbox framework" to go along with our Rails deployment. So, with that said:

Sell me on Ramaze. I love that I can whip out a 2-file application in Sinatra --- one file for the Ruby and templates, and another for the jQ js that'll run on the client. What does Ramaze do better than Sinatra for me?


". I love that I can whip out a 2-file application in Sinatra --- one file for the Ruby and templates, and another for the jQ js that'll run on the client."

Yeah, I love that about Ramaze as well. :)

"What does Ramaze do better than Sinatra for me?"

Appears to be faster (at least in some simple benchmarks I ran). Choice of templating language may be key.

Has a better-sized set of robust standard helpers, should you want to load them.

More built-in adapters for a wide variety of template engines (Haml, Ezamar, Liquid, Markaby, others)

Unbeatable community on irc (#ramaze on freenode)

Way cooler shirts and coffee cups: http://www.cafepress.com/rubystuff/4904578 ;)

Whether or not any of this works out as "better" for anyone is hard to say. I like that Sinatra doesn't dump a ton of must-load crap on you just to get started, but neither does rolling a plain Rack app. Or Ramaze, for that matter.

It's also nice to have a reliable set of built-in libs to pull from, and as best I can see there's a stronger choice in Ramaze.

But, to be honest, Sinatra just never showed enough value to me to dig into all it's possibilities, so I may be wrong in my observations.


Thanks. Helpful! Appreciated.


I 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/ 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!


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


Weird, I get redirected to http://www.sinatrarb.com on safari but not FF. Why would that be?


I'm on Safari (4.0.3) and I'm getting "Failed to open page". Had to manually add the "www.".


Because safari automatically tries www.domain.ext if domain.ext fails?


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


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


It seems only www.sinatrarb.com has a cname to sinatra.github.com




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: