[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

gsub mass substitution

Ckvok Kovsky

1/9/2008 1:44:00 AM

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.
--
Posted via http://www.ruby-....

4 Answers

Jesús Gabriel y Galán

1/9/2008 9:31:00 AM

0

On Jan 9, 2008 2:44 AM, Ckvok Kovsky <mr.oliveira@gmail.com> wrote:
> 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.

Ckvok Kovsky

1/9/2008 10:39:00 AM

0

Jesús Gabriel y Galán wrote:
> On Jan 9, 2008 2:44 AM, Ckvok Kovsky <mr.oliveira@gmail.com> wrote:
>> 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"
>
>> p "Leein�d".gsubm("�",'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"
> => "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.
--
Posted via http://www.ruby-....

Daniel Brumbaugh Keeney

1/9/2008 11:28:00 AM

0

On Jan 9, 2008 4:39 AM, Ckvok Kovsky wrote:
> 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

Jesús Gabriel y Galán

1/9/2008 11:43:00 AM

0

On Jan 9, 2008 12:28 PM, Daniel Brumbaugh Keeney
<devi.webmaster@gmail.com> wrote:
> On Jan 9, 2008 4:39 AM, Ckvok Kovsky wrote:
> > 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')

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.