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

Author Archives: Peter Cooper

By Peter Cooper / June 16, 2006

The guys over at Fingertips present an article entitled ‘excellent and pragmatic proposal for easier Unicode support in Rails‘. Julian ‘Julik’ Tarkhanov has developed a proxy class for String that tweaks all of the methods to work properly with Unicode. You can then use code like this:

text.chars.length > length ? text.chars[0...l] + truncate_string : text

chars provides the proxy object from the text string. You can also use code like text.u.length as chars is aliased to u too.

They want to see it accepted as a patch to Rails, although I’m personally more interested in seeing this become a gem and be available in Ruby generally. Read More

By Peter Cooper / June 16, 2006

Coderay

CodeRay is a totally new syntax highlighting library written in Ruby. It deals with Ruby, C, Delphi, HTML, RHTML, and Nitro-XHTML out of the box, with plans to support YAML, SQL, Python, Perl, PHP, and Java. It’s quite customizable and easy to use, as seen in the code in the image above. Nice work! Read More

By Peter Cooper / June 16, 2006

Mauricio Fernandez reports that the next minor version of Ruby, Ruby 1.8.5, is due for release in mid August to coincide with the Japanese holiday of Obon. Ruby releases are usually tied to holiday periods, most notably Christmas for 1.8.3 and 1.8.4.

For the more adventurous, the initial preview release of 1.8.5 is coming by the end of June, with other preview releases in July and August. If Japanese is your thing, there’s more info here. Read More

By Peter Cooper / June 16, 2006

Newruby

Several months ago there was a competition to redesign the official Ruby homepage at ruby-lang.org. A winning design was chosen, and why said the work on implementing it would begin shortly thereafter.
It hasn’t gone live for real yet, but you can preview the new design in the flesh at http://new.ruby-lang.org/en/ Read More

By Peter Cooper / June 16, 2006

Derek Haynes laments:

Auto-Complete is a great tool when it provides possible results BEFORE you finish typing. Unfortunately, using Rails’s included AJAX helpers to query the database as you type often results in a large delay before matches are returned.

There have been small hacks from several people to help resolve this, but Derek presents a simple local_auto_complete_field helper (with source to use right away) that lets you specify some pre-fetched results for the autocompleter to use right away. Read More

By Peter Cooper / June 16, 2006

I’ve noticed many readers are using del.icio.us, so this should come in handy. Someone has developed a Ruby script that uses the del.icio.us API to download all of your links, and then uses SQLite to put them into a local database, whereupon you can do whatever you like with them. I just gave it a try and it works well.

It lacks some spit and polish though, and will complain if you run it more than once a day (as it attempts to recreate the tables on the same database). While you can run it daily, it uses a new database for each day, which might also not be so great. Read More

By Peter Cooper / June 15, 2006

Peter Szinek has announced he’s going to write a series of articles on ‘screen scraping’ with Ruby (more accurately, extracting data from Web pages and other online sources) and has released the first article entitled “Data Extraction for Web 2.0: Screen scraping in Ruby/Rails“. He covers four basic scraping techniques, first using regular expressions, then HTree and REXML, then RubyfulSoup, and finally WWW::Mechanize. If you need to process shaky HTML sources from Ruby, read on.

The font used on his blog may seem a little painful to some readers, but he covers these main libraries and techniques pretty well. Read More

By Peter Cooper / June 14, 2006

KRJS is an extension to Rails views by Chew Choon Keat that helps provide a radically different mechanism for handling AJAX and RJS. He calls it “RJS without messing the views.”

Rather than use remote_form_for and link_to_remote, for example, you can create specifically named actions on your controller that use RJS to perform the relevant actions upon regular code, rather than clog your views up with JavaScript and AJAX snippets. For example:

<%= form_tag({:action => ‘submit’}, {:id => ‘form’}) %>
<%= text_field ‘account’, ‘login’, :id => ‘account-new-login’ %>
<%= submit_tag ‘Login’ %>
<%= end_form_tag %>

