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}/,""
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!

On 1 May 2008, at 17:19, [email protected] wrote:

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?

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…

unescaped_emotes ={
:":)" => “smile.gif”,
:":(" => “frown.gif”
}
emotes ={
:lol => “laugh.gif”,
:wow => “jawdrop.gif”
}

emotes.each_key do |emote|
text=text.gsub(/[:#{Regexp.escape(emote.to_s)}]/,"<img src=
“#{emotes[emote]}”>")
end
unescaped_emotes.each_key do |emote|
text=text.gsub(/#{Regexp.escape(emote.to_s)/,"<img src=
“#{unescaped_emotes[emote]}”>")
end
return text

==================================

So sweet.

I decided to keep two seperate hashes for the emotes, to make it more
clear and “pretty”. Any key in unescaped_emotes will be replaced as
is, any key in emotes will be replaced if it appears in the text in
the form [:key].

Thanks a ton for your nudge in the right direction.

—A

On May 1, 12:43 pm, Frederick C. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs