Padrino and Others: The Power of Ruby Frameworks
This article is an introduction to the power of ruby frameworks. It’s not always easy to pick the right framework for the right work, and in case of ruby there are many frameworks to choose from a good overview should help you to make the right decision.
Some of them are simple and others are complicated. Which one should I choose for a simple web application? I hope you will be able to answer this question yourself at the end.
Ruby is a powerful language. It has been there for a while; even so many people still don’t realize how useful and easy it can be. I’ve seen people in the development business switching to Ruby and it’s frameworks after programming for many years in different languages and I think there’s a reason for that. The creator of
Ruby, Yukihiro “Matz” Matsumoto, explained the heart of his language in one sentence:
“Ruby is simple in appearance, but is very complex inside, just like our human body”.
Java or C++ languages are complex to learn and write. You need to fight with their syntax instead using your time to resolve problems. That’s why I recommend having a look at Ruby and trying it! There’s no better way than getting your hands dirty in the code.
Have a good read and pick up the framework of your choice.
Let’s start this article with a short list of Ruby frameworks before we dive deeper into them. Discover and find them yourself on the web:
• Ruby On Rails – http://rubyonrails.org/
• Sinatra – http://www.sinatrarb.com/
• Pardino – http://www.padrinorb.com/
• Cuba – http://cuba.is/
• Cramp – http://cramp.in/
• Merb – http://www.merbivore.com/index.html
And others: Camping, Ramaze, Vintage, Halcyon, Nitro/Og, Wuby, Ruby Waf, Webby.
Prepare yourself for the battle!
First, you need to install Ruby. The best tool to use is
RVM – Ruby Version Manager. Use Linux or Mac OS as your operative system to avoid problems. Open up your terminal and install RVM first.
ami@os ~ $ \curl -L https://get.rvm.io |
bash -s stable –ruby
If installation and configuration were successful, RVM should now load whenever you open a new shell. This can be tested by executing the following command, which should output rvm is a function as shown below.
ami@os ~$ type rvm | head -n 1
rvm is a function
Finally, see if there are any dependency requirements for your operating system by running:
ami@os ~$ rvm requirements
Make sure that everything is installed before proceeding further.
Then install Ruby. The installation process is easy, you need to check which Ruby versions are available and install the latest stable one for best security and performance. You don’t need to specify its number: type in rvm install ruby-1.9.3 and the latest stable version will be installed. If the latest version isn’t available as a package for your distribution, Ruby will be installed from source as shown Listing 1.
ThE POWER OF RuBy FRAMEWORkS
Installation of Ruby – 1.9.3-p374- #complete
Use the newly installed Ruby:
ami@os ~ $ rvm use 1.9.3
Check if it works correctly by:
ami@os ~$ ruby -v
ruby 1.9.3p374 (2013-01-15 revision 38858) [i686-linux]
ami@os ~ $ which ruby
Optionally, you can set a version of Ruby to use as the default for new shells. Note that this overrides the system ruby:
ami@os ~ $ rvm use 1.9.3 –default
Once you got your Ruby installed, you can proceed with framework installation.
Ruby on Rails
To can install Rails, type the following command:
ami@os ~$ gem install rails
The three largest benefits you can expect with Ruby on Rails are:
Quicker launch Sites that would traditionally take 12 weeks to build can commonly be launched inside of 6 weeks with
Ruby on Rails. This timesaving results from several factors: a leaner code-base (fewer lines of redundant code), a modular design (re-using existing components rather than building everything from scratch), and the availability of existing plugins (again, reducing the need to build features from scratch).
Easier changes After the site launch, future modifications to your site (e.g., adding new features, making changes to the data model) can be applied quicker because of the reasons noted above.
Introduction to Ruby/RoR
Because of the speed with which Rails sites can be built and modified, you spend less money to create and maintain the website – without compromising its quality, performance, or scalability.
RoR has a few disadvantages as well:
• It’s good for small applications due to its being a heavy framework. It contains a lot of features you will never use.
• Static website. You can create a static website in Ruby on Rails but it’s not worth it. Make your life simpler and use Sinatra for this kind of job.
• Blog oriented website. Why reinvent the wheel? WordPress is the right tool to use. If you want to learn more about Ruby or Rails basic concepts, please refer the following links:
Listing 1. Installing Ruby
ami@os ~ $ rvm list known
# MRI Rubies
ami@os ~ $ rvm install 1.9.3
Searching for binary rubies, this might take some time.
No binary rubies available for: debian/wheezy_sid/i386/ruby-1.9.3-p374.
Continuing with compilation. Please read ‘rvm mount’ to get more information on binary rubies.
Installing Ruby from source to: /home/ami/.rvm/rubies/ruby-1.9.3-p374, this may take a while depending on your
ruby-1.9.3-p374 – #downloading ruby-1.9.3-p374, this may take a while depending on your connection…
ruby-1.9.3-p374 – #extracting ruby-1.9.3-p374 to /home/ami/.rvm/src/ruby-1.9.3-p374
ruby-1.9.3-p374 – #extracted to /home/ami/.rvm/src/ruby-1.9.3-p374
ruby-1.9.3-p374 – #configuring
ruby-1.9.3-p374 – #compiling
ruby-1.9.3-p374 – #installing
Sinatra is a very slim web framework; it needs way less memory at runtime than Rails. Also, since there’s less code involved, request processing is probably faster.
That’s why it can be a very appropriate framework for a web service “this simple”. Sinatra is great to use especially if you need to run many instances (e.g. high traffic or many long running requests), it can be an important factor to the number of machines you need to run your web service.
Sinatra is not, however, good for coding large websites as you would spend a lot of your time doing things, which are already implemented in Rails, like database connections.
Sinatra can be installed and run very easily: Listing 2.
Now open up your browser and paste the following
link to see the sinatra output.
To install Padrino, type the following command:
ami@os ~ $ gem install padrino
The best explanation of why you should use Padrino can be found on their website, its installation is explained above and it’s pretty simple. That’s how its creators describe the Padrino framework:
“Many developers fall in love with the simplicity and expressiveness of Sinatra but quickly come to miss a great deal of functionality provided by other web frame-works such as Rails when building non-trivial applications.
Our team has come to love the philosophy of Sinatra which acts as a thin layer on top of rack allowing middleware to do most of the work and enabling additional
complexity only when required. The goal for this framework is to match the essence of Sinatra and make it suitable for increasingly complex
Listing 2. Sinatra installation
ami@os ~ $ gem install sinatra
Fetching: rack-protection-1.3.2.gem (100%)
Fetching: sinatra-1.3.4.gem (100%)
Successfully installed rack-protection-1.3.2
Successfully installed sinatra-1.3.4
2 gems installed
ami@os ~$ touch hi.rb
Once you have created the hi.rb you can put some basic setup on it for sinatra to work.
ami@os ~$ vim hi.rb
<< START OF THE FILE >>
get ‘/hi’ do
<< END OF FILE >>
Once the file is saved. Run the command below
ami@os ~ $ ruby hi.rb
[2013-02-05 22:45:56] INFO WEBrick 1.3.1
[2013-02-05 22:45:56] INFO ruby 1.9.3 (2013-01-15) [i686-linux]
== Sinatra/1.3.4 has taken the stage on 4567 for development with backup from WEBrick
[2013-02-05 22:45:56] INFO WEBrick::HTTPServer#start: pid=30869 port=4567
applications that require the use of forms, mail delivery, localization, helpers, caching, etc. For our team, coding is an art form and Sinatra best enables this concept because of these core principles:
The Padrino framework is the perfect solution for your small projects as well as for your larger project requirements!”
Cuba was originally inspired by Rum, a tiny but powerful mapper for Rack applications. It integrates many templates via Tilt, and testing via Cutest and Capybara.
Introduction to Ruby/RoR
It can be installed by running the following command in your terminal:
ami@os ~$ gem install cuba
Cuba has a few advantages:
• Quick and simple
• Similar to Sinatra in some aspects
• Good for small websites with less complexity
And a few disadvantages:
• Small community
• There exist other better or similar frameworks
A different Ruby framework is Cramp, which is de- scribed in the following way:
• Lightweight, minimal and able to handle thousands of open connections simultaneously
• Built in support for HTML5 technologies: WebSock-ets and Server-Sent Events (EventSource)
• Easy Streaming APIs
• Allows Ruby 1.9 + Fibers to prevent asynchronous
• Seamless Active Record integration
• Rack Middlewares support + Rainbows! and Thin web servers.
Cramp’s advantages are:
• Great for working with large numbers of open connections
• Provides full-duplex bi-direction communication
• High performance
• It’s best for streaming actions of an application; other
actions can be handled with a different framework.
Cramp has a few disadvantages as well:
• Other frameworks perform better
• Small Community
• Lack of guides on the Internet.
that add in support for a particular feature rather than trying to produce a monolithic library with everything in the core.
Merb’s also thread-safe and was originally engineered to handle multiple file uploads concurrently.
On the Web
https://www.ruby-toolbox.com/categories/web_app_frameworks – Ruby frameworks popularity statistics
http://rubyonrails.org/ – Ruby on Rails
http://rack.github.com/ – Rack a Ruby Web Server Inter- face
http://cramp.in/ – Real-Time Web Application Framework
http://ruby.railstutorial.org/ – One of the best free tutori- als to learn Ruby on Rails
http://mislav.uniqpath.com/poignant-guide/ – For a bit of fun, a famous cartoon based on Ruby
It’s main advantages are as follows:
• Simple, well documented core
• Lots of features, flexible and extensible
• Test and Specs
• Very good documentation.
Merb has just one disadvantage and it’s the fact that it merges into Rails, so, after all, it’s better to stick with Rails.
There are many Ruby frameworks available now in the web and we can expect this list to grow in the future. It’s not easy to choose the best framework for your programming work. While some of them are suitable to use for simple websites, other will work for complex web- sites such as Twitter, which is actually based on Ruby on Rails. The best way is to give every framework a try and play with it. Try to make some simple websites and check their documentation. I’d recommend starting from a simple framework first (here Sinatra seems to be a good choice), and then progress to more complicated one. Ruby on Rails is the most popular Ruby framework these days, so there are many jobs available once you know your way around it. Eventually, the choice is yours.