Gsub mass substitution

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.

On Jan 9, 2008 2:44 AM, Ckvok K. [email protected] 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’, ‘') #=> "hll*"
“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.

Jesús Gabriel y Galán wrote:

On Jan 9, 2008 2:44 AM, Ckvok K. [email protected] 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’, ‘') #=> "hll*"
“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.

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

Hi,

In message “Re: gsub mass substitution”
on Wed, 9 Jan 2008 20:42:57 +0900,
“=?ISO-8859-1?Q?Jes=FAs_Gabriel_y_Gal=E1n?=” [email protected]
writes:

|I’ve tried this and still doesn’t work as expected
|
|jesus@jesus-laptop:~$ irb1.8 -KU
|irb(main):001:0> “âbcdë”.tr “âë”, “ae”
|=> “eebcdee”

Adding

require ‘jcode’

may help you, or bring you another trouble. YMMV.

          matz.

On Jan 9, 2008 12:28 PM, Daniel Brumbaugh K.
[email protected] wrote:

On Jan 9, 2008 4:39 AM, Ckvok K. 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> “âbcdë”.tr “âë”, “ae”
=> “eebcdee”

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

Jesus.

On Jan 9, 2008 2:53 PM, Yukihiro M. [email protected] wrote:

Adding

require ‘jcode’

may help you,

Thanks, Matz. This works:

jesus@jesus-laptop:~$ irb1.8 -KU
irb(main):001:0> require ‘jcode’
=> true
irb(main):002:0> “âbcdë”.tr “âë”, “ae”
=> “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.

Hi,

In message “Re: gsub mass substitution”
on Wed, 9 Jan 2008 23:03:25 +0900,
“=?ISO-8859-1?Q?Jes=FAs_Gabriel_y_Gal=E1n?=” [email protected]
writes:

|> 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.

jcode replaces methods in the String class globally. That may cause
difference.

          matz.

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