Oct 8 2009

The Rubyist: September 2009 Edition

Here is a recap of the top Ruby-related links for the month of September 2009. Links for The Rubyist are provided by A Rubyist Railstastic Adventure, a tumblelog.

Ruby

Rails

JRuby


Sep 7 2009

The Rubyist: August 2009 Edition

Here is a recap of the top Ruby-related links for the month of August 2009. Links for The Rubyist are provided by A Rubyist Railstastic Adventure, a tumblelog.

Ruby

Rails


Aug 3 2009

The Rubyist: July 2009 Edition

Here is a recap of the top Ruby-related links for the month of July 2009. Links for The Rubyist are provided by A Rubyist Railstastic Adventure, a tumblelog.

Ruby

Rails

JRuby


Jan 17 2007

Reopening Ruby Classes

In Ruby the implementation of a class is not closed. In Ruby, it is incredibly easy to add new methods to any existing class at runtime, even core system classes like String, Array, and Fixum. You just reopen a class and add new code. This language feature makes the language incredibly flexible. You might be wondering, why would you need this? Well, take a look at the API documentation of the Java String class. In most development organizations that I have been a part of we have had to write small string libraries that provide far more functionality that provided by the Java String class. The Apache Jakarta Commons Lang project provides a String helper class in StringUtils. StringUtils provides methods such as IsEmpty, Chomp/Chop, and LeftPad. StringUtils. StringUtils is a great class that has saved me from re-inventing the wheel several times, but it is another library to download, configure, learn, and import. Using the StringUtils class is more verbose calling a method on a string.

// Using StringUtils to trim a string.
str = StringUtils.trim(str);
// Calling a trim on a string.
str = str.trim();

Isn’t more concise and easier to read when you invoke a method on a string instead of using StringUtils? Unfortunately, even if an JCP is approved to add your custom string methods to the String class, the whole process is very time consuming. Each incremental release of Java takes years to develop. In Ruby, you can just reopen the String class to add the few methods you feel it lacks. To reopen the Ruby String class we can use the following code:

# reopen the class
String.class_eval do
  # define new method
  def get_size
    # method implementation
    length
  end
end

Continue reading


Oct 22 2006

The History of Ruby

For this RubyConf2006 session, the history of Ruby was broken down into five periods which include pre-history, ancient age, middle age, modern age, and contemporary.

The pre-history of Ruby was dated circa 1993 and it is an age of myth and epic legends which only Matz and his friends know. According to the speaker, Masayoshi Takahashi, Ruby was baptized before it was born, that is to say, before any code had been written. According to Masayoshi-san, a normal name would lead to a normal language, a great name would make a great language. But in a bizarro world, Ruby could have been known as Coral. The name Coral was proposed in a chat session between Matz and an early contributor. But I feel that a rose in any other name would still smell as sweet and that a Ruby in any other name would still bring joy to program.

The ancient age of the Ruby programming language was carbon dated to 1995. At this time, Ruby 0.95 was available to the public, that is Japanese netnews users. At this time the ruby-list ML was launched with the first email: ruby-0.95 test failed.
Continue reading


Jul 24 2006

Ruby Time Formatting

The Time class in Ruby has a powerful formatting function which can help you represent the time in a variety of ways. The strftime function is modeled after C’s printf. Here are some usages examples:

t = Time.now
t.strftime("%m/%d/%Y %H:%M:%S") # 07/24/2006 09:09:03

t.strtime("%A") # Monday
t.strftime("%B") # July

You can use the lower case a and b to get the abbreviated name of the weekday and month, respectively. To get the the day of the year, between 1 – 366, use the j option.

t.strftime("%j") # 205

The strftime function is useful when formatting the created_at datetime of your Rails application ActiveRecord models.