# Gsub mass substitution

**URL:** <https://www.ruby-forum.com/t/gsub-mass-substitution/125435>\
**Category:** Ruby\
**Created:** [January 9, 2008, 2:43am UTC](https://www.ruby-forum.com/t/gsub-mass-substitution/125435 "2008-01-09T02:43:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ckvok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@ckvok](https://www.ruby-forum.com/u/ckvok)\
**Post date:** [January 9, 2008, 2:43am UTC](https://www.ruby-forum.com/t/gsub-mass-substitution/125435/1 "2008-01-09T02:43:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ckvok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@ckvok](https://www.ruby-forum.com/u/ckvok)\
**Post date:** [January 9, 2008, 10:31am UTC](https://www.ruby-forum.com/t/gsub-mass-substitution/125435/2 "2008-01-09T10:31:49Z")

</div>

On Jan 9, 2008 2:44 AM, Ckvok K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![ckvok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@ckvok](https://www.ruby-forum.com/u/ckvok)\
**Post date:** [January 9, 2008, 11:39am UTC](https://www.ruby-forum.com/t/gsub-mass-substitution/125435/3 "2008-01-09T11:39:33Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> On Jan 9, 2008 2:44 AM, Ckvok K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![ckvok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@ckvok](https://www.ruby-forum.com/u/ckvok)\
**Post date:** [January 9, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/gsub-mass-substitution/125435/4 "2008-01-09T12:28:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ckvok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@ckvok](https://www.ruby-forum.com/u/ckvok)\
**Post date:** [January 9, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/gsub-mass-substitution/125435/5 "2008-01-09T14:53:34Z")

</div>

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?=” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

```

---

<div class="post-metadata">

**Author:** ![ckvok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@ckvok](https://www.ruby-forum.com/u/ckvok)\
**Post date:** [January 9, 2008, 12:43pm UTC](https://www.ruby-forum.com/t/gsub-mass-substitution/125435/6 "2008-01-09T12:43:21Z")

</div>

On Jan 9, 2008 12:28 PM, Daniel Brumbaugh K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![ckvok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@ckvok](https://www.ruby-forum.com/u/ckvok)\
**Post date:** [January 9, 2008, 3:04pm UTC](https://www.ruby-forum.com/t/gsub-mass-substitution/125435/7 "2008-01-09T15:04:11Z")

</div>

On Jan 9, 2008 2:53 PM, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![ckvok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@ckvok](https://www.ruby-forum.com/u/ckvok)\
**Post date:** [January 9, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/gsub-mass-substitution/125435/8 "2008-01-09T18:03:20Z")

</div>

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?=” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

```

---

<div class="post-metadata">

**Author:** ![ckvok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@ckvok](https://www.ruby-forum.com/u/ckvok)\
**Post date:** [January 9, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/gsub-mass-substitution/125435/9 "2008-01-09T17:53:52Z")

</div>

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