This code looks like a normal form, but with KRJS, when the form is submitted the on_form_submit action is called on the controller via AJAX. Read More

By Peter Cooper / June 14, 2006

Aidan Finn, a freelance Ruby on Rails developer in Ireland, has developed a quick guide to creating your own generators in Rails. Code generators in Rails are useful when you have similar patterns between controller in various projects, but aren’t ready to jump into creating a Rails Engine or plugin, or where such wouldn’t be relevant. Read More

By Peter Cooper / June 14, 2006

The RubyExamples page is a few years old now, but I just came across a great example which still works, and which demonstrates the intense power of Ruby. Please note that Justin Bishop deserves all the credit for this one.

Here’s the routine:

class RubyStock
require ‘net/http’
def RubyStock::getStocks(*symbols)
Hash[*(symbols.collect{|symbol|[symbol,Hash[\\
*(Net::HTTP.get('quote.yahoo.com','/d?f=nl1&s='+symbol).chop\\
.split(',').unshift("Name").insert(2,"Price"))]];}.flatten)];
end
end

Using it is simple:

puts RubyStock::getStocks(“MSFT”, “IBM”, “GOOG”).inspect

=> {“GOOG”=>{“Name”=>”\”GOOGLE\”", “Price”=>”386.525″}, “IBM”=>{“Name”=>”\”INTL BUSINESS MAC\”", “Price”=>”76.93″}, “MSFT”=>{“Name”=>”\”MICROSOFT CP\”", “Price”=>”21.51″}}

Amazing! It returns within half a second on my machine, and I can’t believe the same scraping logic works after three years. Read More

By Peter Cooper / June 14, 2006

Idiomaticruby
Idiomatic Ruby is an online presentation by Toby DiPasquale that goes through some of the more idiomatic features of Ruby, those that newcomers to Ruby might be confused by or overlook. It covers Modules and Mixins, Exceptions, Regular Expressions, Duck Typing, Iterators, method_missing, Continuations, Closures and Blocks, and is very concise and well put together.

If you have someone in your development team who isn’t quite up to speed with Ruby yet, this presentation can help it all make sense. Read More

By Peter Cooper / June 14, 2006

Railsforum

A couple of enterprising Ruby developers have put together a forum for Ruby and Rails developers called Rails Forum (railsforum.com). It’s very new, but the posts seem to be trickling through already. As with most forums, it’s a chicken and egg situation, but if you enjoy forums, consider signing up and contributing. It’s time the community had a large, bustling forum, as some newcomers find them extremely valuable. Read More

By Peter Cooper / June 14, 2006

O’Reilly has just released “RJS Template for Rails” by Cody Fauser, the god of RJS templates. RJS templates are used in Ruby to create a full AJAX experience. They let you adjust and add elements onto the current page without reloading by using nice, clean Ruby code, without getting down and dirty with JavaScript.

I’ve just bought the book and it’s 58 pages of goodness. I don’t work with RJS too often, so this guide is already opening my eyes to my techniques I wasn’t aware of before. If you’re developing Rails apps and aren’t intimately familiar with RJS, you need it. Read More

By Peter Cooper / June 13, 2006

Stefan Kaes takes a look at common performance problems with Ruby on Rails. He looks at:

  • session containment and storage
  • caching constants
  • optimizing finder queries
  • avoiding slow helpers

(As an aside, InfoQ is shaping up to be a great resource. It’s worth checking out from time to time.) Read More

By Peter Cooper / June 13, 2006

Pat Eyler of O’Reilly’s Ruby blog has interviewed James Gray of Ruby Quiz fame. James talks about how he was won over to Ruby via Perl 6, and how the Ruby Quiz has continued to explode in popularity.

(If you haven’t checked it out, the Ruby Quiz is a weekly Ruby programming contest. A new challenge is issued each Friday, and a few days later everyone chips in their solutions and ideas. All of this is then archived on the site, allowing anyone to learn some great Ruby tricks whenever they like. There are currently 82 quizzes to learn about, so enjoy.) Read More