Ruby-Tricks

Distributing A Rails Application As A Single File

I've had this link sitting in my brew pile for some time and wanted to see if I could get it to work with the latest version of Ruby and Rails, but since I've been too busy so far, I wanted to post it anyway in case a reader would find it useful.

Read more →

How to Cache Data and Preload The Cache in Rails

Courtenay has come up with an ingenious solution to caching and precaching in Ruby on Rails with memcached. It caches data from database requests, as you'd expect, but you can also put together a special forking method that caches data in advance. Courtenay's suggestion for this is if a user does a search and you cache the initial page of search results, you could speculate the user might progress onto page two of the search, so you can get Rails to precache that data in advance so the next page load is blazing fast.

Read more →

How to create a UNIX /etc/init.d startup script with Ruby

Most init.d start-up scripts seem to be bash scripts, but you can write them with any language. Here's some template code I use to create my own Linux services. You can even add them to chkconfig to be started properly on startup, and they'll also work with RedHat / CentOS's service system straight off.

Read more →

Chronic: Natural date parsing for Ruby

Chronic is a natural language (English only, at present, I think) time and date parser written entirely in Ruby. It supports a staggering number of different ways of expressing the date and time. For example:

Read more →

The ease of PHP with the power of Ruby

#!/usr/bin/ruby                                                                 
require 'rubyhp'
__END__
<html>
 <body>
  <% cgi.params.each do |key, value| %>
   <%= key %>: <%= value %><br />
  <% end %>
  <% if cgi.params.empty? %>
   Sorry, please enter some cgi parameters. How about "?foo=baz"?
  <% end %>
 </body>
</html>

Christopher Cyll has put together a great little example of how to quickly create templated Web pages with Ruby without requiring any frameworks. It relies on CGI, but sometimes that's all you need, and it makes throwing together tiny Ruby-powered Web pages possible as easy as creating a simple PHP page.

Read more →