andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-May-01 16:19 UTC
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 :) :O :( 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!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 1 May 2008, at 17:19, andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > > 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?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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-May-01 17:51 UTC
Re: Hashes and Regexes and Special Characters
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 Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 1 May 2008, at 17:19,
andrew.ohns...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:
>
>
>
>
>
>
>
> > 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?
>
> 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- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---