Dr Nic

Auto-completer for my blog comments

It took 4 hours to return from the town of Strängnäs to Stockholm via train. Normally, its 40 minutes. The train never turned up. 4 * 60 – 40 = 200 minutes of non-travel.

Stuck in a train station for 200 minutes I tackled a problem that has irked me for some time.

In my blog comments, when I reply to someone else’s comment I’ll normally use the syntax: “@chris – thanks for the kind feedback, I’ll send the money via paypal“.

And then Chris says “@Dr Nic – yeah, send it to the usual account; the tax man will never find it“.

And then I say “@chris – no problem“.

Its not that “@chris” is difficult to spell, but I’ve used IRC and Gmail long enough that I like auto-completion. The absence of auto-completion in Skype irks me too.

What I really want is:

“@c” + TAB expands to “@chris – “

So that’s what I now have. And its neat.

Demo of auto-completer

Unfortunately, the first person to leave a comment below won’t have anyone to reply to, and so theoretically wouldn’t get to experience the joys of the feature, that is so awesome and sexy that my very own wife called:

“That’s nice dear, now help with dinner”.

So, by default “Nic”, “Dr Nic”, “drnic” are available. The 2nd commenter will get these + the name of the 1st commenter. The 3rd commenter will get… hehe, you thought I was going to type that out.

Here’s what you’ll see when you save your comments:

Testing Autocompleter

Public release

Its currently implemented for Wordpress, using Prototype/Scriptaculous. Well it works on my Wordpress theme. It figures out the auto-completion list from the HTML, not from the blog/forum server.

I’m not sure how different everyone’s HTML is for their comment blocks. That makes it tricky to release the conde at the moment for general consumption. You might be an HTML guru and make it work, but your sister isn’t.

So, can you please comment below (wink wink) with the name of your blog software, and include your blog url in the URL field. I might be asking for grief here, but I’ll scope out everyone’s blog comment HTML and see how different/similar they are.

BUT!…

If someone in the comments has already mentioned your blog software (Wordpress, Mephisto, etc) then you MUST reply to their comment. If you are first to comment with your blog software, then use “@Dr Nic” instead.

This is life-or-death critical as it gives you an excuse to play with the auto-completer thingy. Lives are at stake here people!

Don’t have a blog, but want to comment anyway to play with it in all its snazziness? Go for gold.

Pastie paradise

Summary: click to select

Ever tried emailing someone a chunk of code? Some program output?

It starts in the default font, then you change it to a fix-width font, but you still don’t have any syntax highlighting; and the end user has no easy way to extract it from their email and reuse it.

Oh, how about sharing chunks of text/code/program output via Skype/GoogleTalk where the IM makes no effort what-so-ever to keep your pasted text nicely formatted.

Or, IRC, where its definitely poor form to paste in multi-line chunks of anything. Plus it looks ugly and is completely non-reusable.

Pastie

The solution is to store the chunk somewhere else (website) and forward that (url) to all and sundry.

The solution is pastie (or TextMate pastie or other pastie services), by Josh Goebel

How to use?

Hands up if you’ve never driven in a car and used its seatbelts.

Ok, then why does every airline around the world show you how to use one?

Right. So, I’m not going to show you how to use pastie. Its a form and a submit button.

Moving on…

The other day, Chris Wanstrath, took another step closer to becoming a Deity for Novel Helper Apps, with the release of sake – the system-wide Rake tool. A tremendous way to access Ruby code (coded as Rake tasks) from anywhere, without polluting your path, or requiring you to bundle the code as a RubyGem to share it easily.

What does this have to do with pasties?

There is a sake task for sending anything to pastie. Cop a look…

# sake 1.0.6 available now
gem install sake -y
sake -i http://pastie.caboo.se/73801.txt
sake -T
sake pastie:send
  # Sends STDIN or FILE=file to Pastie;
  # USAGE: cat some_code.rb | sake pastie:send
  # OR:    sake pastie:send FILE=some_code.rb

On OSX, you can get the url for your new pastie in the clipboard using pbcopy:

cat some_code.rb | sake pastie:send | pbcopy

Want to send a patch to someone via pastie?

svn diff | sake pastie:send | pbcopy

and the pastie url is now in your clipboard ready to paste in trac/irc/email, etc.

Pastie-ing sake tasks

Theoretically, you could send someone your beloved sake tasks with:

sake -e my_task | sake pastie:send | pbcopy

As of sake 1.0.6, you can use the -P option instead:

