RoR, Sinatra, Padrino and Others: The Power of Ruby Frameworks

RoR, Sinatra,

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.

Getting started

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

Using /home/ami/.rvm/gems/ruby-1.9.3-p374

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

/home/ami/.rvm/rubies/ruby-1.9.3-p374/bin/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

Using /home/ami/.rvm/gems/ruby-1.9.3-p374

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

More cost-effective

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:

http://guides.rubyonrails.org/

http://www.rubyonrailstutorials.com/

Listing 1. Installing Ruby

ami@os ~ $ rvm list known

# MRI Rubies

[ruby-]1.8.6[-p420]

[ruby-]1.8.7[-p371]

[ruby-]1.9.1[-p431]

[ruby-]1.9.2[-p320]

[ruby-]1.9.3-p125

[ruby-]1.9.3-p194

[ruby-]1.9.3-p286

[ruby-]1.9.3-p327

[ruby-]1.9.3-p362

[ruby-]1.9.3[-p374]

[ruby-]1.9.3-head

[ruby-]2.0.0-rc1

ruby-head

….

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

http://railstutor.com/

Sinatra

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.

http://localhost:4567/hi

Padrino

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

require ‘rubygems’

require ‘sinatra’

get ‘/hi’ do

”Hello Sinatra”

end

<< 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:

• Clean

• Compact

• Fast

• Creative

• Concise

The Padrino framework is the perfect solution for your small projects as well as for your larger project requirements!”

(source: http://www.padrinorb.com/pages/why)

Cuba

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

Cramp

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

callbacks spaghetti

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

Merb

Like Ruby on Rails, Merb is an MVC framework. Unlike Rails, Merb is ORM-agnostic, JavaScript library agnos- tic, and template language agnostic, preferring plug-ins

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

(Source: http://cramp.in/)

It’s main advantages are as follows:

• Speed

• Simple, well documented core

• Lots of features, flexible and extensible

• Generators

• Test and Specs

• Plugins

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

Summary

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.

More than 8 Color Vim Syntax Highlighting in GNOME Terminal

http://www.alfredrossi.com/?p=49

More than 8 Color Vim Syntax Highlighting in GNOME Terminal

filed in Uncategorized on Jun.27, 2009

Matt Wozniski wrote an awesome little Vim plugin called CSApprox that lets Vim transparently apply GVim colorschemes in the terminal. CSApprox works by approximating the color in the GVim colorscheme to the closest availble color in your terminal, provided that your terminal supports either 88 or 256 colors.

After downloading the plugin zip and extracting it to the ~/.vim directory, I was met with a rather disappointing message:

CSApprox skipped; terminal only has 8 colors, not 88/256
Try checking :help csapprox-terminal for workarounds

Don’t worry, GNOME Terminal isn’t actually limited to 8 colors, it just evades detection. We can easily force 256 color support by setting the t_Co option in ~/.vimrc .

1
set t_Co=256

Super Nice Vim editor Configuration for MacOS and Linux Ubuntu Mint not only for Rails

Source: http://vim.spf13.com/

Installation easily installed on *nix, os x and windows

Easy Installation *nix and os x

The easiest way to install spf13-vim is to use our automatic installer by simply copying and pasting the following line into a terminal. This will install spf13-vim and backup your existing vim configuration.

If you are upgrading from a prior version (before 3.0) this is also the recommended installation.

curl http://j.mp/spf13-vim3 -L -o - | sh

Updating to the latest version

$HOME/to/spf13-vim/
git pull
vim +BundleInstall! +BundleClean +q

Installing on Windows

On Windows and *nix Git and Curl are required.

Installing dependencies

Install msysgit

After installation try running git --version within command prompt (press Win-R, type cmd, press Enter) to make sure all good:

C:\> git --version
git version 1.7.4.msysgit.0

Setup Curl

Instructions blatently copied from vundle readme Installing Curl on Windows is easy as Curl is bundled with msysgit! But before it can be used with Vundle it’s required make curl run in command prompt. The easiest way is to create curl.cmd with this content

@rem Do not use "echo off" to not affect any child calls.
@setlocal

@rem Get the abolute path to the parent directory, which is assumed to be the
@rem Git installation root.
@for /F "delims=" %%I in ("%~dp0..") do @set git_install_root=%%~fI
@set PATH=%git_install_root%\bin;%git_install_root%\mingw\bin;%PATH%

@if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%
@if not exist "%HOME%" @set HOME=%USERPROFILE%

@curl.exe %*

And copy it to C:\Program Files\Git\cmd\curl.cmd, assuming msysgit was installed to c:\Program Files\Git

to verify all good, run:

C:\> curl --version
curl 7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8k zlib/1.2.3
Protocols: dict file ftp ftps http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp
Features: Largefile NTLM SSL SSPI libz

Installing spf13-vim on Windows

The easiest way is to download and run the spf13-vim-windows-install.cmd file.

Excellent Configuration A highly optimized .vimrc config file

The .vimrc file is suited to programming. It is extremely well organized and folds in sections. Each section is labeled and each option is commented.

It fixes many of the inconveniences of vanilla vim including

  • A single config can be used across Windows, Mac and linux
  • Eliminates swap and backup files from littering directories, preferring to store in a central location.
  • Fixes common typos like :W, :Q, etc
  • Setup a solid set of settings for Formatting (change to meet your needs)
  • Setup the interface to take advantage of vim’s features including
    • omnicomplete
    • line numbers
    • syntax highlighting
    • A better ruler & status line
    • & more
  • Configuring included plugins

Customization

Create ~/.vimrc.local and ~/.gvimrc.local for any local customizations.

For example, to override the default color schemes:

echo colorscheme ir_black  >> ~/.vimrc.local

For my personal use…