root/ruby/rot13.rb

Revision 147, 1.1 kB (checked in by andrew.pennebak..@gmail.com, 1 year ago)

Added threatlevel.rb.
Added sunset.rb.
Added firefoxcounter.rb.
Added nationaldebt.rb.

Now rot13.rb uses pre-made conversion table.
Moved alchemy to http://code.google.com/p/yellosoft-alchemy/.

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env ruby
2
3 # Author:: Andrew Pennebaker (andrew.pennebaker@gmail.com)
4 # License:: GPLv3
5 # Copyright:: Copyright 2007-2008 Andrew Pennebaker
6 #
7 # == Usage
8 #
9 # rot13 [OPTIONS]
10 #
11 # Data is read from STDIN and written to STDOUT.
12 #
13 # --help, -h:
14 #    Help with usage
15 #
16 # --shift, -s:
17 #    Set Caesar shift (default 13).
18
19 require "getoptlong"
20 require "rdoc/usage"
21
22 def create_rule(n=13)
23         rule={}
24
25         # "A"[0].upto("Z"[0])
26         65.upto(90) { |b|
27                 rule[b]=(b+n-65)%26+65
28         }
29
30         # "a"[0].upto("z"[0])
31         97.upto(122) { |b|
32                 rule[b]=(b+n-97)%26+97
33         }
34
35         return rule
36 end
37
38 def crypt(rule, b)
39         if rule.include?(b)
40                 return rule[b]
41         else
42                 return b
43         end
44 end
45
46 def main
47         shift=13
48
49         begin
50                 opts=GetoptLong.new(
51                         ["--help", "-h", GetoptLong::NO_ARGUMENT],
52                         ["--shift", "-s", GetoptLong::REQUIRED_ARGUMENT]
53                 )
54
55                 opts.each { |option, value|
56                         case option
57                                 when "--help"
58                                         raise
59                                 when "--shift"
60                                         shift=value.to_i
61                         end
62                 }
63         rescue
64                 RDoc::usage("Usage")
65         end
66
67         rule=create_rule(shift)
68
69         STDIN.each_byte { |b|
70                 putc crypt(rule, b)
71         }
72 end
73
74 if __FILE__==$0
75         begin
76                 main
77         rescue Interrupt=>e
78                 nil
79         end
80 end
Note: See TracBrowser for help on using the browser.