# Help with rather simple method

**URL:** <https://www.ruby-forum.com/t/help-with-rather-simple-method/103174>\
**Category:** Ruby\
**Created:** [May 2, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/help-with-rather-simple-method/103174 "2007-05-02T18:35:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deniz\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@Deniz\_D](https://www.ruby-forum.com/u/Deniz_D)\
**Post date:** [May 2, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/help-with-rather-simple-method/103174/1 "2007-05-02T18:35:49Z")

</div>

Hello.

I’m trying to come up with the “best”, most Ruby-ish way to write a  
method such as this one:

# This method replaces randomly selected characters in ‘string’ with new

# randomly selected characters of [A-Za-z0-9]. However, it does not

# replace any character with the character itself, i.e., it does not

# replace any “a” with “a”. Also, no position in the string will ever be

# replaced twice.

# ‘string’ is the String to perform the replacing on.

# ‘amount’ is the amount of different characters to replace.

def random\_replace(string, amount)  
…  
end

You don’t have to care about any error checking of course.

Any ideas? The solution I’ve come up with (but which is yet to be  
implemented) does not feel Ruby-ish at all and could probably be  
implemented in pretty much any other programming language.

And no, this is not school work, no matter what you think.

-Deniz D.

---

<div class="post-metadata">

**Author:** ![Deniz\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@Deniz\_D](https://www.ruby-forum.com/u/Deniz_D)\
**Post date:** [May 2, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/help-with-rather-simple-method/103174/2 "2007-05-02T18:53:51Z")

</div>

On May 3, 2007, at 1:35 AM, Deniz D. wrote:

> ever be  
> implemented) does not feel Ruby-ish at all and could probably be  
> implemented in pretty much any other programming language.
> 
> And no, this is not school work, no matter what you think.
> 
> -Deniz D.

Lets define that a little better before we get started:  
‘amount’ means what? Number of characters to replace?  
Do you chose random positions in the string to replace?  
How often do you randomize?  
What’s the point?  
Does it need to be reversible?  
Perhaps what you’re looking for is some of the encryption  
algorithms… even very simple ones like ROT13 might give you some  
ideas.  
How Ruby-ish it feels is kind of subjective.

---

<div class="post-metadata">

**Author:** ![Deniz\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@Deniz\_D](https://www.ruby-forum.com/u/Deniz_D)\
**Post date:** [May 2, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/help-with-rather-simple-method/103174/3 "2007-05-02T19:35:56Z")

</div>

John J. wrote:

> > # replace any character with the character itself, i.e., it does not
> > 
> > Any ideas? The solution I’ve come up with (but which is yet to be  
> > implemented) does not feel Ruby-ish at all and could probably be  
> > implemented in pretty much any other programming language.
> > 
> > And no, this is not school work, no matter what you think.
> > 
> > -Deniz D.
> 
> Lets define that a little better before we get started:  
> ‘amount’ means what? Number of characters to replace?

Correctly guessed!

> Do you chose random positions in the string to replace?

Yes!

> How often do you randomize?

Once every program run.

> What’s the point?

I’m currently working on a project which is supposed to perform a rather  
odd type of penetration testing on protocol implementations, using  
specification files which describe attacks to perform on the host. This  
method is used in trying to find very simple vulnerabilities, by  
replacing some characters in strings to see what happens if we send  
something unexpected to the server.

> Does it need to be reversible?

Not at all!

---

<div class="post-metadata">

**Author:** ![Deniz\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@Deniz\_D](https://www.ruby-forum.com/u/Deniz_D)\
**Post date:** [May 2, 2007, 7:36pm UTC](https://www.ruby-forum.com/t/help-with-rather-simple-method/103174/4 "2007-05-02T19:36:56Z")

</div>

On May 2, 11:26 am, Deniz D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # ‘string’ is the String to perform the replacing on.
> 
> And no, this is not school work, no matter what you think.
> 
> -Deniz D.

def random\_replace(string, amount)  
locations = (0…string.size).to\_a  
amount.times{  
p = locations.slice!( rand( locations.size ) )  
chars = (‘a’…‘z’).to\_a + (‘A’…‘Z’).to\_a +  
(‘0’…‘9’).to\_a - [string[p,1] ]  
string[p] = chars[rand( chars.size )]  
}  
end

s = ‘foo bar’  
random\_replace( s, 3 )  
puts s
