hi all, i have one problem while encoding the special characters like '''',./?:""!@#$%^&*():;\{}[] >,I get string of book name containing some special characters, now i have to replace those characters with encoded value so i write following function below:- def special_char(text) #puts "bookname in special_char::::== #{text}" searchwords = text arrlength= searchwords.length #puts "arrlength::::== #{arrlength}" arrlength.times do|i| str=searchwords[i] str=str.gsub(''%'',''%25''), str=str.gsub(''|'',''%7C''), str=str.gsub(''\'''',''%5C''), str=str.gsub('' '',''%20''), str=str.gsub('','',''%2C''), str=str.gsub(''?'',''%3F'') searchwords[i]=str end but when i pass the parameter text it gives me error :"private method `gsub'' called for 84:Fixnum" if i add one more line in above function as suppose for (:) this special character it will give the above error. thanks in advanced -- Regards Sachin S. Kewale -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
you can use URI.escape(''string with special char'') instead On Tue, Feb 21, 2012 at 2:23 PM, sachin kewale <sachinkewale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> hi all, > i have one problem while encoding the special characters like > '''',./?:""!@#$%^&*():;\{}[] >,I get string of book name containing some > special characters, > now i have to replace those characters with encoded value so i write > following function below:- > > def special_char(text) > #puts "bookname in special_char::::== #{text}" > searchwords = text > arrlength= searchwords.length > #puts "arrlength::::== #{arrlength}" > arrlength.times do|i| > str=searchwords[i] > str=str.gsub(''%'',''%25''), > str=str.gsub(''|'',''%7C''), > > str=str.gsub(''\'''',''%5C''), > str=str.gsub('' '',''%20''), > str=str.gsub('','',''%2C''), > str=str.gsub(''?'',''%3F'') > > searchwords[i]=str > end > > but when i pass the parameter text it gives me error :"private method > `gsub'' called for 84:Fixnum" > > if i add one more line in above function as suppose for (:) this special > character it will give the above error. > > thanks in advanced > > -- > Regards > Sachin S. Kewale > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- *David Angga Prasetya* *RoR Developers* skype: david.angga phone: +62 85 222 1 5555 2 * * -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 21.02.2012, at 11:23, sachin kewale wrote:> hi all, > i have one problem while encoding the special characters like '''',./?:""!@#$%^&*():;\{}[] >,I get string of book name containing some special characters, > now i have to replace those characters with encoded value so i write following function below:- > > def special_char(text) > #puts "bookname in special_char::::== #{text}" > searchwords = text > arrlength= searchwords.length > #puts "arrlength::::== #{arrlength}" > arrlength.times do|i| > str=searchwords[i] > str=str.gsub(''%'',''%25''), > str=str.gsub(''|'',''%7C''), > > str=str.gsub(''\'''',''%5C''), > str=str.gsub('' '',''%20''), > str=str.gsub('','',''%2C''), > str=str.gsub(''?'',''%3F'') > > searchwords[i]=str > end > > but when i pass the parameter text it gives me error :"private method `gsub'' called for 84:Fixnum" >Ugly coding, sorry... Try this instead (works on ruby 1.9): def encode(string) map = {"symbol" => "spec_char" …. } # full it start_index = 0 while match = string.match(/[\W]/, start_index) new_char = map[match.to_s] string[match.begin(0)..match.end(0)-1] = new_char start_index = match.begin(0) + new_char.size # correct start_index end string end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.