Old Ruby Script : Rnaarerge ltreets iisnde wdors and sltil raed tehm

This little gem comes from October 20, 2006. I had just heard about the research saying that as long as you keep the first and last letters of a word in place the order of the other letters doesn’t matter. Supposedly your mind will still figure out the correct order. It turns out that this e-mail forward wasn’t as impressive as it made out to be, but the script was fun to make anyways. Here’s what the e-mail said:

Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn’t mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteer be at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe.

I was playing with Ruby at the time, so I put together a short script to make those kinds of sentences. Here’s the code:

#!/usr/bin/env ruby
puts "Enter your phrase to be mixed"
def swap(a,b) ($x[a],$x[b] = $x[b],$x[a]) if (a > 0 && a < $x.length - 2) end
myarray = STDIN.readline.split(' ').each {|$x| $x.split('').each_index { |w| swap(w, rand($x.length - 2) + 1)}}
puts myarray.join(' ')

If you want to try it, save it to as a Ruby file on a computer with Ruby installed, and run it from the command line. It will ask you for a line of text. Each word (or space separated group of characters) will keep its first and last character in place, while the others will be mixed up.

Apparently I used this script to explain Ruby to someone, as I also found an explanation file:

#!/usr/bin/env ruby

# prints the prompt
puts "Enter your phrase to be mixed"

# defines a new subroutine named 'swap(a,b)'. swap takes two parameters
def swap(a,b)
# In ruby, you can add an if after the operations, as I did here. The operation swaps the items at indexes a and b
# The if part makes sure we're not swapping the first or last charachters
($x[a],$x[b] = $x[b],$x[a]) if (a > 0 && a < $x.length - 2)
# End of the subroutine

#sets the variable myarray to the results of the operation
#STDIN.readline -- Get one line from stdin (ie. from the terminal)
#.split(' ') -- split the preceding object on each space and put into an array
#.each -- for each element in the array (handle defined here as global variable |$x|) do what follows the variable declaration
#$x.split('') -- split on each letter and put into the array
#.each_index -- similar to each, but instead, the variable (|w|) is the index of each element, one at a time
#swap(w,z) -- call the swap subroutine with parameters of w and z
#rand($x.length -2) + 1) -- gives a random index in the word not including the first or last letter
myarray = STDIN.readline.split(' ').each {|$x| $x.split('').each_index { |w| swap(w, rand($x.length - 2) + 1)}}

#prints each element of the myarray variable with a space in between
puts myarray.join(' ')

So that’s that. Maybe it’ll help with learning Ruby? Or making funny sentences? Or wasting your time?

This entry was posted in Computers, Something Interesting and tagged . Bookmark the permalink.

2 Responses to Old Ruby Script : Rnaarerge ltreets iisnde wdors and sltil raed tehm

  1. Dad says:

    Taht’s cool. It mkeas it oaky to be a bad sleeper.

  2. Dad says:

    Sorry, I spelled the last word wrong. But you get the idea.

Leave a Reply

Your email address will not be published. Required fields are marked *