Posts Tagged ‘ruby’

17
4/09
0

Meta Programming Meta

Meta programming is one of the most difficult, but rewarding styles of programming I have ever tried. It comes as second to nature to a language like ruby, but to me, it requires an incredible amount of mental strength to accomplish some of the simplest tasks. My first non-tutorial attempt was for a rails plugin auto_html. I added some of the easiest meta programming one can do; write out new methods as strings and have them transformed into regular methods. From auto_html_for.rb:

  suffixed = raw_attr.to_s + AutoHtmlFor.options[:htmlized_attribute_suffix]
  setter = %|
    def #{suffixed}=(val)
      @#{suffixed} = val
    end
  |
  getter = %|
    def #{suffixed}
      if @#{suffixed}.nil?
        self.auto_html_prepare
      end
      @#{suffixed}
    end
  |
  class_eval getter
  class_eval setter

Everything between %| and | is one string, and then class_eval evaluates that string and adds a new method to the class it is working in. Now that it is all finished, it looks so simple, but it took some effort to get right. What got accomplished with this code block though, is remarkably powerful, but only the tip of the iceberg when it comes to meta programming.

If you need to flex some serious brain muscle, start up irb and start getting meta.

3
2/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.