Skip to content

Instantly share code, notes, and snippets.

@roryokane
Forked from jescalan/scramble.rb
Created May 9, 2012 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roryokane/2649686 to your computer and use it in GitHub Desktop.
Save roryokane/2649686 to your computer and use it in GitHub Desktop.
Sensical sentence scrambler
# ---------------------------
# Sensical Sentence Scrambler
# ---------------------------
# This short program takes any word longer than three characters and randomly shuffles all the characters
# except for the first and the last. Strangely enough, sentences are still quite readable like this.
# **Usage**
# Save the file on your computer as 'scramble.rb'.
# From the command line, run `ruby scramble.rb "Here's my sentence."`, and it should output
# the scrambled version below, e.g. "Hre'es my stnecene.".
# **Support**
# If you are having trouble with this, have questions about how it works, or anything else, feel free
# to email me at jeff.escalante@carrotcreative.com
def scramble_word(word)
word[0].to_s + word[1..-2].chars.to_a.shuffle.join.to_s + word[-1].to_s
end
sentence = ARGV.first.split(" ")
result = []
sentence.each do |word|
if word.length > 3
result << scramble_word(word)
else
result << word
end
end
puts result.join(" ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment