Monday, November 27, 2006

Code: Ruby - Fun With ActiveSupport

If you have discovered the joy of coding in Ruby you will understand my enthusiasm! Not for years have I stayed up all night to learn a new programming language - excited at each new aspect, anxious to try what I have learned in a project. Ruby is simply a joy to use!

I am a newbie to Ruby but perhaps you will find something useful from my chaotic thoughts here.

One of these things is ActiveSupport - which many of you may know is a dependency within Rails. Yet you don't need to code a Rails project to enjoy this great library. For those of you not in the know, ActiveSupport is a collection of utility classes and standard library extensions.

Lets take a look at some of what you can do with ActiveSupport.

Inflections
>> "job".pluralize              #=> "jobs"
>> "job".singular #=> "job"
>> 23.ordinalize #=> "23rd"
>> 10.ordinalize #=> "10th"
>> "readers_best_work".humanize #=> "Readers best work"


Numeric
>> 10.bytes             #=> 10
>> 10.megabytes #=> 10485760
>> 10.terabytes #=> 10995116277760
>> 20.seconds #=> 20
>> 20.hours #=> 72000
>> 20.fortnights #=> 24192000
>> 20.months #=> 51840000
>> 20.minutes.ago #=> Mon Nov 27 11:53:23 Pacific Standard Time 2006
>> 20.minutes.from_now #=> Mon Nov 27 12:37:39 Pacific Standard Time 2006


Time
>> now = Time.now       #=> Mon Nov 27 12:19:21 Pacific Standard Time 2006
>> now.at_midnight #=> Mon Nov 27 00:00:00 Pacific Standard Time 2006
>> now.next_week #=> Mon Dec 04 00:00:00 Pacific Standard Time 2006
>> now.seconds_since_midnight #=> 44361.859
>> now.to_s #=> "Mon Nov 27 12:19:21 Pacific Standard Time 2006"
>> now.to_s(:short) #=> "27 Nov 12:19"
>> now.to_s(:rfc822) #=> "Mon, 27 Nov 2006 12:19:21 Pacific Standard Time"

No comments: