Synthesis : Scott Becker

Random Permutation in Ruby

This post on Algo Blog about random permutation shows a cool a little algorithm for randomizing an array which apparently impresses interviewers. I thought it might be fun to write it in Ruby:

def permute_array(a)
  1.upto(a.length - 1) do |i|
    j = rand(i + 1)
    a[i], a[j] = a[j], a[i]
  end
  a
end

# alternate version
def permute_array2(a)
  0.upto(a.length - 2) do |i|
    j = rand(a.length - i)
    a[i], a[j] = a[j], a[i]
  end
  a
end

This is actually a pretty fun little function to use. For example – to find all the unique possible combinations of letters in my name: (Secret Scrabble weapon)

(1..5000).map{ permute_array("scott".split("")).join }.uniq.sort

=> ["costt", "cotst", "cotts", "csott", "cstot", "cstto", "ctost",
"ctots", "ctsot", "ctsto", "cttos", "cttso", "ocstt", "octst",
"octts", "osctt", "ostct", "osttc", "otcst", "otcts", "otsct",
"otstc", "ottcs", "ottsc", "scott", "sctot", "sctto", "soctt",
"sotct", "sottc", "stcot", "stcto", "stoct", "stotc", "sttco",
"sttoc", "tcost", "tcots", "tcsot", "tcsto", "tctos", "tctso",
"tocst", "tocts", "tosct", "tostc", "totcs", "totsc", "tscot",
"tscto", "tsoct", "tsotc", "tstco", "tstoc", "ttcos", "ttcso",
"ttocs", "ttosc", "ttsco", "ttsoc"]

Comments Off on Random Permutation in Ruby

More RejectConf Videos

From the ever-helpful Dr. Nic.

Comments Off on More RejectConf Videos

RejectConf2007 Videos

Found a couple videos from RejectConf, which I missed out on while at RailsConf. And since there was 4 parallel tracks at RailsConf, the most anyone could see is 25% of it. If anyone knows links to more videos from RailsConf07 or RejectConf07, I’d love to know.

Comments Off on RejectConf2007 Videos

New version of AssetPackager

Thanks to Dan Kubb for alerting me to a new version of JSMin, the library used by AssetPackager to compress javascript.

With the release of the latest jQuery 1.1.1, it triggered a bug in how jsmin was treating characters within a regexp.

AssetPackager now has the latest version.

Also, for those using Prototype, the v1.5 release that comes with Rails 1.2.1 has a missing semi-colon on line 846. This of course breaks when compressed. To fix it, this line should have a semi-colon at the end. This:

if (params && /Konqueror|Safari|KHTML/.test(navigator.userAgent)) params += '&_='

Should be:

if (params && /Konqueror|Safari|KHTML/.test(navigator.userAgent)) params += '&_=';

Update: Michael Schuerig has posted this to the rails trac so hopefully this will be fixed soon. In the meantime, it’s pretty simple to make the change yourself.

Comments Off on New version of AssetPackager

Hash – to_query

Look what just appeared in Rails trunk – Hash#to_query – turn a hash of values into a form-encoded query string. I’ve wanted this on occasion in the past. Check it out:

Simple Conversion

{:a => 10}.to_query

Becomes this:

a=10

Nested Conversion

{:person => {:name => 'Nicholas', :login => 'seckar'}}.to_query

Becomes this:

person[name]=Nicholas&person[login]=seckar

Multiple Nested

{:person => {:id => 10}, :account => {:person => {:id => 20}}}.to_query

Becomes this:

account[person][id]=20&person[id]=10

Array Values

{:person => {:id => [10, 20]}}.to_query

Becomes this:

person[id][]=10&person[id][]=20

You can do the same thing in Prototype:

$H({ action: 'ship', order_id: 123, fees: ['f1', 'f2'], 'label': 'a demo' }).toQueryString();

Becomes this:

'action=ship&order_id=123&fees=f1&fees=f2&label=a%20demo'

It doesn’t look like Prototype handles the nested conversions yet though.

Comments Off on Hash – to_query

Asset Packager is in a book!

My plugin AssetPackager has been featured in Scott Raymond’s new book Ajax on Rails !

Thanks Scott! Everyone go buy a copy. AssetPackager has a full section in Chapter 9 – Performance. Nice.

Comments Off on Asset Packager is in a book!

New Plugin: MySQL Tasks

I wrote some convenience rake tasks to automate creation and backup of MySQL databases, and I use it all the time, so I figured I’d plugin-afy it to make it simple to use in all my projects, and share it with the world as well! Here’s the repository

MySQL Tasks

Some rake tasks to automate common database tasks (create/destroy & backup/restore).

Install

./script/plugin install http://sbecker.net/shared/plugins/mysql_tasks

Components

rake db:mysql:create           # Create database (using database.yml config)
rake db:mysql:destroy          # Destroy database (using database.yml config)
rake db:mysql:backup           # Dump schema and data to an SQL file (/db/backup_YYYY_MM_DD.sql)
rake db:mysql:restore          # Load schema and data from an SQL file (/db/restore.sql)

Specifying RAILS_ENV works if you want to perform operations on test or production databases.

Comments Off on New Plugin: MySQL Tasks

Asset Packager Updates

Some updates to Asset Packager

Comments Off on Asset Packager Updates

Sit back and REST

In Scott Raymond’s latest post, he discusses refactoring IconBuffet.com and following the latest DHH Rails dogma – REST-style resources. He manages to simplify IconBuffet from 10 controllers & 76 actions to 13 controllers and 58 actions total, without removing or adding any features. Thats about 20 less actions. Needless to say, following the recommended REST-style pattern can simplify your work a lot.

“Cutting actions is great, but even more significant is that the remaining ones are almost completely uniform. There are seven standard Rails actions: index, new, create, show, edit, update, and destroy. Everything else—oddball actions—are usually a clue that you’re doing RPC. In the old version, there were forty oddball actions; now there are only five (and four of those are essentially static pages in the about controller.) The upshot is that the controllers are very uniform, which makes the entire application conceptually simpler, and thus easier to maintain, test, and extend.”

Check out more at his site.

Comments Off on Sit back and REST

RailsConf

RailsConf was great. Tons of new ideas. People using their brains. Outlines of most of the talks at RailsConf can be found here and here.

Now I’m just hanging out at my g/f’s dad’s house in suburbs outside Chicago for a day before we go back to Florida. Gotta hang with the fam. Hopefully we’ll get to hang out IN Chicago a bit tomorrow before the plane.

Comments Off on RailsConf