A Ruby on Rails glossary.

So it’s been about a year since I started working on the Ruby language in the context of the Rails framework. It’s been awesome,  the combination of Ruby’s developer friendly language style and the incredible productivity of the Rails framework makes this a killer framework for web applications. Building the basic code is simply a matter of typing in some commands that generate all the scaffolding of code needed for a basic website. The rest is usually a matter of tweaking and adding to the generated scaffold.

The Evolution of Computer Programming Languages

Image by dullhunk via Flickr

Like every web technology the Ruby on Rails framework has its own set of idioms, that one needs to get familiar with and since I am new to Ruby, for me this includes some Ruby idioms as well. I saw a post today that lists a glossary of Rails terms and I thought I’d do my own. So here goes –
  1. Application ServersThin, Mongrel, Passenger, UnicornPow – These are application servers that run the Ruby web application and respond to the requests. They are generally integrated with a web-server like Apache or Nginx, though Mongrel is technically capable of running on its own as a web-server (though since it is not multi-threaded this configuration is not useful for anything except for very light loads or on a development machine).
  2. Active Record – Active record is the ORM tool used in the Rails framework to abstract the database handling. It is an implementation of the active record architectural pattern described by Martin Fowler thus – “An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.” The Active Record code is available as a separate gem that is downloaded as a dependency to Rails.
  3. Active Resource – A simple way to think of active resource is that it’s like active record but for REST based web-services. Active record provides a wrapper around REST actions providing a transparent proxy between a business entity and REST based web-services.
  4. Active Support – This is a set of utility classes and standard library extensions that are useful in Rails application development. It includes classes for dealing with caching, dates, time zones, text, etc. Like active record and active resource, active support is available as a separate gem that is installed as a dependency of the Rails gem.
  5. Bundler – Bundler manages the gem dependencies of a ruby application in a repeatable way across multiple machines. It automatically looks for and installs / updates all the gems needed by the application when it is deployed on a machine. It can do this is a repeatable way across multiple machines and deployments.
  6. Capistrano – Capistrano is a tool for executing command on groups of servers over SSH. It is used mainly in deploying web applications and is capable recognizing different types of server environment (like staging and production, etc).
  7. Capybara – This is an advanced version of the Webrat tool. It lets you do acceptance testing of applications using various drivers (Selenium and Rack are built-in) by simulating a real user hitting your web application.
  8. CoffeeScript – Coffee-script is a little language that compiles into java-script. It has rapidly become very popular and has been included in Rails 3.1 as the default method of writing JavaScript code in a Rails application. I am personally a bit on the fence here but I like its terse syntax.
  9. Cucumber – Cucumber lets software development teams describe how software should behave in plain text. The text is written in a business-readable DSL and serves as documentation, automated tests and development-aid – all rolled into one format. Cucumber along with RSpec are main tools used for the BDD methodology favored in many Rails projects.
  10. DSL(Domain Specific Language) – A domain-specific language is a language dedicated to a particular domain or the representation of a particular problem or solution. DSL’s are more common than you think, some good examples in the Ruby being – Cucumber, Rake, Haml etc.
  11. ERB (Embedded Ruby) – ERB is a simple and powerful templating system for Ruby. It is part of the Ruby core and it allows one to place ruby within other files (any text document).
  12. FactoryGirl – This is a gem that can be used for creating object factories in an automated fashion. This is useful in testing or even for populating an application when deployed
  13. Gem – At its most basic form, a Ruby gem is a package. It has the necessary files and information for being installed on the system. There is an online repository for Ruby gems at http://rubygems.org/ For me this is the biggest find and literally the “hidden gem” of my Ruby learning experience has been – Ruby Gems.  It’s like having an app-store for your development needs, where most of the best gems are freely available to you!  The number of awesome, useful gems actually available to ruby developers means that we have a huge advantage over other frameworks. A testament to the power of Ruby gems has been that Microsoft has started its own package management library – Nuget which, seems to be simply a straight lift of the Ruby Gems concept, except of course they have incorporated it into the Visual Studio as an extension. Nuget was released by the same team that created the Microsoft version of Rails – ASP.Net MVC so you can see the Rails influence…
  14. Gherkin – Gherkin is a DSL  that emerged from the Cucumber project. It was designed to succinctly describe behavior in a form that can be acted upon by a tool like Cucumber. It is described as business readable DSL and can be used in any project that has need of such a tool. The lexer and parser for the language has been create and the library is available in several frameworks including Ruby, JVM, Javascript (Node.js) and as a .NET dll.
  15. Git – Git is a free and open source, distributed version control system. It was written by Linus Torvalds to replace the old source control system for Linux and has become one of the tools of choice for a lot of Ruby projects. Git is typically used along with Github which is an online source control repository that leverages Git.
  16. Haml – Haml is an HTML abstraction language. Haml functions as a replacement for inline page templating systems such as PHP, ASP, and ERB, the templating language used in most Ruby on Rails applications.
  17. Heroku – Heroku is a cloud based platform as a service (PaaS) which was one of the first to have Ruby and Rails support. One could build a Rails application and directly deploy it on Heroku without having to set up a web-server. The platform has evolved to support multiple languages including Java, Scala, Node.js, Clojure and Python.
  18. jQuery – jQuery has become the JavaScript framework of choice for web developers everywhere. It is a fast, framework that leverages CSS style selectors to allow developers to traverse, animate and handle events in the HTML DOM while abstracting browser specific quirks as far as possible. It has become so popular that it displaces Prototype as the default JavaScript framework used in Rails 3 applications.
  19. Memcached – A distributed open source caching system, memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. It is a generic store and can be used in any situation where caching is needed and is available in multiple languages and technologies. It is used in a lot of Rails applications even though today, other object stores like Redis are getting popular.
  20. Merb – Like Ruby on Rails, Merb is an MVC framework. But, it a lighter and more barebones version. I haven’t used Merb – I’d love it if someone could give a more apt description of what Merb is and how it compares to Rails and Sinatra.
  21. Open classes / Monkey Patching – Open classes is a feature of the Ruby language where you can simply open and add methods to existing classes. Active support library uses this feature to add a lot of its helper functions on the Ruby core classes like string. A popular name for this particular practice is “Monkey Patching”.
  22. Paperclip – Its a complete (and easy) file attachment library for active record. It provides validation features based on the properties of the file like size, type, etc. The files themselves are saved in the filesystem. It has some basic image manipulation features as well, like creating thumbnails etc. There is a dependency on the ImageMagick library (an awesome image manipulation library) in order to use Paperclip.
  23. Prototype – Prototype is a JavaScript Framework that aims to ease development of dynamic web applications. It was originally the default JavaScript framework bundled with Rails.
  24. Rack –  Rack is a gem that provides a simple interface between a web-server and the Ruby web framework. Rack is used in various Ruby web frameworks, like Rails, Sinatra and Merb.
  25. Rake – Rake is the ruby version of make but much more developer friendly. It provides capabilities that are similar to make but it uses the Ruby syntax instead of XML files or makefile syntax to define tasks.
  26. REST(Representational State Transfer) – REST is a set of principles that define how Web standards, such as HTTP and URIs, are supposed to be used. Well, actually REST as a concept is more abstract than that – its architectural style that can be applied to distributed systems. REST in the context of Rails, mainly is used to refer to the way Rails exploits REST concepts in the way that Rails web applications are structured.
  27. RJS(Ruby JavaScript) – It is a ruby helper used to generate JavaScript that are used to decorate HTML pages. It is used mainly in AJAX calls, and in situations where you wanted to conditionally emit JavaScript. The general opinion though was that it sucked and was avoided in favor of the unobtrusive style of jQuery.
  28. Routing – Routing is the aspect of the Rails web application that deals with the translation of a URL request to a specific action in the application. In Rails, it is the job of the router to recognize URLs and dispatch them to a controller’s action. It can also generate paths and URLs, avoiding the need for hardcoded strings in your views.
  29. RSpec – RSpec is a Behaviour-Driven Development tool for Ruby programmers. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD. Like Cucumber RSpec uses a DSL to simplify design and implementation tests.
  30. Ruby Debug – Just like it says on the tin – it is a debugger for ruby applications. Like the other tools it is gem that can be installed and can run your ruby application in debug mode.
  31. RVM(Ruby Version Manager) – This is an awesome tool from Wayne Seguin that is used to install, manage and work with multiple versions of the Ruby programming language on your machine. It even allows you to segregate your gems so you could work with multiple versions of the same gem for different applications.
  32. SASS & SCSS – These are CSS abstraction languages (I believe SCSS is a reference to the version that supports CSS3). They are compiled down to CSS. They allow you use variables, modules and include other files. I am not very familiar with them and would welcome more information from readers.
  33. Sinatra – From the website – “Sinatra is a DSL for quickly creating web applications in Ruby with minimal effort.” Like Merb, Sinatra is a minimalist web framework. Again, like Merb, I haven’t used Sinatra at all and would welcome some comments to flesh out this entry.
  34. Test::Unit – This is the default testing library available with Ruby. It is from the xUnit family of testing frameworks and is a part of the core Ruby libraries. It is mostly not used in favor of more powerful BDD style tools like RSpec.
  35. UJS(Unobtrusive Javascript)– “Unobtrusive JavaScript” is a general approach to the use of JavaScript in web pages. The general principles of this approach includes –
    1. Separation of behavior from the markup – The HTML in a document is meant to describe the structure of the document and should not contain programmatic elements in it. This is similar to the principle of separation of the structure from the presentation of the document when talking about the separation of CSS and HTML.
    2. Should not pollute the global namespace – UJS code should add as little as possible to the global object or global namespace of the environment in which it runs.
    3. Degrade gracefully – If the browser rendering the document does not have the capability needed the code should be able to degrade gracefully without breaking the rest of the application.
  36. Vim – IMO Vim, the text editor that just celebrated its 20th anniversary in 2011 has experienced a resurgence of sorts with the rise of Ruby and Rails. In fact there is a large number of Vim plugins that have been created to make developers even more productive and happy on this text editor. Indeed after Textmate (the other favored tool for Rails developers) I think Vim has the biggest following among Rails developers and the fact that its available even on Linux and Windows only makes it cooler…
  37. Web ServersApache, nginx, Lighthttpd , Thin – In the Rails world web servers options include Apache, nginx and lighty(lighthttpd) of which Apache is by far the most popular. I have included Thin in this list as well since it includes both a web-server and an application server. With advent of PaaS providers like Heroku, this particular aspect of web application development is becoming abstracted out, just as the database is getting abstracted on the other end.
  38. Webrat – This is a headless browser that is used in web application testing. It lets you run in browser test without the associated overhead of actually instantiating a browser. This does not execute any JavaScript but is great for doing tests of simple websites without much JS interactions.
  39. Will_Paginate – This is the most popular pagination library out there for Ruby on Rails. It alos supports other Ruby web frameworks like Sinatra and Merb. This gem has a wide variety of formatting options and can accept an active record object or even a simple array. It is quite configurable and can be easily extended to do AJAX pagination as well.

