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
From the ever-helpful Dr. Nic.
Comments Off on More RejectConf 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
A List Apart is conducting their annual web designer survey:

Comments Off on A List Apart’s Web Design Survey
David Berlind’s video Is it time to throw away your servers? is a nice high level explanation of Amazon EC2, and he begins to make the case in business terms for moving to virtual servers. Unfortunately his cost comparison is not apples to apples – he fails to atleast touch on the extra costs involved. Particularly EC2 bandwidth, which was completely left out on the EC2 side, and was no doubt included in the $350/month per physical box number.
Not to mention the time/money for atleast one linux guru to setup your EC2 servers, secure them, back them up, SLAs, etc. I’d like to see a more in depth whiteboard video that goes into these issues. I think EC2 is great, but the cost differences aren’t quite as dramatic as Berlind’s video would have you believe, unless no one ever visits your site. Lets see a more honest comparison. I think EC2 would still come out ahead.
Comments Off on Yes, it’s time to go virtual. But lets be realistic.
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
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
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
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!
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
Some updates to Asset Packager
- asset_packager now tolerates periods in asset names. (Ex: “admin.base.css”) Thanks Timur Vafin.
- updated Capistrano integration docs
- before the compressed assets were getting the subversion revision for the entire repository. Now they get the last modified revision possible for each file. Now it should work as intended – no revision stamp change unless the source files have been updated!
Comments Off on Asset Packager Updates