[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: String.str_replace ????

e

1/25/2005 5:23:00 AM


>
> Lähettäjä: 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



5 Answers

Eko Budi Setiyo

1/25/2005 5:39:00 AM

0

Thanks,
solve my problem

regards
Eko

E S wrote:

>>Lähettäjä: 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
>
>
>
>
>
>




Robert Klemme

1/25/2005 8:21:00 AM

0


"E S" <eero.saynatkari@kolumbus.fi> schrieb im Newsbeitrag
news:20050125052315.WKZL2811.fep31-app.kolumbus.fi@mta.imail.kolumbus.fi...

> B) You can use Enumerable:
>
> class String
> def str_replace(what, with)
> s = self.collect do |c|
> with if c === what
> end
> end
> end

I don't think this will work as String#each does not yield single
characaters but "lines".

>> a=<<EOS
foo
bar
end
EOS
=> "foo\nbar\nend\n"
>> a.each {|x| p x}
"foo\n"
"bar\n"
"end\n"
=> "foo\nbar\nend\n"
>> a.collect {|x| p x; :x}
"foo\n"
"bar\n"
"end\n"
=> [:x, :x, :x]

Regards

robert

Eko Budi Setiyo

1/26/2005 12:33:00 AM

0

Robert Klemme wrote:

>"E S" <eero.saynatkari@kolumbus.fi> schrieb im Newsbeitrag
>news:20050125052315.WKZL2811.fep31-app.kolumbus.fi@mta.imail.kolumbus.fi...
>
>
>
>>B) You can use Enumerable:
>>
>>class String
>> def str_replace(what, with)
>> s = self.collect do |c|
>> with if c === what
>> end
>> end
>>end
>>
>>
>
>I don't think this will work as String#each does not yield single
>characaters but "lines".
>
>
>
You are right, it doesn't
But I already manage to make String.str_replace (case sensitive) and
String.str_ireplace (incase sensitive) pass all my unit test
I already check there is not same function yet build in Ruby object
Thanks a lot for giving me some idea

regards
Eko

>>>a=<<EOS
>>>
>>>
>foo
>bar
>end
>EOS
>=> "foo\nbar\nend\n"
>
>
>>>a.each {|x| p x}
>>>
>>>
>"foo\n"
>"bar\n"
>"end\n"
>=> "foo\nbar\nend\n"
>
>
>>>a.collect {|x| p x; :x}
>>>
>>>
>"foo\n"
>"bar\n"
>"end\n"
>=> [:x, :x, :x]
>
>Regards
>
> robert
>
>
>



Yohanes Santoso

1/26/2005 6:38:00 AM

0

Eko Budi Setiyo <contact_us@haltebis.com> writes:

> But I already manage to make String.str_replace (case sensitive) and
> String.str_ireplace (incase sensitive) pass all my unit test

> I already check there is not same function yet build in Ruby object
But there is: String#gsub()


class String
def str_replace(what, with)
re = Regexp.new(Regexp.quote(what))
self.gsub(re) {with}
end

def str_ireplace(what, with)
re = Regexp.new(Regexp.quote(what), Regexp::IGNORECASE)
self.gsub(re) {with}
end
end


raise "Failed" if "aaaaaaaa0aaaaaa3aaaaaaa".str_ireplace("AA", "") != "03a"
raise "Failed" if "aaaaaaaa0aaaaaa3aaaaaaa".str_ireplace("A", "") != "03"



YS.


Eko Budi Setiyo

1/26/2005 8:34:00 AM

0

Yohanes Santoso wrote:

>Eko Budi Setiyo <contact_us@haltebis.com> writes:
>
>
>
>>But I already manage to make String.str_replace (case sensitive) and
>>String.str_ireplace (incase sensitive) pass all my unit test
>>
>>
>
>
>
>>I already check there is not same function yet build in Ruby object
>>
>>
>But there is: String#gsub()
>
>
>class String
> def str_replace(what, with)
> re = Regexp.new(Regexp.quote(what))
> self.gsub(re) {with}
> end
>
> def str_ireplace(what, with)
> re = Regexp.new(Regexp.quote(what), Regexp::IGNORECASE)
> self.gsub(re) {with}
> end
>end
>
>
>raise "Failed" if "aaaaaaaa0aaaaaa3aaaaaaa".str_ireplace("AA", "") != "03a"
>raise "Failed" if "aaaaaaaa0aaaaaa3aaaaaaa".str_ireplace("A", "") != "03"
>
>
>
>YS
>
>

Thanks you verymuch
your scripts pass the following test

require 'test/unit'

class Test_string_ireplace < Test::Unit::TestCase
def test_replace1
source = 'rasa FiSh bakar'
check = 'rasa ikan bakar'
assert_equal(check,source.str_ireplace('fIsh','ikan'))
end

def test_replace2
source = 'dunia ini panggung sandiwara'
check = 'dunia ini panggung codewara'
assert_equal(check,source.str_ireplace('sandi','code'))
end

def test_replace3
source = 'dunIa inI panggung sandiwara'
check = 'duneKoa eKoneKo panggung sandeKowara'
assert_equal(check,source.str_ireplace('i','eKo'))
end

def test_replace4
check = 'dunia ini panggung sandiwara'
source = 'dunekoa ekoneko panggung sandekowara'
assert_equal(check,source.str_replace('eko','i'))
end

def test_replace5
check = ' ini panggung sandiwara'
source = 'dunia ini panggung sandiwara'
assert_equal(check,source.str_replace('dunia',''))
end

def test_replace6
check = 'ini panggung sandiwara'
source = 'dunia ini panggung sandiwara'
assert_equal(check,source.str_replace('dunia ',''))
end

def test_replace7
check = '03'
source = 'aa0a3aa'
assert_equal(check,source.str_replace('a',''))

check = '03'
source = 'aaaaaa0aaaa3aaaaaaaa'
assert_equal(check,source.str_replace('aa',''))

check = '03'
source = 'aaaaaa0aaaa3aaaaaaaa'
assert_equal(check,source.str_replace('aa',''))

check = '03'
source = '0 3'
assert_equal(check,source.str_replace(' ',''))

check = '0aa3'
source = '0 3'
assert_equal(check,source.str_replace(' ','a'))
end

def test_case_sensitive1
source = 'dunIa ini panggung sandIwara'
check = 'dunIa ekoneko panggung sandIwara'
assert_equal(check,source.str_replace('i','eko'))
end

end

>
>
>