Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > String.str_replace ????

Reply
Thread Tools

String.str_replace ????

 
 
Eko Budi Setiyo
Guest
Posts: n/a
 
      01-25-2005
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





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments