Numbering lines with Ruby


I've been fiddling around a little more with integrating Ruby scripts and one-liners with Quicksilver, and I found a way to number the lines of a selection. This is handy for those times when you decide to bullet-point some text. Generating bullet numbering manually is a chore, and if you rearrange your points you need to renumber all the following points. It's a simple and not very elegant bit of code — I'm sure someone with more than my newbie's knowledge of Ruby could make it much more efficient. For some reason, I couldn't seem to cram it all into a one-liner, so you'll have to save the code below as a file somewhere on your system.

You need to call this script via Quicksilver or the command line like this: ruby /pathto/line_nos.rb | pbcopy. Then copy your text to be numbered, run the line above, then paste. Voila! Numbered lines. Note that the backslash at the end of the line starting '$std_out' indicates a continuation line.

#!/usr/bin/env ruby
# line_nos.rb
text = `pbpaste`
lines = text.split(" n")
def number_lines(max)
  i = 0
  while i < max
    yield i
    i += 1
$std_out = number_lines(lines.length) {|val| num = val + 1;
   puts "#{num}. #{lines[val]}n"}
comments powered by Disqus