Jan
17
2007
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
8 comments | tags: class_eval, dsl, Java, mixin, module, reopen class, Ruby, stringutils | posted in Java, Ruby, TechKnow
Oct
22
2006
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
no comments | tags: history, matz, pickaxe, Ruby, rubyconf, rubyconf2006 | posted in Ruby, TechKnow
Jul
24
2006
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.
no comments | tags: printf, Ruby, rubyonrails, strftime, time formatting | posted in Ruby, TechKnow