Syntax notes on Ruby ecosystem

Some notes, on syntax in the Ruby ecosystem, namely Ruby itself, ERB and Rails.

Ruby

Comments

# single line comment

=begin
Multi
Line
Comment

The =begin and =end must be at the beginning of the line
=end

# or just
# do multiline
# like this

Too bad multiline comments are a bit clunky

ERB

ERB (Embedded Ruby) is a templating system for embedding Ruby code within a text document like HTML.

From the github README:


<% Ruby code -- inline with output %>
<%= Ruby expression -- replace with result %>
<%# comment -- ignored -- useful in testing %> (`<% #` doesn't work. Don't use Ruby comments.)
% a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
%% replaced with % if first thing on a line and % processing is used
<%% or %%> -- replace with <% or %> respectively

The main things to remember are:

  • <% means “Ruby in here, do it”
  • <%= means “Ruby in here, evaluate it and output the result” (the = implies evaluation)
  • <%# means “Comments” (like comments in bash or yml) all are closed with %>