Its always easy to skip going through the details of what your stacked up knowledge and just go for trying out new stuff. This time, I take it one step back and skim through pages of books I have always ignored and pick some sections that are worth looking into and post them here.

I've been trying to cut down on paper materials at home. Mostly, trying to read all those books and photocopied materials and just jot down notes from them. I decided to post them instead. So here goes..

From a photocopy of a ruby/rails book (no title gathered):

Some string extensions:

string = "Now is the time"  
puts string.at(2)  #=> "w"  
puts string.from(8)  #=> "he time"  
puts string.to(8)  #=> "Now is th"  
puts string.first  #=> "N"  
puts string.first(3)  #=> "Now"  
puts string.last  #=> "e"  
puts string.last(4)  #=> "time"

puts string.starts_with?("No")  #=> true  
puts string.ends_with?("ME")  #=> false

count = Hash.new(0)  
string.each_char { |ch | count[ch] += 1 }  
#=> {" " => 3, "w" => 1, "N" => 1, "o" => 1, "e" => 2, "h" => 1, "s" => 1, "t" => 2, "i" => 2 }

Customizing inflections:

Inflector.inflections do |inflect|  
  inflect.plural(/-in-law$/, "s-in-law")
  inflect.singular(/s-in-law$/, "-in-law")
  inflect.uncountable("air", "information", "water")  #no singular form, no plural form
end

Extensions to numbers:

puts 3.ordinalize  #=> "3rd"  
puts 321.ordinalize  #=> "321st"

puts 20.byes  #=> 20  
puts 20.kilobytes  #=> 20480  
puts 20.megabytes  #=> 20971520  
puts 20.gigabytes  #=> 21474836480  
puts 20.terabytes  #=> 21990232555520  
puts 20.petabytes  #=> 22517998136852480  
puts 1.exabyte  #=> 1152921504606846976

puts 20.seconds  #=> 20  
puts 20.minutes  #=> 1200  
puts 20.hours  #=> 72000  
puts 20.days  #=> 1728000  
puts 20.weeks  #=> 12096000  
puts 20.fortnights  #=> 24192000  
puts 20.months  #=> 51840000  
puts 20.years  #=> 630720000

puts Time.now  #=> Tue May 18 22:03:21 +0800 2010  
puts 20.minutes.ago  #=> Tue May 18 21:43:53 +0800 2010  
puts 20.hours.from_now  #=> Wed May 19 18:04:11 +0800 2010  
puts 20.weeks.from_now  #=> Tue Oct 05 22:04:22 +0800 2010  
puts 20.months.ago  #=> Thu Sep 18 22:04:34 +0800 2008  
puts 20.minutes.until("2010-12-26 12:00:00".to_time)  #=> Sun Dec 26 11:40:00 UTC 2010  
puts 20.minutes.since("2006-11-08 01:15:00".to_time)  #=> Wed Nov 08 01:35:00 UTC 2006

Time and Date Extensions:

now = Time.now  
puts now  #=>  Tue May 18 22:09:17 0800 2010  
puts now.to_date  #=> 2010-05-18  
puts now.to_s  #=> Tue May 18 22:09:17 +0800 2010

puts now.to_s(:short)  #=> 18 May 22:09  
puts now.to_s(:long)  #=> May 18, 2010 22:09  
puts now.to_s(:db)  #=> 2010-05-18 22:09:17  
puts now.to_s(:rfc822)  #=> Tue, 18 May 2010 22:09:17 +0900  
puts now.ago(3600)  #=> Tue May 18 21:09:17 +0800 2010  
puts now.at_beginning_of_day  #=> Tue May 18 00:00:00 +0800 2010

puts now.at_beginning_of_month  #=> Sat May 01 00:00:00 +0800 2010  
puts now.at_beginning_of_week  #=> Mon May 17 00:00:00 +0800 2010  
puts now.beginning_of_quarter  #=> Thu Apr 01 00:00:00 +0800 2010  
puts now.at_beginning_of_year  #=> Fri Jan 01 00:00:00 +0800 2010  
puts now.at_midnight  #=> Tue May 18 00:00:00 +0800 2010

puts now.change(:hour => 13)  #=> Tue May 18 13:00:00 +0800 2010  
puts now.last_month  #=> Sun Apr 18 22:09:17 +0800 2010  
puts now.last_year  #=> Mon May 18 22:09:17 +0800 2009  
puts now.midnight  #=> Tue May 18 00:00:00 +0800 2010  
puts now.monday  #=> Mon May 17 00:00:00 +0800 2010

puts now.months_ago(2)  #=> Thu Mar 18 22:09:17 +0800 2010  
puts now.months_since(2)  #=> Sun Jul 18 22:09:17 +0800 2010  
puts now.next_week  #=> Mon May 24 00:00:00 +0800 2010  
puts now.next_year  #=> Wed May 18 22:09:17 +0800 2011  
puts now.seconds_since_midnight  #=> 79757.033881

puts now.since(7200)  #=> Wed May 19 00:09:17 +0800 2010  
puts now.tomorrow  #=> Wed May 19 22:09:17 +0800 2010  
puts now.years_ago(2)  #=> Sun May 18 22:09:17 +0800 2008  
puts now.years_since(2)  #=> Fri May 18 22:09:17 +0800 2012  
puts now.yesterday  #=> Mon May 17 22:09:17 +0800 2010

puts now.advance(:days => 30)  #=> Thu Jun 17 22:09:17 +0800 2010  
puts Time.days_in_month(2)  #=> 28  
puts Time.days_in_month(2, 2000)  #=> 29

Extension to Ruby Symbols:

groups = posts.group_by { |post| post.author_id }

or

groups = post.group_by(&:author_id)