Merb-tastic
posted by hampton on December 3rd, 2007

Merb is highly influenced by Rails. That might be an understatement. Anyone who knows Rails will quickly find Merb to be very familiar, but with a few key differences. Mostly, Merb is built around the philosophy of keeping it as light as possible. Don't come looking for AJAX helpers, ORMs built-in, large core extensions, or lots of the other cushy niceties that Rails provides by default. Most of these things can be installed into Merb quickly using gems, but they aren't in the basic Merb install.
This makes for a different feeling experience. Merb requires a little more legwork from you as the developer, but it also steps out of your way more often than Rails does. Merb is like Rails for those who want to hack around with more freedom.
I probably wouldn't recommend someone new to Ruby to start with Merb. Start with Rails for many, many reasons. Most of all, Rails skills are far more bankable than claiming Merb skills (for the time being at least). Most people don't know what Merb is. Also, Rails teaches you lots of good things. Its when you start to feel the edges of Rails that you want to push out of them and customize your framework more that Merb starts to become a good option.
Also, Merb is fantastically fast. The original idea behind Merb was to do Mongrel and ERB (M+Erb) together and make them work as quickly as possible. The inspiration was Rails for the way the original version was built. However, as time has gone on, the code has expanded into being a real development powerhouse for experienced Ruby developers. Did I mention that its fast as shit...? I don't find Rails prohibitively slow, but when you start to see how quickly Merb uses Ruby to produce HTTP requests my nipples start to get hard. I've seen 500 requests a second for a basic request on my commodity box. Checkout the benchmarks against Rails.
ORMs
Merb is ORM-agnostic which means that you can choose whatever ORM you want. Its easy to write ActiveRecord models just as easily as Rails, or you can experiment with some of the up-and-coming Ruby ORMs like DataMapper and Sequel.
DataMapper
Let's look at this example.
class Zoo < DataMapper::Base
property :name, :string, :default => 'Unknown'
property :notes, :text
property :updated_at, :datetime
has_many :exhibits
validates_presence_of :name
end
In this example, DataMapper sees that you want a property called :name when it is loaded and so it checks to make sure that the column is there. If it isn't it builds it for you. Did I mention that DataMapper is thread-safe? That means that Merb can run in all of its multi-threaded glory. I'll be honest that I haven't gotten to use this one much, but I know the developers are working hard to expand its feature base and have it in extremely active development. Read more about it here.
Sequel
Sequel is a type of ORM that I've never really come across before. It is focused much more on building queries than DM or AR are. You basically construct these objects using Ruby syntax that represent an un-executed query object. Let's build one here that will get us all of the users who are admins and in the order that they were created.
DB = Sequel.open 'mysql://root:1234@localhost/test_database'
admin_list = DB[:users].filter(:is_admin => true).order(:last_name)
Now, this is just a symbolic object at this point. Nothing has been executed. We can execute the command using Enumerable-style interfacing
admin_list.collect do |user|
"#{user[:last_name]}, #{user[:first_name]}"
end
#=> ["Bergin, Candace", "Ferrier, Emerson"]
Every time we execute collect on that object, we get a fresh new query with new data. So, Sequel isn't a replacement for DM or AR, but it does give us a really awesome and speedy way to interact with our database. I am looking to use this in some places where I need some super-fast and simple queries to be run in a lightweight section of one of my applications.
Views

dependency 'haml'
That should do it. You now have the full Haml/Sass suite running in your Merb application. Easy as your momma.
Et Cetera
Some of the other differences between Rails that I really like are....
- Mailers have their own directory in /app/mailers
- config/dependencies.rb is a nice place to declare all of your gem requirements instead of a messy plugin folder.
- If you "freeze edge" the framework puts itself into a folder called "framework"
- As long as you don't use ActiveRecord, you can use multi-threading.
- DRb is built in and easy as pie.
Installation
Its as simple as....
$ sudo gem install merb -y
but of course you'll also want to...
$ sudo gem install merb_datamapper datamapper haml erubis -y
Yup, that's it. Now generate a project this way
$ merb -g project_name
Are you sure you're getting this? I know its complicated. If you're scared just go back to PHP and hug your doll. Now, go dive into that and see what you can figure out with your Rails-fu. You'll be surprised at how familiar it feels.
Final Thoughts
Merb isn't some perfect "Rails killer". However, its a really interesting framework that will hopefully make Rails answer to some of its long-standing claims (performance isn't that important) and keep the Rails core team on their toes. Flat out, competition is good. Extra frameworks in Ruby doesn't mean that we dilute some special chemical in the community, but it means that we can start to focus on solving the problems for our applications and not making our applications fit our solution.
This is especially true if you are starting an application that you know needs to be high-performance. Merb is the place to go for that then. That's the case at Unspace right now. We are working on a project that requires unbelievably high uptime and request performance in order to be tenable. Rails just simply isn't the solution for that. Its why 37s wrote a C library to build Campfire with... because Rails is just not built for that kind of thing. Does that make Rails bad? No way! Rails is fantastic and will continue to be fantastic into the future. However, now there is a new kid on the block that is worth consideration for new projects where Rails isn't a perfect-fit.
Stay tuned for more from Unspace with Merb though. We've even already started patching Merb and we'll soon be announcing our new internal project that uses Merb to handle possibly millions of requests quickly. First we have to get the patent filed.
December 3rd, 2007 at 05:53 PM
You’re bang on when you describe how Merb is positioned in relation to Rails. I think some people might have the temptation to turn this into a fight over frameworks, but I think that should be avoided.
Competition A++
December 13th, 2007 at 09:05 AM
Did I mention ActiveRecord is thread safe;-) ? If someone differs, I’d love to see a piece of code which would speak louder than “Hey it doesn’t work” Et Cetera Et Cetera. However, thing to be noted is, if you’re using libmysql, only one ruby thread can use it at one point – thanks to green green threads. But DM is no different here, it’s a ruby limitation. And ruby mysql binding are way too slow.
I agree that Merb is a very good alternative and competition is always good. However, there are lies, there are goddamn fucking lies, and then there are benchmarks. Over the time, I just literary stopped doing benchmarks “Hello World” application. They’re just fooling. Usually, it’s always the application, where the perfomance cruft lies. So if Rails gives you 40 req/sec, I’d be surprised to see if the same app, when ported on merb, gives any more than 50 req/sec. You get the point. The difference is usually worth all the goodies which come along with rails.
January 3rd, 2008 at 10:15 PM
“ruby mysql binding are way too slow.”
Which DM doesn’t use, partly for that reason.
“there are goddamn fucking lies, and then there are benchmarks”
Yes and no. Contrived examples can be used to give a particular framework an advantage certainly, but they can still be done honestly and fairly. The “Lies and Benchmarks” makes for a nice bit of rhetoric, but to suggest benchmarks do not translate into real application performance is ridiculous. It show’s the base performance of the framework, which OBVIOUSLY affects application performance.
“Usually, it’s always the application, where the perfomance cruft lies”
Application performance which is limited by the base performance of the framework. Are you gonna say a 4 cylinder car can outrace a V12 as long as you drive it properly?
“So if Rails gives you 40 req/sec, I’d be surprised to see if the same app, when ported on merb, gives any more than 50 req/sec.”
There’s benchmarks, then there are arbitrary numbers. C’mon, don’t tell us benchmarks are lies then just pull some bullshit numbers out of the air.
February 19th, 2008 at 11:38 PM
I’m fairly sure you have absolutely no idea about how green threads work ( event based model for that matter ). You wanted a real benchmark, so here it is. Please compare it with “random” number I came up with.
April 4th, 2008 at 04:30 PM
dependency ‘ham’ should now be dependency ‘merb-haml’ and it requires you to first do
gem install merb-haml
or just install merb-more
gem install merb-more
May 23rd, 2008 at 09:48 AM
Well, I would like to point out to th readers that merb is changing very fast. So couple of steps in the article above are incorrect. 1. new application should be created with $ merb-gen app <app-name>
2. One step that will help you a lot is $ sudo gem sources -a http://merbivore.com
A more upto date article can be found at http://opensrcideas.blogspot.com/2008/05/merb-sherb-for-newbies-part-i.html
December 24th, 2008 at 07:45 AM
Rails 3 – Rails and Merb Merge! What you think about new logo? http://www.railsgeek.com/2008/12/23/rails-3-rails-and-merb-merge
June 10th, 2010 at 04:51 PM
Wo…Nice Blog, Nice tips, Great article!, I have recently started a blog, the information you provide on this site has helped me tremendously. Thank you for all of your time & work. Well Come Back To My Blog Ipad http://www.ipadzu.net
August 20th, 2010 at 01:16 AM
I really do not know what a merb is. Good thing this blog was posted. It has complete information but for other people they might immediately think that it is not really interesting.