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
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 => "
}
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!