Ruby: regex scanning in a case statement

Here’s a handy ruby snippet that might come in handy one day. When the regex matches (input should end with “ today”), you can directly grab the matched value using the special $1 variable. case input when /(.*)\stoday$/i then puts "Today: #{$1}" end I think you can see how you can bend this to your own needs.

Rails Snippet: Caching expensive calls

In Rails, from time to time, you may encounter you have a method you call several times, but which returns always the same result. For example, have the following: class Person < ActiveRecord::Base has_many :articles def get_approved_articles self.articles.find(:all, :conditions => {:approved => true}, :order => 'approved_on DESC') end end A query is fired every time you call Person#get_approved_articles. To cache the result of the query during this request, just add a bit of magic