So, that’s all the terms that I can think of so far, I’ll try to add to this as and when I come across more terms. Nowadays, web development is more than a single framework, even one like Rails.  I have found the Rails community is more open and embracing of different ideas and frameworks – even to the extent of moving away from Rails and using lightweight frameworks like Sinatra. IMO that makes this glossary all the more relevant and fun.

As I have mentioned in the article I don’t profess to be an expert about or have even used all the tools and technologies listed above. I would love to get your comments and suggestions on what could be added.

And last but not the least – I wish you all an awesome 2012 – may all your efforts bear fruit this year.

Update (Jan 12 – 2012) : Since there has been some interest in this post, I have added a few more entries and provide more information where I can – stay tuned :-)

Needing an IDE should be considered a language smell

I’m a long time Visual Studio .Net (VS.Net) user – I have worked on the Microsoft .Net platform for a long time and have used the VS.Net tool during that time as my primary integrated development environment (IDE). It’s an awesome tool that has got better as the .Net platform progressed and matured with awesome features like code-completion (one of the first IDEs that had this), interactive debugging, refactoring and one of the most comprehensive documentation tools (MSDN) in the business.

What was frustrating, however, was that to write a simple program, I needed to fire up VS.Net (which is this huge beast of a program that used a lot of resources). On the other hand writing your code in Notepad and then compiling and linking from the command line while interesting was, in my opinion, not practical. I found myself looking for the code completion features and searching Google for C# keywords and .Net API syntax. Using Notepad seemed too difficult so I tried to compromise and I started looking at other “lighter” IDEs like Sharp Develop, Komodo Edit and Mono Develop to deal with this problem. But that meant having to learn my way around the new IDE with different keyboard shortcuts and user interfaces. The mental switching needed to work on another IDE was too high a barrier and despite several half-hearted tries I always found myself going back to VS.Net. The massive footprint of VS.Net was something I decided to ignore to avail myself of the many tools that made coding in C# easy.

Recently, I changed jobs and suddenly I was on Linux and programming in Ruby. I was apprehensive and I searched hard to find a replacement for VS.Net. I found Eclipse to be a bit too bulky and heavy, it was too much like VS.Net – resource hungry :-) Net Beans looked promising but then its community decided that Ruby and Rails support was not a priority which disqualified it for me. After some more looking I concluded that the Linux way seemed to be to use general purpose text editors like Vim and Emacs and customize them to ones specific needs.

Faced with coding without –  “gasp” – a proper enterprise IDE and having to learn Vim or Emacs – I decided to start of with a simpler text editor first. After looking around a bit more I settled on Red Car which is a JRuby open source editor that was copying TextMate (which I had heard was the editor that all the hip Ruby people used) and resigned myself to a drop in my productivity…

Surprisingly though this did not turn out to be the case. I was certainly writing less code but amazingly I was still more productive in Ruby and Rails than I was in C# and ASP.NET in terms of the amount of work done. Despite the lack of an IDE, I found the Ruby syntax to be concise and extremely expressive (I have a separate post on what I love about Ruby) which made it easy to code without having to resort to code completion or similar niceties. More importantly I found reading and understanding code in Ruby to be vastly easier than C# and I found myself not needing the features provided by VS.Net to get a mental model of the code base.

