Ruby rot13 Script

Started by fugibo, November 24, 2009, 07:29:39 pm

Previous topic - Next topic

fugibo

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

Blizzard

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
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.