Does anybody have a good starting point for how to read the first word of a string and then, based on several defined pairs, swap it? I''ve been considering using something like @split = @message.split(''-'') if @split[0] == "word" ... elsif @split[0] == "otherword" ... end Even if this is the best way to do it, I''m not sure how to replace the word or patch the string back up for display. :/ -- Posted via http://www.ruby-forum.com/.
How about something like words = {} words[''word''] = ''newword'' words[''otherword''] = ''newotherword'' outmessage = message splits = message.split('' '') if (words[splits[0]]) splits[0] = words[splits[0]] outmessage = splits.join('' '') end obviously it can be cleaned up a fair bit, but the hash will save having a god awful if or switch statement Simon On Thu, 23 Apr 2009 16:22:33 +0800, Robert Scott <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Does anybody have a good starting point for how to read the first word > of a string and then, based on several defined pairs, swap it? > > I''ve been considering using something like > > @split = @message.split(''-'') > if @split[0] == "word" > ... > elsif @split[0] == "otherword" > ... > end > > Even if this is the best way to do it, I''m not sure how to replace the > word or patch the string back up for display. :/
how about this? lookup = { "word"=>"newword",..} first_word = @message[0...-uPkaz0FUOsYkt/YBF/qpGQ@public.gmane.org(" ")] first_word = lookup[first_word] On Apr 23, 1:22 pm, Robert Scott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Does anybody have a good starting point for how to read the first word > of a string and then, based on several defined pairs, swap it? > > I''ve been considering using something like > > @split = @message.split(''-'') > if @split[0] == "word" > ... > elsif @split[0] == "otherword" > ... > end > > Even if this is the best way to do it, I''m not sure how to replace the > word or patch the string back up for display. :/ > -- > Posted viahttp://www.ruby-forum.com/.