# Gsub-ing each character

**URL:** <https://www.ruby-forum.com/t/gsub-ing-each-character/115699>\
**Category:** Ruby\
**Created:** [September 27, 2007, 7:18pm UTC](https://www.ruby-forum.com/t/gsub-ing-each-character/115699 "2007-09-27T19:18:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 27, 2007, 7:18pm UTC](https://www.ruby-forum.com/t/gsub-ing-each-character/115699/1 "2007-09-27T19:18:41Z")

</div>

I want to convert certain letters to numbers on a gets.chomp…  
my initial approach was this----

words = gets.chomp  
new = words.each {|p| p.gsub(/[h]/, ‘00’)}

puts new

##### 

however this doesnt work the miracle 🙂

new = words.gsub(/[h]/, ‘00’)

puts new

##### this works perfectly, but the problem occurs if i want to gsub

more than one character as if i have new = another.gsub then puts new  
only outputs the last gsub.

Ideas?

Thanks!

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 27, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/gsub-ing-each-character/115699/2 "2007-09-27T20:46:25Z")

</div>

On Sep 27, 12:18 pm, Michael L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Thanks!
> 
> Posted viahttp://www.ruby-forum.com/.

Normally, you’d use String#tr for something like this, but since you  
want to replace single characters with multiple characters, you might  
try something like this:

subs = {  
‘h’ =\> ‘00’,  
‘i’ =\> ‘01’,  
‘j’ =\> ‘02’,  
}

new = gets.chomp

subs.inject(new) {|acc, sub| acc.gsub(sub[0], sub[1])} unless new.nil?

For something more similar to what you’re already doing, you can chain  
the #gsub calls together:

new.gsub(‘h’, ‘00’).gsub(‘i’, ‘01’).gsub(‘j’, ‘02’) unless new.nil?

This may or may not run faster than my first solution, but the first  
is easier to code/maintain.

Jeremy

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 27, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/gsub-ing-each-character/115699/3 "2007-09-27T20:50:31Z")

</div>

On Sep 27, 1:43 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Oops…made a couple mistakes in the assignment (or lack thereof):

> subs = {  
> ‘h’ =\> ‘00’,  
> ‘i’ =\> ‘01’,  
> ‘j’ =\> ‘02’,
> 
> }

words = gets.chomp  
new = subs.inject(words) {|acc, sub| acc.gsub(sub[0], sub[1])} unless  
new.nil?

or:

words = gets.chomp  
new = words.gsub(‘h’, ‘00’).gsub(‘i’, ‘01’).gsub(‘j’, ‘02’) unless  
new.nil?

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 27, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/gsub-ing-each-character/115699/4 "2007-09-27T20:55:51Z")

</div>

Sorry, more mistakes. I’m giving up after this.

subs = {  
‘h’ =\> ‘00’,  
‘i’ =\> ‘01’,  
‘j’ =\> ‘02’,  
}

words = gets.chomp  
new = subs.inject(words) {|acc, sub| acc.gsub(sub[0], sub[1])} unless  
words.nil?

or:

words = gets.chomp  
new = words.gsub(‘h’, ‘00’).gsub(‘i’, ‘01’).gsub(‘j’, ‘02’) unless  
words.nil?

Hopefully that’s right.

Jeremy

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 27, 2007, 9:34pm UTC](https://www.ruby-forum.com/t/gsub-ing-each-character/115699/5 "2007-09-27T21:34:37Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Sorry, more mistakes. I’m giving up after this.
> 
> subs = {  
> ‘h’ =\> ‘00’,  
> ‘i’ =\> ‘01’,  
> ‘j’ =\> ‘02’,  
> }
> 
> words = gets.chomp  
> new = subs.inject(words) {|acc, sub| acc.gsub(sub[0], sub[1])} unless  
> words.nil?
> 
> or:
> 
> words = gets.chomp  
> new = words.gsub(‘h’, ‘00’).gsub(‘i’, ‘01’).gsub(‘j’, ‘02’) unless  
> words.nil?
> 
> Hopefully that’s right.
> 
> Jeremy

Haha no worries, thanks a ton – didnt know you could chain gsubs but  
now i think ill go with the inject method cause yes you were right when  
you talked about maintaining it.
