Command line erb

While reading the Textmate book I stumbled upon a command line tool, I didn’t knew about: erb.
Yes, that’s just as in ERb — embedded Ruby. The tool itself, as well as its help, is at your finger tips and should come with your standard Ruby installation:

[2] stephan@rubin ~ #erb --help 
print this helperb [switches] [inputfile] 
-x print ruby script 
-n print ruby script with line number 
-v enable verbose mode 
-d set $DEBUG to true 
-r [library] load a library 
-K [kcode] specify KANJI code-set 
-S [safe_level] set $SAFE (0..4) 
-T [trim_mode] specify trim_mode (0..2, -) 
-P ignore lines which start with "%"

What is it good for? Any kind of text generation, whenever there’s some text you need often, but would like to have ‘preprocessed’.

Let’s assume you’de like to embed the current date at the beginning and any fortune output at the end of your new e-mails. This could be your template file, let’s call it mail_template:

     <%= '%A %d %B in the year %Y')%>    

<%= %w( Dear Hi Hello ).sort_by{ rand }.first %> <name_here>,    


Best wishes    

<%= ENV[ 'USER' ].capitalize %>    

--<%= `fortune` %>

Obviously this needs fortune to be installed on your computer. Run this though erb:

erb mail_template

This is an example of the output:

      Wednesday 25 April in the year 2007.    

Dear <name_here>,    


Best wishes    


--Suddenly, Professor Liebowitz realizes he has come
to the seminar without his duck ...

Replace the place holders and add your message. However, beware of the fact that sometimes fortune will leave you with quite a few lines of text.