Does Ruby rock?

Tuesday 22 July 2008

I want to write a method that collates the string descriptions of a collection of items, separating them with a HTML link break, and returns the result in a form I can output on a webpage.

In Ruby I can write the whole method with one concise line:

def to_formatted_string

  collection.map {|item| ERB::Util.html_escape(item.to_string) + '<br />'}

end

In C# the solution is somewhat less elegant:

public string ToFormattedString()
{
  StringBuilder output = new StringBuilder();

  foreach (string item in collection)
  {
    output.Append(HttpUtility.HtmlEncode(item.ToString()) + "<br />");
  }

  return output.ToString();
}

Conclusion: Ruby rocks!

Post a Comment