The more I worked with Ruby, the more I liked this way of working and I had a personal epiphany about programming language design. Some languages like C# or VB.Net, forced you to have a IDE just to be able to code in them, while others like Ruby let you simply code. This is because coding in a language like C# involves a lot of ceremony – you needed to write a lot of stuff to help the compiler understand the code – while Ruby tries to get out of your way and just let you code. The terseness and expressiveness of languages like Ruby allow programmers to easily keep larger amounts of functionality in their head.

This particular characteristic of languages should be recognized as a smell – just like a code smell… If you find yourself firing up an IDE as the first step, when you want to write or read a program in a particular language, that is a language smell. It indicates that the language is probably not at the right level of expressiveness for the problem it is trying to solve. To be clear, this particular postulate should be considered in terms of the language design and of course should not be the only way to choose a language. But, it certainly should be an important consideration, IMO,  especially since the language you choose will be something that you will be something you are going to spend most of your waking hours using and more importantly trying to understand.

I think if you come in at the start of your day and it takes you less time to load your code, read and understand what you have written and get into your coding zone, you are definitely going to be more productive, than if you have to fire up a program, wait for a while for it to load and then jump around several files to get your mental model back in your head to start coding. And if you have chosen the language you are using wisely, you will be more productive and in my case a lot happier as well.

Thoughts on Ruby – I think it’s great…

So it’s the middle of the year and like last year I am in the middle of a new language. This time however there are some differences from last year.

The biggest one is that this year I am actually working on the language – Ruby – while I am learning it and I think this makes a big difference. Though I know that one can learn a language using sample exercises and Code Katas – somehow I think the experience of working with a language everyday – gives one a real appreciation of what it is like to live with a language. When you are learning a language by choice – in your own time – you get to choose the problems and the Katas. When you have to work with the language and you use it daily on problems thrust upon you by seemingly unreasonable and sometime crazy requirements – you get a better understanding of the warts and wrinkles of the language along with the pretty bits.