sake -P my_task | pbcopy

Coming from the future

Rumour has it, (because Chris told me) that Chris and PJ are building an app to take pastie-ing to the next level.

I’m guessing the magic formula will be:

  1. Create a pastie clone
  2. Add social networking
  3. Add versioning of pasties
  4. Write wicked-cool blog post and link the whole store to a dubious personal trait, such as drinking sake and beer at the same time
  5. Watch their Working With Rails popularity rankings climb (see Chris and PJ)

Bonus – Railscast competition

I love the Railscast videos, by Ryan Bates. Short video chunks of Rails/Ruby goodness.

To motivate people to submit Rails Documentation Patches he’s now running a competition with prizes!

Where art thou generator?

Summary: click to select

UPDATE: Rails Generators within RubyGems support is now in Rails Edge

Generators in Rails are awesome. No? So you don’t use the rails command to create a new rails app? Nor the model, migration, or plugin generators? Yeah, they are awesome. Hug the next one you meet.

On my part-time quest to replace the use of Plugins with RubyGems within Rails, it occurred to Geoffrey Grosenbach [1] and myself that there is no easy way to bundle rails generators within RubyGems. Patooey, we say!

Currently, there are several places you can store Rails generators:

  • Builtin ones – like model, scaffold etc – are stored in the rails gem.
  • ~/.rails/generators/ for your own personal generators.
  • Rubygems whose name ends with _generator, such as ajax_scaffold_generator
  • vendor/generators/
  • vendor/plugins/_some_plugin_/generators/ – the most common mechanism for deploying generators

But no way to bundle multiple generators in a RubyGem or have a gem without _generator in its name. And Rails has laid claim to the _generator namespace, which is a bit greedy.

This is outrageous!

Damn it, you’re right! Its now in Rails Edge!

rails newapp
cd newapp
rake rails:freeze:edge

So try out this new Rails Core patch. Never touched a raw patch before? Nevermind. To play with it, let’s create a new rails app:

cd vendor/rails/railties
wget http://dev.rubyonrails.org/attachment/ticket/8730/gem_generators.patch?format=txt -O gem_generators.patch
patch -p0 < gem_generators.patch
cd ../../..

The world’s first Generator-Enabled RubyGem is…

So you can play with this fabbo new feature and explore how to add generators to your RubyGems, Rick Olson’s restful_authentication plugin has been ported into a RubyGem. It is currently available for manual download and will soon be live on RubyForge.

wget http://drnicwilliams.com/wp-content/ruby/gems/restful_authentication-1.0.0.gem
gem install restful_authentication
ruby script/generate

And you’ll see the authenticated generator is discovered from the RubyGem. Neat. Note, we didn’t install the old plugin within the rails app.

So, now let’s play with it…

ruby script/generate authenticated user

It works exactly like it did from the original plugin. But now its a RubyGem, and you never ever need to reinstall this plugin for each new app, ever again. Ever.

Merge plugin generators into RubyGems

There exists a RubyGem daemons, and there exists a separate plugin, that makes it easy to use daemons via your rails app.

This separation will be no longer necessary!

Now the generator code can be moved into the daemons gem itself.

Have a look at the plugin, and note the generators folder. Simply copy this folder into the daemons gem. Then rename this folder to rails_generators. Repackage and release.

Shaazam!

PS. To the owner of daemons repository, come on dude! Upgrade from CVS to SVN:)

Want to write your own generators?

There is some useful info on writing generators on the tubes. Start there.

In all cases, (for an example “foo” generator) the generator file must be called foo_generator.rb, and it must contain a class called FooGenerator.

Does it work with “vendor everything?”

Normal people living normal lives won’t know there exists a Rails Core ticket to support “vendor everything”, created by Marcel Molina Jr [2] (and his backing singer Dave Thomas).

This allows you to store gem files in a /vendor/gems folder, and it will be super magically exploded into the /vendor/gems/home folder. That is, you can freeze raw gems into your rails source structure.

To test this, first uninstall the restful_authentication gem and then reload it into the /vendor/gems folder:

gem uninstall restful_authentication
cd vendor/rails/railties
wget http://dev.rubyonrails.org/attachment/ticket/8511/gems_loaded_from_vendor_like_plugins.diff?format=txt -O gems_loaded.patch
patch -p0 < gems_loaded.patch
cd ../../..
ruby script/generate

Do this just to confirm that the authenticated generate is no longer available.

cd vendor
ls .

