The Ruby Style Guide Introduction

Last Saturday, we had Geekcamp Baguio Ruby on Rails workshop day 2. Most of the talks and workshop lessons were about Ruby Core Classes and Rails CRUD. There was nothing complex, cryptic or challenging about what we taught for the day. But it was indeed impressive that most were able to complete the lessons very quickly and easily considering some have no background in software development.

Andrei Navarro gave his talk on the Core Classes (String, Array, Hash) and discussed about blocks too. Thank you Drei for traveling for so many hours from Manila and giving that talk here in Baguio.

I continued with Enumerable, Range and Regular Expressions and most of what I have discussed is part of a resource I wrote on December 2012.

After several months, I finally decided to open source it. You may download that here.

I like to give credit to Packt Publishing for the idea. I have never really seen myself as a technical writer. I just code and say things but never really took it seriously. Recently, I have been reviewing books for them. It’s a good experience to learn the process of writing a book. I have much more respect now for those who write for a living. It is challenging.

This guide is published primarily for the workshop participants.

More on Ruby Basics

At this point, participants should know the difference between a local variable (something), an instance variable (@something) and a class variable (@@variable). Please review about *% notations”, how to create an array, add items to an array and remove items from an array. Other data types like Hash were already discussed on Day 1 by 3 different speakers so repetition would have probably made the topic clear.

Syntactic Sugar or Cryptic Code

I recall giving an explanation on conditional assignment which may be concise but cryptic in a way that a beginner will probably not understand what it means.

country ||= "PH"

To read that, simply think that if the local variable country is not defined, return the string “PH.”

Drei mentioned the term “syntactic sugar.” At times, we do forget about the nature of Ruby like the fact that there is an “implicit nil” value for any variable which was not previously defined. During the workshop, we did improve on this code from Railscast:

def sortable(column, title = nil)
  title ||= column.titleize
  css_class = column == sort_column ? "current #{sort_direction}" : nil
  direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
  link_to title, {:sort => column, :direction => direction}, {:class => css_class}
end

Because of the implicit nil (any variable or method not defined is always nil), the use of the ternary operator is unnecessary and a bit cryptic.

def sortable(column, title = nil)
  title ||= column.titleize
  css_class =  "current #{sort_direction}" if column == sort_column
  direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
  link_to title, { sort: column, direction: direction }, { class: css_class }
end

That looks better and we don’t really want to use hash rockets (=>) anymore. If you still see examples that use that, please improve and use the Ruby 2.0 Hash syntax.

You will encounter more Ruby idioms and you can decide for yourself when these idioms should be used. Write sensibly for others to understand your code.

Some non-Ruby discussion during breaks was the shared hatred for CoffeeScript. “Write less, do more” is a jQuery tagline and we do love jQuery as a long as it is not written in CoffeeScript. I have read a CoffeeScript book and completed an online course and unfortunately use it for work too but I hate it very much.

Encapsulation

We also discussed about private methods and how knowing too much about anything is unhealthy. Privacy matters. It is part of the concept of encapsulation or hiding methods. We did not cover the protected methods but you can learn about that on your own. With the Rails helper method, you can declare a private method on a controller as a helper for your public methods (class and instance).