Chaos Project

General => Electronic and Computer Section => Programming / Scripting / Web => Topic started by: fugibo on November 24, 2009, 07:29:39 pm

Title: Ruby rot13 Script
Post by: fugibo on November 24, 2009, 07:29:39 pm
I made this a while back and just converted it into an executable library, so I'd thought I'd post it here. The code is probably horribly inefficient, but it works.
#! /usr/bin/ruby
#  This is an executable Ruby library that adds rot13 functinality to the String class,
#  and can encrypt/decrypt STDIN.

class String

def rot13!
size.times do |i|
if self[i].between? 65, 90
self[i] += 13
self[i] = 64 + self[i] - 90 if self[i] > 90
elsif self[i].between? 97, 122
self[i] += 13
self[i] = 96 + self[i] - 122 if self[i] > 122
end
end

return self
end

def rot13
return self.dup.rot13!
end

end

begin
if $0 == __FILE__
input = ''
while ( b = STDIN.gets ) != nil
input << b
end

STDOUT.print input.rot13
end
rescue
nil
end


Enjoy.

EDIT: rot13 (http://en.wikipedia.org/wiki/rot13)
Title: Re: Ruby rot13 Script
Post by: Blizzard on November 25, 2009, 03:08:23 am
Why don't you just modulate? o.o

			if self[i].between? 65, 90
self[i] = (self[i] - 65 + 13) % 26 + 65
elsif self[i].between? 97, 122
self[i] = (self[i] - 97 + 13) % 26 + 97
end