|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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.squ eeze,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 . -- Posted via http://www.ruby-forum.com/. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 s, Jesus. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 s, > > 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-forum.com/. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Jan 9, 2008 2:53 PM, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> Hi, > > In message "Re: gsub mass substitution" > on Wed, 9 Jan 2008 20:42:57 +0900, "=3D?ISO-8859-1?Q?Jes=3DFAs_Gabrie= l_y_Gal=3DE1n?=3D" <jgabrielygalan@gmail.com> writes: > > |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 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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
I should thank you three for the solution. It works in the manner Jesús
has posted. Thank you all. -- Posted via http://www.ruby-forum.com/. |
|
![]() |
| Outils de la discussion | |
|
|