Ruby String Replacement Script

Use a text file to replace strings in your pages. 

Ruby Script Library

Our Ruby scripts are all compatable with Deluxe hosting. Also see our GoDaddy Ruby Tutorial.

These simple little pieces help demonstrate many of Ruby's uses.

hash = {};"../replace.txt", "r") do |file|
while ! file.eof do
line2 = file.gets
key, val = line2.split(":::")
hash[key] = val if key and val

def replacer(line,hash)
hash.each{|k,v| line[k] = line[k].replace(v) if line.include? k}
return line

We begin by creating an empty hash. Then we load a file called "replace.txt" and it's in the directory just below our current directory. Ex:

[homepage]:::<a href="index.html">Home</a>
[links]:::<a href="links.html">Links</a>
[quoteoftheweek]:::Clothes make the man. Naked people have little or no influence on Society. -Mark Twain

Then that file is parsed at the ":::", which you can change to anything, and then the key and value are stored in a hash. Then, when it comes time to print your page, just check each line with 'replacer("line to print",hash).

If you don't want to have to include hash in the replacer method, you can simply put a $ in front of hash (at every occurrence!) to make $hash. This makes $hash a global variable, which can be used in things like "def". If you don't have the $, and you don't tell replacer what hash to use, it can not find hash. Variables that don't have $ can not be read from a "def" function.