[Courses] [Ruby] Lesson 0: Installing, References,
and your first homework assignment
Andrea Landaker
qirien at icecavern.net
Sat Nov 12 04:54:23 EST 2005
I'm a computer programmer turned stay-at-home-mom, who still loves
computers. :-) Most of my programming these days is simple Ruby scripts for
myself or for my website (an audio/image gallery program, recoding mp3s,
etc). In the past I've used Java, C++, and Scheme, but I'm a big fan of
Ruby. I have never really sat down and "learned" it, though -- just started
ploughing through getting things to work. So I'm looking forward to learning
some (probably basic) new things that I hadn't thought I needed but will
probably be useful.
Not sure if I'll make it through the whole course due to the fact that my
1-year-old hates me being on the computer (he likes to turn it off), but
it'll be fun!
OK, for the next part of the homework, here's my husband's interactive
crytpogram solver:
#!/usr/bin/ruby
$code = ARGV.join(' ').upcase
puts $code
print '$ '
STDOUT.flush
while STDIN.gets
if ~ /([A-Za-z]{2}).*/
code = $1[0..0].upcase
text = $1[1..1].upcase
$code.gsub!(code,'!')
$code.gsub!(text,code)
$code.gsub!('!',text)
else
puts "Enter xY to make x -> Y"
end
puts $code
print '$ '
STDOUT.flush
end
The first line puts all the "arguments" (the cryptogram itself) into the
global variable "code", uppercased. It prints it out, and then takes two
letters from the user. The first letter goes in the local variable "code",
and the second goes into "text". Then, throughout the global variable
"code", it swaps the two (using the exclamation point for an intermediary
character). This repeats essentially forever.
--
Andrea Landaker
http://www.icecavern.net/~qirien/
More information about the Courses
mailing list