3
Feb 09
0

Ruby Was Meant For The Web

Usability of programming languages is not something you think about all the time. I am not talking about the learning curve or whether or not it uses pointers usability, but the usability of a language for a variety of purposes. One such purpose is web programming, and specifically writing code embedded in HTML. Out of all the languages that are used to do it, I have not come across any better than Ruby. Compare these examples:

PHP

<?php foreach($array as $val) { ?>
    <li><?php echo $val ?></li>
<?php } ?>

C#

<% foreach(var val in array)
   { %>
       <li><%= val %></li>
<% } %>

Ruby

<% array.each do |val| %>
  <li><%= val %></li>
<% end %>

The PHP code is similar to ruby, but the curly braces always tend to get lost when they are surrounded by so much else. They are so skinny compared to other code, and they are usually not given any sort of syntax highlighting to help identify them.

The C# code is close to PHP, but Visual studio will adjust your code to fit C# coding style standards. Dropping the open curly brace to new line looks code in the C# editor, but not so much in the HTML editor. I have never been a fan of line-wrap in any code, and with the indent-happiness of HTML, line-wrap can quickly lead to code that doesn’t follow the visual hierarchy and becomes much less readable.

Finally, the Ruby code. While it does require fewer keystrokes to type the equivalent code in this case, Ruby has other benefits. Instead of curly braces, Ruby uses the keywords do and end. When writing this code in an editor, the keywords have syntax highlighting making them all the more easy to distinguish from the surrounding HTML.

Small differences in the design of a programming language can make big differences in its usability. The ease of embedding Ruby in HTML is just one of the many reasons why this language rocks.

Leave a comment

(required)

(not published) (required)