Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Author Archives: Peter Cooper

By Peter Cooper / June 9, 2006

Chris Anderson looks at deploying Rails apps with Capistrano and Mongrel on Planet Argon. He provides a whole pile of recipe snippets, as well as some Capistrano recipes for Mongrel ‘spinner’ and ‘restart’ tasks.

If all of this sounds like double dutch, you might want to skip this post, or learn more about Capistrano and Mongrel, as they’re both very useful tools when it comes to Ruby on Rails. Read More

By Peter Cooper / June 9, 2006

I’m not a Windows user at all, but RForward struck me as perhaps being very interesting to those wanting to roll out Rails apps on Windows servers. It sounds like some of the new UNIX-side solutions to deploying Rails applications.. that is, proxying requests from one daemon to another rather than doing it all from one.

RForward is a reverse proxy designed to make Ruby on Rails applications easy to install into Microsoft’s IIS web-server.

The traditional way of installing Ruby on Rails applications into IIS is to use FastCGI. I have written a tutorial – as you can see it is quite long-winded and error-prone. Read More

By Peter Cooper / June 9, 2006

maiha from #caboose ran some benchmarks and produced this graph of ‘requests per second’ for a basic app under different dispatcher setups (Apache, lighttpd, Mongrel, etc.):

Rails-Deployer-Benchmarks

It shouldn’t be taken as definitive (the Litespeed + lighttpd time seems suspicious to me), but it definitely demonstrates the power of lighttpd and Mongrel. Read More

By Peter Cooper / June 8, 2006

Lately I’ve been finding little known Ruby gems and trying them out. My latest find is EventMachine. EventMachine describes itself:

Ruby/EventMachine is a fast, simple event-processing library for Ruby programs. It lets you write network clients and servers without handling sockets- all you do is send and receive data. Single-threaded socket engine- scalable and FAST!

Simply install with gem install eventmachine and you can create a fast, multi-user server / daemon like so:

require ‘rubygems’
require ‘eventmachine’

module EchoServer
def receive_data(data)
send_data “>>> You sent: #{data}”
close_connection if data =~ /quit|exit/i
end
end

EventMachine::run {
EventMachine::start_server “127.0.0.1″, 8081, EchoServer
}

Once you run this, you can telnet to localhost on port 8081 and have a conversation with yourself! Read More

By Peter Cooper / June 8, 2006

Pastie

Pastie is a new code pasting system developed by Josh Goebel. It has a 37signals type quality to it. It’s clean, quick, and does just what it says it’ll do. It colors your code and is just generally slick, pretty, and a fine piece of work. Josh also claims it’s less than 200 lines of Rails. Let’s hope the source gets released! Read More

By Peter Cooper / June 8, 2006

Ryan Daigle reports that the latest ‘edge Rails’ has a cool new feature that lets you specify find conditions more logically. For example:

Post.find(:first, :conditions => ['status = ? and active = ?', 1, 1])

.. becomes:

Post.find(:first, :conditions => { :status => 1, :active => 1 })

I’ve accidentally tried to use this style before, and am glad it’s now an approved part of Rails. If you’re running edge Rails, you should have access to it as soon as you update, otherwise wait for Rails 1.2 :)

If this tickles your fancy, you might also want to check out ez_where by Ezra Zygmuntowicz that lets you do insanely cool stuff like:

articles = Article.ez_find(:all, :include => :author) do |article, author|
article.title =~ "%Foo Title%"
author.any do
name == ‘Ezra’
name == ‘Fab’
end
end

Yes, abstraction rules. Read More

By Peter Cooper / June 8, 2006

It’s quick and easy (to do, not necessarily to parse!).

Step one: Install the FeedTools gem with gem install feedtools

Step two: Use the following code:

require ‘rubygems’
require_gem ‘feedtools’

