Forum: Ruby simple programming q

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F82cffce6c0b609f002236943a6b3301?d=identicon&s=25 grrr (Guest)
on 2006-02-15 00:33
(Received via mailing list)
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
Ff63c03fd68754adbadd2c6314646bef?d=identicon&s=25 Bill Guindon (agorilla)
on 2006-02-15 00:45
(Received via mailing list)
On 2/14/06, grrr <grrr@toto.maatti> 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)}
170bd539f12a1f93b2bd122183ed1cba?d=identicon&s=25 Matthias Ludwig (Guest)
on 2006-02-15 00:51
(Received via mailing list)
> #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!!
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-02-15 00:54
(Received via mailing list)
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 Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
37a3c73ffbf864e4b28f7f2384ee12ce?d=identicon&s=25 Timothy Hunter (tim-hunter)
on 2006-02-15 01:03
(Received via mailing list)
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!'
7f891fbe8e3bae7f9fe375407ce90d9d?d=identicon&s=25 Harold Hausman (Guest)
on 2006-02-15 02:13
(Received via mailing list)
Haha! That's awesome. (sorry, I'm easily amused)

-Harold
F82cffce6c0b609f002236943a6b3301?d=identicon&s=25 grrr (Guest)
on 2006-02-15 12:50
(Received via mailing list)
Re:
>> Eric Hodel:
>> blokoftxt.tr 'Tao', 'Woa'

On Wed, 15 Feb 2006 10:12:41 +0900, Harold Hausman 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!
This topic is locked and can not be replied to.