Hashes and Regexes and Special Characters

I am rolling my own smiley/emoticon generator. A lot of this has do
do with my less than stellar knowledge of ruby, so if this is better
asked on a ruby list, let me know and I'll move along...

I have text based emoticon replacement working fine. I use a hash to
define the text/image pairs like this...

emotes = {
  :wow => "wow.gif",
  :lol => "lol.gif"
}

I then have a simple regex that goes through the text and plucks out
[:key] and replaces it with an image tag.

The problem is I would also like to do conversion of :slight_smile: :open_mouth: :frowning: and
such. I'm running into trouble setting up my hash like that.

I can't seem to use special characters like : or ( in a key. So I
actually made two hashes, one with description/emote_text pairs and
one with description/image pairs like so...

special_keys = {
  :smile => ":)"
  :frown => ":frowning:
}
special_emotes = {
  :smile => "regular_smile.gif"
  :frown => "frown.gif"
}

Then I thought I would regex something like

special_keys.each_value do |key|
text=text.gsub(/#{key}/,"<img
src=#{special_emotes[special_keys.index(#{key})]}>"
end

Now, forgetting for the moment the right side of that regex, which I
don't know if it will even work or not, the regex is failing because
the ')' in #{key} is closing the regex early. So I thought I would
get smart and escape the special characters in special_keys like so:

special_keys = {
  :smile => ":\)"
}

Which, to my suprise ends up as {:smile=>":)"}

Foo!

About this time I figured I was trying too hard and that there's
probably something shiny in ruby that will solve this little problem.
Anyone out there an expert hash wrangler?

TIA!

I reckon the reason that happens is because in a string, ) is a perfectly valid character but \) is an unknown character escape and so the \ gets dropped.
You really don't want to be doing this by hand though, RegExp.quote will do that for you.
Lastly you can have a symbol with anything you want in it, but you need to say :'here is a weird symbol'

Fred

Frederic,

Thank you SO much for your help. I managed to turn the mess of code I
was writing into something very simple and rubyish.

I now have the following in my helper...