# Simple programming q

**URL:** <https://www.ruby-forum.com/t/simple-programming-q/53826>\
**Category:** Ruby\
**Created:** [February 15, 2006, 12:33am UTC](https://www.ruby-forum.com/t/simple-programming-q/53826 "2006-02-15T00:33:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![grrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@grrr](https://www.ruby-forum.com/u/grrr)\
**Post date:** [February 15, 2006, 12:33am UTC](https://www.ruby-forum.com/t/simple-programming-q/53826/1 "2006-02-15T00:33:44Z")

</div>

I am a ruby newbie. I want to make a simple script that replaces certain  
characters in a block of text with specific other characters.

I am wondering what kind of loop or algo is thought to be most effective  
for this kind of work…  
Maybe go through the block of text with a hashing table and replace  
each ‘hit’? Would this be reasonable approach?

#example pseudocode idea  
blokoftxt = ‘Theres some text in block.’  
replacekey = { ‘T’ = ‘W’, ‘a’ = ‘o’, ‘o’ = ‘a’ }

blokoftxt.each ( if equal to a replacekey, replace with key value )

This would mean going through each character in the textblock. I guess  
one  
could conversely go through the replacekey array and match to the  
textblock?? Which way to go?

J Von Rubiginner

---

<div class="post-metadata">

**Author:** ![grrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@grrr](https://www.ruby-forum.com/u/grrr)\
**Post date:** [February 15, 2006, 12:45am UTC](https://www.ruby-forum.com/t/simple-programming-q/53826/2 "2006-02-15T00:45:50Z")

</div>

On 2/14/06, grrr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> replacekey = { ‘T’ = ‘W’, ‘a’ = ‘o’, ‘o’ = ‘a’ }
> 
> blokoftxt.each ( if equal to a replacekey, replace with key value )

Here’s a simple answer, using a regular expression.

blokoftxt = ‘Theres some text in block.’  
replacekey = { ‘T’ =\> ‘W’, ‘a’ =\> ‘o’, ‘o’ =\> ‘b’ }  
replacekey.each {|key, val| blokoftxt.gsub!(/#{key}/, val)}

---

<div class="post-metadata">

**Author:** ![grrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@grrr](https://www.ruby-forum.com/u/grrr)\
**Post date:** [February 15, 2006, 12:51am UTC](https://www.ruby-forum.com/t/simple-programming-q/53826/3 "2006-02-15T00:51:53Z")

</div>

> #example pseudocode idea  
> blokoftxt = ‘Theres some text in block.’  
> replacekey = { ‘T’ = ‘W’, ‘a’ = ‘o’, ‘o’ = ‘a’ }

you can use String.gsub! to replace a pattern

replacekey.each\_key {|key, value| blockoftext.gsub!(key, val) }

this works for your example, but not for something like {’.’ =\> ‘t’}  
because first parameter of gsub is a regexp!!

---

<div class="post-metadata">

**Author:** ![grrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@grrr](https://www.ruby-forum.com/u/grrr)\
**Post date:** [February 15, 2006, 1:03am UTC](https://www.ruby-forum.com/t/simple-programming-q/53826/4 "2006-02-15T01:03:59Z")

</div>

grrr wrote:

> replacekey = { ‘T’ = ‘W’, ‘a’ = ‘o’, ‘o’ = ‘a’ }

text = \<\<-ENDTEXT  
The Owl and the Pussy-cat went to sea  
In a beautiful pea green boat,  
They took some honey, and plenty of money,  
Wrapped up in a five pound note.  
The Owl looked up to the stars above,  
And sang to a small guitar,  
‘O lovely Pussy! O Pussy my love,  
What a beautiful Pussy you are,  
You are,  
You are!  
What a beautiful Pussy you are!’  
ENDTEXT

replace\_key = { ‘O’ =\> ‘d’, ‘P’ =\> ‘w’, ‘b’ =\> ‘x’}  
new\_text = text.gsub(/[#{replace\_key.keys.join}]/) {|match|  
replace\_key[match]  
}  
puts new\_text

The result is:

The dwl and the wussy-cat went to sea  
In a xeautiful pea green xoat,  
They took some honey, and plenty of money,  
Wrapped up in a five pound note.  
The dwl looked up to the stars axove,  
And sang to a small guitar,  
‘d lovely wussy! d wussy my love,  
What a xeautiful wussy you are,  
You are,  
You are!  
What a xeautiful wussy you are!’

---

<div class="post-metadata">

**Author:** ![grrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@grrr](https://www.ruby-forum.com/u/grrr)\
**Post date:** [February 15, 2006, 12:54am UTC](https://www.ruby-forum.com/t/simple-programming-q/53826/5 "2006-02-15T00:54:56Z")

</div>

On Feb 14, 2006, at 3:33 PM, grrr wrote:

> #example pseudocode idea  
> blokoftxt = ‘Theres some text in block.’  
> replacekey = { ‘T’ = ‘W’, ‘a’ = ‘o’, ‘o’ = ‘a’ }
> 
> blokoftxt.each ( if equal to a replacekey, replace with key value )

blokoftxt.tr ‘Tao’, ‘Woa’

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![grrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@grrr](https://www.ruby-forum.com/u/grrr)\
**Post date:** [February 15, 2006, 12:50pm UTC](https://www.ruby-forum.com/t/simple-programming-q/53826/6 "2006-02-15T12:50:42Z")

</div>

Re:

> > Eric H.:  
> > blokoftxt.tr ‘Tao’, ‘Woa’

On Wed, 15 Feb 2006 10:12:41 +0900, Harold H. wrote:

> Haha! That’s awesome. (sorry, I’m easily amused)
> 
> -Harold

Yes indeed! Hehe I guess I am easily amused, too!

Thats pretty much the solution I was looking for, even if I was  
expecting  
replies from Knuths books. Thanks Eric!

---

<div class="post-metadata">

**Author:** ![grrr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@grrr](https://www.ruby-forum.com/u/grrr)\
**Post date:** [February 15, 2006, 2:13am UTC](https://www.ruby-forum.com/t/simple-programming-q/53826/7 "2006-02-15T02:13:24Z")

</div>

Haha! That’s awesome. (sorry, I’m easily amused)

-Harold
