# \[SOLUTION\] Quiz #76

**URL:** <https://www.ruby-forum.com/t/solution-quiz-76/58573>\
**Category:** Ruby\
**Created:** [April 23, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/solution-quiz-76/58573 "2006-04-23T15:49:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse\_H-K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@Jesse\_H-K](https://www.ruby-forum.com/u/Jesse_H-K)\
**Post date:** [April 23, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/solution-quiz-76/58573/1 "2006-04-23T15:49:13Z")

</div>

I sincerely hope I am doing this correctly, as…

(1) I have never posted to a mailing list.  
(2) I have never participated in Ruby Q.  
(3) I really hope I am posting at the right time, place, etc

I apologise if I did this incorrectly.

Here was my solution to the Ruby Q.:

#By Jesse H-K  
#On the date of Sunday April 23, 2006

class String  
#Take a string, and scramble the center of each word.  
def scramble  
#find each word and replace it with…  
gsub(/\w+/) do |word|  
if (1…3) === word.length  
#…the word if it’s length is 1, 2, or 3. These words cannot be  
scrambled.  
word  
else  
#…the first character, plus the scrambled middle, and ending with  
the last character.  
word[0].chr + word[1…(-2)].scan(/./).sort\_by { rand }.join +  
word[-1].chr  
end  
end  
end  
end

while str = gets  
puts str.scramble  
end

I don’t have any formal education with programming, but this was an  
easier challenge, so I thought I’d try my hand at it.

Since nothing in the String could be affected besides words, I figured I  
would use the built in String#gsub . I simply find each word using the  
regex /\w+/ (which will find 1 or more letters), and return it if its  
length is from 1 to 3. This is because these words cannot be altered in  
any way. For all the other words, I take the first character, and  
seperate the middle of the string into and Array of letters using  
String#scan. This Array is randomely shuffled with Enumerable#sort\_by,  
and join back tpogether into a String. Lastly, the final character is  
appended.

To use my example, either run with a file as a command line argument, or  
enter text as an input-result loop.

This was a great way to get involved in Ruby Q… Thanks to all those  
that organize it.

---

<div class="post-metadata">

**Author:** ![Jesse\_H-K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@Jesse\_H-K](https://www.ruby-forum.com/u/Jesse_H-K)\
**Post date:** [April 23, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/solution-quiz-76/58573/2 "2006-04-23T21:24:29Z")

</div>

On Apr 23, 2006, at 8:49 AM, Jesse H-K wrote:

> I sincerely hope I am doing this correctly, as…
> 
> (1) I have never posted to a mailing list.  
> (2) I have never participated in Ruby Q.  
> (3) I really hope I am posting at the right time, place, etc
> 
> I apologise if I did this incorrectly.

Looks perfect to me.

> I don’t have any formal education with programming, but this was an  
> easier challenge, so I thought I’d try my hand at it.

You did a nice job. Thanks for sending it in!

> Since nothing in the String could be affected besides words, I  
> figured I  
> would use the built in String#gsub . I simply find each word using the  
> regex /\w+/ (which will find 1 or more letters)

And numbers and _s. 😉 \w is equivalent to [A-Za-z0-9_], just FYI.

James Edward G. II
