gsub mass substitution

C

Ckvok Kovsky

Hello. I'm a ruby newbie.
I'd like to make a "mass substitution" version of gsub, like in the sed
example:

$echo "ásturãkolsé" | sed -e 'y/áãé/aae/'
asturakolse

Alike, I'd do something like "ãolélü".gsubm("ãéü","aeu") which would
return "aolelu"

----->Code-1:
class String
def gsubm(letras_ruins,substituir_por_este_char)
resultado = self.split(//).each {|letra_do_self|
letras_ruins.split(//).each {|letra_a_substituir|
letra_do_self.gsub(letra_a_substituir.to_.join.squeeze,substituir_por_este_char)
#.join.squeeze
#p 'ls:' + letra_do_self.squeeze
}
}
return resultado.join
end
end

p "Leeinád".gsubm("á",'a')a

----->Code-2:
p "leeináãd".sub(/[áã]/,'a')

I didn't get success in any case. I've searched a lot also, but couldn't
find any solution.
Thanks in advance for any help.
 
J

Jesús Gabriel y Galán

Hello. I'm a ruby newbie.
I'd like to make a "mass substitution" version of gsub, like in the sed
example:

$echo "=E1stur=E3kols=E9" | sed -e 'y/=E1=E3=E9/aae/'
asturakolse

Alike, I'd do something like "=E3ol=E9l=FC".gsubm("=E3=E9=FC","aeu") whic= h would
return "aolelu"
p "Leein=E1d".gsubm("=E1",'a')a

I didn't get success in any case. I've searched a lot also, but couldn't
find any solution.

Maybe tr will work for you:

irb(main):012:0> "abcdefghijk".tr "abc", "123"
=3D> "123defghijk"

It supports character ranges, and padding the second argument
with its last character if it's shorter than the first one:

"hello".tr('aeiou', '*') #=3D> "h*ll*"
"hello".tr('^aeiou', '*') #=3D> "*e**o"
"hello".tr('el', 'ip') #=3D> "hippo"
"hello".tr('a-y', 'b-z') #=3D> "ifmmp"

I was having some problems testing your cases, I think because of the
strange chars, maybe that's the source of your problems too?

Hope this helps,

Jesus.
 
C

Ckvok Kovsky

Jesús Gabriel y Galán said:
Maybe tr will work for you:

irb(main):012:0> "abcdefghijk".tr "abc", "123"
=> "123defghijk"

It supports character ranges, and padding the second argument
with its last character if it's shorter than the first one:

"hello".tr('aeiou', '*') #=> "h*ll*"
"hello".tr('^aeiou', '*') #=> "*e**o"
"hello".tr('el', 'ip') #=> "hippo"
"hello".tr('a-y', 'b-z') #=> "ifmmp"

I was having some problems testing your cases, I think because of the
strange chars, maybe that's the source of your problems too?

Hope this helps,

Jesus.

The "strange chars" are common chars in Brazilian Portuguese and
Portuguese as well as other languages. e.g.: francês, inglês, pátria,
nação, aeroviário, silábica, and so on.

Ruby wasn't designed to deal with those chars in the same way you can
deal with english-only chars.

"som".tr('sm','NM') #=> "NoM"
"cruxificação".tr("çã","ca") #=> "cruxificaaaaao"

$ ruby --version
ruby 1.8.6 (2007-09-24 patchlevel 111) [i686-linux]

Maybe it's better to use a system() call and execute sed or start
learning some other language like perl.

Thanks once again.
 
D

Daniel Brumbaugh Keeney

Ruby wasn't designed to deal with those chars in the same way you can
deal with english-only chars.

Have you tried setting the character set to Unicode? That can make a
difference. Also, before you abandon Ruby altogether, be aware that
Ruby 1.9 deals with characters completely differently than Ruby 1.8,
so those problems you're having might go away when you upgrade (not
necessarily now, of course, 1.9 being officially 'experimental')


Daniel Brumbaugh Keeney
 
J

Jesús Gabriel y Galán

Have you tried setting the character set to Unicode? That can make a
difference. Also, before you abandon Ruby altogether, be aware that
Ruby 1.9 deals with characters completely differently than Ruby 1.8,
so those problems you're having might go away when you upgrade (not
necessarily now, of course, 1.9 being officially 'experimental')

I've tried this and still doesn't work as expected

jesus@jesus-laptop:~$ irb1.8 -KU
irb(main):001:0> "=E2bcd=EB".tr "=E2=EB", "ae"
=3D> "eebcdee"

Maybe someone can explain if this should work as the OP
and myself are expecting or not and why?

Jesus.
 
J

Jesús Gabriel y Galán

Hi,

In message "Re: gsub mass substitution"
|I've tried this and still doesn't work as expected
|
|jesus@jesus-laptop:~$ irb1.8 -KU
|irb(main):001:0> "=E2bcd=EB".tr "=E2=EB", "ae"
|=3D> "eebcdee"

Adding

require 'jcode'

may help you,

Thanks, Matz. This works:

jesus@jesus-laptop:~$ irb1.8 -KU
irb(main):001:0> require 'jcode'
=3D> true
irb(main):002:0> "=E2bcd=EB".tr "=E2=EB", "ae"
=3D> "abcde"

Although I don't understand all the nuances that are coming into play here.
or bring you another trouble. YMMV.

This left me a bit nervous, but as I'm not in need of this (I'm not the OP)
I'll leave it for later when I have more time to understand more about
this issue.

Thanks for answering,

Jesus.
 
C

Ckvok Kovsky

I should thank you three for the solution. It works in the manner Jesús
has posted. Thank you all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top