Note that you now have a gems folder, and within it you have a home folder, which itself stores lots more new folders. This is Marcel’s patch in action!

cd gems
wget http://drnicwilliams.com/wp-content/ruby/gems/restful_authentication-1.0.0.gem
cd ../..
ruby script/generate

And the authenticated generator is back! Discovered auto-magically from within your own rails source! Investigate the vendor/gems/home folders at your leisure.

So this article is about two things…

Herein layeth a tale of two tickets (first one is now in Rails Edge). Sneaky, nay?

Please try them out and comment within each ticket about any praise/thoughts/fixes/outright-damnations so the nice Rails Core team (esp. to DHH who is not sure if “vendor everything” is something that should be in Rails Core [3]) know this is an important direction for Rails to go.


[1] Normally I’d reference/link
Nuby On Rails or Peepcode, but I just love the “half a head” self-photo on his home page.


[2] More info about Marcel on
Rails Core page.


[3] Based on a chat in irc #rails-contrib the other day, when that ticket was first created.

Want a new $$$ stream for your Rails consulting business?

Summary: click to select

Ruby on Rails started in the DHH’s “basement” and was picked up by alpha geeks around the world over the last few years. Now, beta geeks and business people are picking it up. This is a good thing – it means you can get a job in Rails, or do Rails consulting/freelancing and have clients who know what Rails is and what it can do for them.

But they don’t know code. They don’t know good code, and they don’t know good developers.

Aha! You are a good developer; perhaps you can help?

Start offering “code audits” and “code reviews” to companies/teams/other freelancers, as recently offered by Robby Russell’s Planet Argon company.

Its an awesome opportunity for you to help other developers with feedback, help business owners know what’s going on with their development teams, and make $$$.

$$$ is good and making it should make you happy.

Should you be in a situation where you have gaps of time between consulting projects, or your a full-time employee looking to start into the consulting world, perhaps start offering Code Reviews and Code Audits as a service.

Its also a nice service to offer companies that didn’t hire you for a job, but went with someone else instead.

Thank you for considering us for your project. Although you did not select our company for the development, we know you’ve chosen someone competent. You may be interested in our complimentary service – code audits and code reviews. If its ok, we’ll contact you in a month or so to offer you this service so you know that your developers are on track and doing an excellent job!

Ok, that’s a crappy sales letter, but you get the gist.

Smart people doing smart things in Netherlands – RubyEnRails 2007

Summary: click to select

rubyenrails2007-me-presenting-by-fngtps.jpg

Photo by Thijs van der Vossen,
Fingertips

For one million people in Amsterdam, time stood still as a parade of Rails gurus in Netherlands/ Belgium talked up a storm at RubyEnRails 2007. Only 240 were allow inside to the free, one day conference, so riots ensued at the doorsteps of the building. Police were called in. Helicopters. Tanks.

Or so I’m told. I was too busy inside meeting many very clever developers and sharp business people. The buzz of excitement for Rails in the foyer during the breaks was tremendous. The conference organisers recorded the audio of all presentations which will be great to listen to.

I was flown in my private jet, helicoptered to the conference building and shepherded in by 24 Dutch Special Forces men, all of equal height and build, identical in every fashion. Before my keynote, I was interviewed by all the major TV networks. I have no idea what drives me to make this all up.

But I was given a free RubyEnRails conference t-shirt. I was also able to give a couple of presentations:

Keynote

It was a wonderful opportunity to start the day, and I shared “What’s cool with Rails?” focusing on some hot potatoes like scaling and deployment, as well as RESTfulness. Slides here.

Ultimately the basic theme of the keynote was “Its all just text”, and choosing between Rails, Merb, Camping, Mongrel handlers, becomes a design/architectural decision to be made for each portion of each application you build.

I included a live demo of the Magic Model Generator, and encouraged people to write RubyGems instead of Rails plugins, as they provide better support for: dependencies, versioning, and reuse outside of Rails.

DIY syntax

The ability to extend my programming language tickles me pink. Often you write a block of code and you just think “That should be prettier and simpler”.

With Ruby meta-programming, blocks, method_missing, const_missing and optional parentheses you can craft nearly any syntactic sugar you like to replace lengthy, complicated code.

Slides here.

Dr Nic Academy

I also took the opportunity to introduce Dr Nic Academy – training courses in Ruby on Rails by me. The first course will be 7th and 8th of July in Amsterdam. If you are thinking of attending, hold off buying tickets!! The new website and a special bonus discount price will be coming soon. I’d hate you to miss out.