I actually had a cursory look at Ruby sometime back, I had downloaded the language and looked at a tutorial on the web. I solved a couple of basic Project Euler questions with it. I remember thinking at the time – “Hmmmm, not really all that different from Python…” and deciding that learning it was not going to get me much. I was looking to exploring languages like F# and Erlang at the time, since they were based on different ways of solving programming problems.

So, when I started on this Ruby project this year, I came with certain preconceptions – but then something happened that changed how I approached Ruby completely. I read the online book – Why’s (Poignant) Guide to Ruby and I realized that the way I had coded Ruby thus far was not the way Ruby was intended to be coded. Before I go a little further I think I will digress a little give my impression of Why’s (Poignant) Guide to Ruby

This is not the first time I had heard of Why and his book.

_why's self-portrait from Why's (poignant) Gui...

Image via Wikipedia

Indeed one would be hard pressed indeed to spend any time in the Ruby community and not have heard about Why the lucky stiff, his book and his sudden disappearance from the internet (another digression – I really am amazed at the fact this person was able to keep his virtual life so compartmentalized from his non-virtual one that he was able to disappear so completely!). My first impression after reading the first few pages was – Wow!! This guy must have a good collection of hallucinogens and Pink Floyd albums – to create those weird cartoons…

Why's foxes

Image via Wikipedia

But, happily I persevered and was rewarded with a real appreciation of the beauty of Ruby. This book is not only a guide to the Ruby language – it is a guide to the Ruby philosophy – The Ruby Way. I would definitely recommend this book to all developers that are looking to learn Ruby, especially if you are coming from a background of working on the “big iron” languages like C# – this book will provide that mental shift in the way you code, that will let you really appreciate Ruby.

Well, now that the digression is out of the way, lets come back to the question coding style in Ruby. Rubyists tend to favor terse code that conveys its intent clearly and concisely. The Ruby culture uses idiomatic syntax and encourage succinctness in code. I really came to appreciate this when I saw the Ruby approach to a basic programming structure – the loop.

The canonical implementation of a for loop is in the form –

for <counter> <condition>
<begin block>
	<do something>
<end block>

In C it is –

for(i = 0; i < 10; i++)
{

}

In VB it is –

for i = 0 to 10

next

or in the case of iterating through an array

for each <item> in <group>
  <do something>
next

In Ruby the for loop can be expressed in a variety of ways –
The vanilla for loop is available –

for i in 0..10

end 

for  in 

end

But there is also –

5.times { <do something> }

1.upto(10) (<do something)

<group>.each {<do something>}

<group>.map
  <do something>
end .
.
.

Now it can be argued that functions like map are available in other mainstream programming languages as well – but the point I’m trying to make is – in the Ruby way of programming they become the usual way of doing things.

Ruby is a language designed for developers – but in order to fully appreciate and utilize this one must write (and read) Ruby the way Ruby is meant to be written. Once you embrace “The Ruby Way” you will see in Ruby a language that stays out of the way of your thinking. Instead of grappling with or worrying about syntax, you spend more of you time working on your solution. You can see the syntax is very easy to read. In fact the expressiveness of Ruby makes it easy to get to the intent of the code without getting hung up on the syntax. Reading code after you left it for awhile becomes a snap and you find yourself getting back to where you left of more easily.

I had written a post earlier regarding “the coding zone” – Ruby gets me into the coding zone more easily and lets me sustain it for longer than with any other language I know. It is liberating to be able to conceive of and code entire features in the time it took me to look up the idiosyncrasies of the syntax and battle the compiler of other languages. I find coding in Ruby to be a pleasant experience, not the least because of the incredible community around it. The Ruby community in a very real sense is one of the strengths of the language that put it ahead of Python in my book.

So (as you can probably tell from this long post) – I am really enjoying my latest foray into Ruby. That’s not to say Ruby is perfect – even if you ignore its real performance disadvantage – Ruby has some things that can definitely be improved – but with its great community I think it has a real chance of moving forward and fixing these things.