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

Author Archives: Peter Cooper

By Peter Cooper / July 12, 2006

Classroomapple
Install now with sudo gem install classroom

ClassRoom (RubyForge project) is a project to develop a distributed ‘class server’ powered by DRb that I have been working on. Let’s skip long explanations and jump into code. First, we’ll create a very basic “Dog” class with some basic features:

class Dog
attr_accessor :name

def self.count
@@count ||= 0
end

def initialize(options)
self.name = options[:name]
@@count ||= 0
@@count += 1
end
end

Next, we’ll create a program that can use Dog via ClassRoom:

require ‘rubygems’
require ‘classroom’

class_server = ClassRoom::Client.new(‘classroom://:2001′)
class_server.add_class(IO.read(‘dog.rb’))
class_server.load_class(:all)

puts "There are #{Dog.count} dogs"
fido = Dog.new(:name => "Fido")
puts "There are #{Dog.count} dogs"
rufus = Dog.new(:name => "Rufus")
puts "There are #{Dog.count} dogs"
puts "fido’s name is #{fido.name}"

# => There are 0 dogs
# => There are 1 dogs
# => There are 2 dogs
# => fido’s name is Fido

Take care to notice that at no point is dog.rb actually included/’require’d. Read More

By Peter Cooper / July 11, 2006

Trestle

Scaffolding is not meant to be a final solution for any application. It’s designed to provide ‘scaffolding’ for you to extend and improve. However, the developers of Trestle believe that the scaffolding could be improved significantly and have released their own version.

The trestle generator is an adaptation of the Rails scaffold generator. It produces scaffolding that’s more like production-quality code while maintaining all the rapid goodness you know and love about Rails.

The primary difference is that the HTTP verbs GET and POST are respected and used properly, as opposed to the system in Rails’ default scaffolding. This also allows trestles to be more succinct, with just four actions (index, new, edit, and destroy). Read More

By Peter Cooper / July 10, 2006

Geoffrey Grosenbach introduces the RaPT Plugin Manager for Rails, the answer to the problem of slow plugin installation. Installable as a gem (gem install rapt), RaPT caches the locations of different plugins so that installation is quick and easy. Future plans include developing a central plugin repository, auto-announcement of self-developed plugins, and automatic plugin upgrading. If you want the files direct, check out the Rubyforge project site for RaPT. Read More

By Peter Cooper / July 10, 2006

This library is so amazingly cool that it requires no descriptions beyond these code examples:

“runs”.en.present_participle
# => “running”

2004.en.numwords
# => “two thousand and four”

“cow”.en.quantify( 20_432_123_000_000 )
# => “tens of trillions of cows”

“ruby”.en.plural
# => “rubies”

Or what about?

allobjs = []
ObjectSpace::each_object {|obj| allobjs << obj.class.name}

puts “The current Ruby objectspace contains: ” +
allobjs.en.conjunction( :generalize => true )
The current Ruby objectspace contains: thousands of Strings,
thousands of Arrays, hundreds of Hashes, hundreds of
Classes, many Regexps, a number of Ranges, a number of
Modules, several Floats, several Procs, several MatchDatas,
several Objects, several IOS, several Files, a Binding, a
NoMemoryError, a SystemStackError, a fatal, a ThreadGroup,
and a Thread

Go learn more. Read More

By Peter Cooper / July 9, 2006

You can test your controllers, your models, and, well, most of your application, but till now there hasn’t been a way to explicitly test your Rails application’s helpers. Now there’s a solution.. the helper_test plugin (with source and instructions). Read More

By Peter Cooper / July 9, 2006

Assert Select

assert_select is a plugin by Assaf Arkin that allows you to use CSS selectors in your functional tests to check that certain HTML elements match your assertions. On the surface, this isn’t too far removed from using why’s Hpricot to do assertions on HTML, but in reality having the full power of CSS selectors available changes everything (Update! Hpricot has XPath and CSS addressing too!). Some examples:

# Form includes four input fields
assert_select “form input”, 4

# Page does not have any forms in it.
assert_select “form”, false, “Page must contain no forms”

def test_login_form_has_all_fields
get :login
assert_select “form[action=http://myapp/login] input” do |inputs|
assert_equal 3, inputs.size
assert_select inputs[0], “input[type=name][name=username]”
assert_select inputs[1], “input[type=password][name=password]”
assert_select inputs[2], “input[type=submit][value=Login]”
end
end

More examples here. Read More

By Peter Cooper / July 8, 2006

Spotlightruby

Arcadian Visions have released a plugin for Mac OS X’s Spotlight that lets you quickly search your Ruby source code. Once indexed, you can search by module, class, method, general contents, or code comments. (Found via Have GNU, Will Travel)

(Added: Tread with caution! It seems one user is having problems installing this on their Intel x86 powered Mac. See comments.) Read More

By Peter Cooper / July 8, 2006

New to Rails 3? Check out the Ruby on Rails 3 Tutorial book and screencast.

A book and screencast series showing you how to develop and deploy industrial-strength Rails apps in a direct, step by step way. The screencast series includes 12 lessons over more than 15 hours! Get the best “over the shoulder” experience of following what a top Rails 3 developer does when building an app today. Click here to learn more.

Please note that this post is over four years old – it’s from 2006! As such, these tips were relevant to Rails 1.2 and this content is woefully out of date. Read More

By Peter Cooper / July 7, 2006

Textmatecheatsheet

Mike Clark has produced a single page PDF cheat sheet of about fifty TextMate keyboard shortcuts to speed up your Ruby development. As a long time TextMate user who never uses the shortcuts, this is very useful. Great work, Mike! Read More

By Peter Cooper / July 7, 2006

Comatose

RadiantCMS has been getting a lot of press in the Rails world lately as the first, interesting Rails powered CMS (it’s open source too), but now comes Comatose, a ‘micro CMS’ that works as a Rails plugin, allowing you to easy integrate it with your own application. As Matt McCray, the developer, says:

Lately, I’ve had a recurring issue arise on my projects: They generally require a few content pages. Nothing fancy. Just a privacy policy, terms & conditions, an FAQ, that kind of thing.

I don’t really want to make these static HTML pages. They are likely to change at some point, especially the FAQ. Read More

By Peter Cooper / July 7, 2006

Shattered

It’s still pretty new, but Shattered Ruby looks interesting. It’s a Ruby game development framework that runs on Mac OS X, Linux, and Windows, and uses the Ogre3D libraries (which support OpenGL and DirectX) for graphics. There’s even a blog. It has some pretty neat event handling:

key :pressed => [:a, :b], :action => :do_something
def do_something
puts "will be called when a or b is pressed"
end

I want to find some time to play with this.. Read More

By Peter Cooper / July 6, 2006

MeantimeFilter is an interesting new plugin for Rails by Roman Le Negrate. It’s a little like around_filter, but rather than using a class with ‘before’ and ‘after’ methods, it uses a single method (like the other types of filter) and passes in the method to wrap ‘around’ as a code block. You can then yield to this or pass it into anything you like. An example: Read More

class PostsController < ApplicationController
before_filter :authenticate
meantime_filter :scope_posts_to_user

# Displays the posts of the logged in user.
def show
@posts = Post.find(:all)
end

def create
# …
@post = Post.create(params[:post]) # Automatically associated to @user
# …

By Peter Cooper / July 6, 2006

Paul Battley has developed a Ruby to JavaScript converter. I’m trying to think what this is useful for, but this is an amazing results for just a few hours’ work.

Found via _why Read More

By Peter Cooper / July 5, 2006

Hpricot

Ruby legend whytheluckystuff has developed a new HTML parser called Hpricot. It’s easy to install and use and parses HTML in a liberal fashion. It does, however, require a compiler to install (as it’s written in C), so should be okay on Linux and Mac OS X, though not necessarily on Windows (yet).

Here’s some demo code:

require ‘hpricot’
doc = Hpricot.parse("index.html")
(doc/:p/:a).each do |link|
p link.attributes
end

This is a good alternative to RubyfulSoup, if you’re finding RubyfulSoup too slow (though RubyfulSoup is certainly worth a try!) Read More

By Peter Cooper / July 3, 2006

DevX.com has published a great article by Mark Watson entitled “Ruby Programming Language Enables Concise Network Programming“. Mark gives quick rationales and examples for:

  • a simple (not an exaggeration) Web server from scratch in 12 lines of code
  • a basic WEBrick server
  • a simple REST server with pure sockety madness
  • a quick and simple XML-RPC Web service
  • a slightly more complex SOAP Web server

A great introduction to some of the networking power at your fingertips with Ruby. Read More