feed = FeedTools::Feed.open(‘http://www.petercooper.co.uk/index.rdf’)

puts “Feed title is #{feed.title}”

feed.items.each do |item|
puts “#{item.title} – #{item.link}”
end

Step three: Learn more with the official tutorial and API documentation.

Step four: You are now the King of all feeds. Read More

By Peter Cooper / June 8, 2006

From http://rails.co.za/articles/2006/06/03/using-railscron:

RailsCron, by Kyle Maxwell, ‘is a way to run background tasks using your Ruby on Rails environment.’ You could arguably do what ever you want to do in the background via running a crontab that executes the script/runner, but RailsCron enables you to do it all from ruby code, and seeing as it is an ActiveRecord object, you can manipulate it as such from your application.

Rails Cron isn’t the be-all and end-all of background task automation, but it can provide a useful crutch to lean on in some situations. If you need to send regular mails to users, etc, give it a look. Read More

By Peter Cooper / June 7, 2006

Ruby Inline is an analog to Perl’s Inline::C. Out of the box, it allows you to embed C/++ external module code in your ruby script directly. By writing simple builder classes, you can teach how to cope with new languages (fortran, perl, whatever).

I downloaded it (simply download the gem and install it) and tapped out the following code, and it worked just great:

require ‘rubygems’
require_gem ‘RubyInline’

class << self
inline do |builder|
builder.c "
int dummy(int input) {
int i = 1;
while (input >= 1) { input–; i *= 2; }
return i;
}
"
end
end

puts dummy(8)

This works a treat on OS X. Read More

By Peter Cooper / June 7, 2006

Radiant-1

A lot of people have considered developing a content management system in Rails over the past year, but few attempts have turned into something usable. Enter Radiant CMS. It’s not finished yet, but it provides a good example of a Rails application and is available now via SVN. If digging through Rails source code is the way you learn best, enjoy! Read More

By Peter Cooper / June 6, 2006

The forum set up for the Rails Recipes book has a section where readers can write their own Rails recipes. Some of them are pretty good and would have been good contenders for the book. Here are some of them:

By Peter Cooper / June 6, 2006

Pagination in Rails is good, but it can lack flexibility in many situations. At that point it’s time to roll your own. However, Phil Bogle and Laurel Fan came up with a solution they call paginate_by_sql that can solve some of the custom pagination problems. This needs to become a plugin.

I’m reposting their code here simply because I like to see syntax coloring :) Read More

# paginate by sql
# http://thebogles.com/blog/2006/06/paginate_by_sql-for-rails-a-more-general-approach/
# added support for sql with arguments
# added a :count option for passing in either a Integer count or count query.
module ActiveRecord
class Base
def self.find_by_sql_with_limit(sql, offset, limit)
sql = sanitize_sql(sql)
add_limit!(sql, {:limit => limit, :offset => offset})
find_by_sql(sql)
end

def self.count_by_sql_wrapping_select_query(sql)
sql = sanitize_sql(sql)
count_by_sql("select count(*) from (#{sql})")
end
end
end

class ApplicationController < ActionController::Base
def paginate_by_sql(model, sql, per_page, options={})
if options[:count]
if options[:count].is_a?

By Peter Cooper / June 6, 2006

Rubyclasses

These are a few years old now, but they can still come in useful. Here are some useful graphs of Ruby’s class hierarchies for data types, IO, etc. Read More

By Peter Cooper / June 6, 2006

This cool Ruby cheat sheet quickly runs you through.. reserved words, syntax rules, escape characters, regular expression characters and formats, file mode strings, special variables, expressions, operations, built in classes, and more. Extremely useful to beginners and advanced Rubyists alike. Read More

By Peter Cooper / June 6, 2006

Watir

I can’t test this as I’m not a Windows user, but Watir looks very interesting. It’s a testing tool that runs with Ruby on Windows and it controls an instance of Internet Explorer and lets you test any web sites or applications you want programmatically. It’s like integration tests but for any site, and from IE. There’s a sample test if you want to see how a basic test comes together.

Of course, if you want something that’s cross platform and cross browser, Selenium will be more your cup of tea! Read More