Created
April 11, 2011 06:39
-
-
Save ungoldman/913148 to your computer and use it in GitHub Desktop.
Fixing _why's Evil Idea Encoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fixing _why's Evil Idea Encoder | |
============================== | |
Working my way through _why's (poignant) guide to ruby, I noticed that | |
the more complex his examples became, the less often they actually | |
worked when tested. Frustrated by my inability to copy and paste examples | |
into the shell and see them work without doing any real learning, I | |
grudgingly decided to find a way to bring his examples to life and satisfy | |
my obsessive need for results and tidiness. | |
This fix deals with the wordlist.rb, encode.rb and decode.rb scripts | |
found in Section 3 (Chaining Delusions Together) | |
of Chapter 4 (Floating Little Leaves of Code) | |
of why's (poignant) guide to ruby. | |
Read the chapter here: | |
http://mislav.uniqpath.com/poignant-guide/book/chapter-4.html | |
There are two problems with these scripts: | |
------------------------------------------ | |
(1) Ruby's 'require' method can't find wordlist.rb and returns the following error: | |
<internal:lib/rubygems/custom_require>:29:in `require': | |
no such file to load -- wordlist (LoadError) | |
from <internal:lib/rubygems/custom_require>:29:in `require' | |
from encode.rb:1:in `<main>' | |
Ruby can't find wordlist.rb because it's not looking in the local directory. | |
The solution I found was to include the following line | |
at the top of both encode.rb and decode.rb: | |
$:.unshift File.dirname(__FILE__) | |
This ensures that the 'require' method checks | |
the local directory for files to include. | |
Source: | |
http://stackoverflow.com/questions/1061121/ruby-unable-to-use-require | |
(2) Once the first problem is fixed, we get another error: | |
encode.rb:7:in `<main>': | |
undefined local variable or method `code_words' for main:Object (NameError) | |
In this instance, the variable we need is undefined because you can't | |
import a local variable from an external source. There are three possible | |
solutions: make code_words either a constant, a global variable or an | |
object with a new user-defined class. For simplicity's sake I went with | |
a global variable by changing each instance of code_words to $code_words. | |
Source: | |
http://www.falsepositives.com/index.php/2005/09/12/getting-rubys-require-to-work-in-the-poignant-guide-to-ruby/ | |
That's it! You can now encode and decode evil ideas about | |
starmonkeys and Nigeria and catapults to your heart's content. | |
Thanks to _why for his legacy, his lunacy, and his mysterious disappearance. | |
Put the kabosh on @n8ji | |
If anyone knows a better solution to these problems, | |
please complain loudly and obscenely about this gist on an obscure forum, | |
or just send me a message or add a comment at the bottom. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$:.unshift File.dirname(__FILE__) | |
require 'wordlist' | |
# Print each idea out with the words fixed | |
Dir['idea-*.txt'].each do |file_name| | |
idea = File.read( file_name ) | |
$code_words.each do |real, code| | |
idea.gsub!( code, real ) | |
end | |
puts idea | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$:.unshift File.dirname(__FILE__) | |
require 'wordlist' | |
# Get evil idea and swap in code words | |
print "Enter your new idea: " | |
idea = gets | |
$code_words.each do |real, code| | |
idea.gsub!( real, code ) | |
end | |
# Save the jibberish to a new file | |
print "File encoded. Please enter a name for this idea: " | |
idea_name = gets.strip | |
File::open( "idea-" + idea_name + ".txt", "w" ) do |f| | |
f << idea | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$code_words = { | |
'starmonkeys' => 'Phil and Pete, those prickly chancellors of the New Reich', | |
'catapult' => 'chucky go-go', 'firebomb' => 'Heat-Assisted Living', | |
'Nigeria' => "Ny and Jerry's Dry Cleaning (with Donuts)", | |
'Put the kabosh on' => 'Put the cable box on' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment