The Accidental Rubyist

invalid byte sequence in UTF-8

ruby one-liners as random signatures for blog posts and emails

leave a comment »

The random sigs appearing below are due to some simple features of Vim (and bash).

1. Create sig files by splitting a tips/oneliners file. I found a ruby1liner file on the internet and split it based on ‘^#’. The file names are now ruby.1, ruby.2 etc. A tgz of these is here.

You can split the file using split or csplit. Since my split did not, and csplit seems buggy, I wrote a ruby program to do so, csplit.rb.
You may unzip the above tgz in some convenient folder.

2. A script is needed to return a random file from that folder. The jot command is great for returning random numbers. The 60 signifies the number of the highest file (60 sig files). rubysig.sh:

#!/bin/bash
DIR=~/sigs
cat $DIR/ruby.`jot -r 1 1 60`

3. Your editor needs to pick up a random file when it loads a new file and insert it at the correct place. I use the Movable Type format, so my sig must go after the 3 hyphens. After placing the sig, I’d like the cursor to come back to one line after the BODY tag. I placed the following lines in a source file that my editor Vim can understand. The second line is the one that does the work.

/^---$
:r !rubysig.sh
?BODY?1

You may use a Vim command-line option to go to end of file (+) but I already have some links at the end so the extra steps.

This is how the new file is created.
vim -s ~/bin/y.so filename.html
I could also have just said:
vim + -c ':r !rubysig.sh' file.html
This would put the sig at the end of the file. Check your Vim options with vim -h.

If you use a mail client such as mutt or alpine, the command rubysig.sh can be used to generate the sgnature.

# print every 3rd line starting at line 4
$ cat | ruby -pe ‘next unless $. >= 4 && $. % 3 == 0’

Advertisements

Written by totalrecall

September 4, 2008 at 6:55 pm

Posted in ruby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: