Thanks, solve my problem regards Eko E S wrote: >>LçÉettçËäº Eko Budi Setiyo <contact_us / haltebis.com> >>Aihe: String.str_replace ???? >> >>Hi all >> >>Any body can help me with this str_replace problem. >> >>thanks >> >>______________________________________ >> >>class String >> def str_replace(str_in,str_out,string = self.to_s) >> result = String.new >> str_in_counter = 0 >> string_pointer = 0 >> str_in_counter = 0 >> >> >> if str_in.length <= 1 >> >> string.length.times{ >> if string[string_pointer] == str_in >> result += str_out >> else >> result += string[string_pointer] >> end >> } >> >> else >> >> buffer = '' >> string.length.times{ >> if string[string_pointer].to_s == str_in[str_in_counter].to_s >> buffer += string_array[string_pointer].dup >> if buffer.length == str_in_length >> result += str_out >> str_in_counter = -1 >> buffer ='' >> end >> else >> result += buffer + string[string_pointer] >> str_in_counter = -1 >> buffer = '' >> end >> str_in_counter += 1 >> string_pointer += 1 >> } >> >> end >> >> result >> end >> >> >> >>require 'test/unit' >> >>class Test_string_replace < Test::Unit::TestCase >> >> def test_replace >> check = '03' >> source = 'aa0a3aa' >> assert_equal(check,source.str_replace('a','')) >> >> check = '03' >> source = 'aaaaaa0aaaa3aaaaaaaa' >> assert_equal(check,source.str_ireplace('aa','')) >> >> end >> >>end >> >> > >Well, > >A) You can just String#tr (or #tr!): > > > >>>"aaaaa3aaaaa0aaaa".tr 'a', '' >>> >>> >=> "30" > >B) You can use Enumerable: > >class String > def str_replace(what, with) > s = self.collect do |c| > with if c === what > end > end >end > >E > > > > > >