1 2 3
def rot_encode(s, offset=2) s.split('').map{ |char| (char.ord + offset).chr }.join end
Refactorings
No refactoring yet !
bob
January 18, 2010, January 18, 2010 20:04, permalink
This is not a "rotation" because high things don't wrap back around. e.g. with offset=13 is is not Rot-13. Instead, it just increases the character value by a certain amount. Some characters might be mapped into unprintable characters.
Sam Figueroa
January 19, 2010, January 19, 2010 06:49, permalink
You know what bob. You're right. I didn't notice it at the time but now it is butt obvious to me. So where is your refactoring then :P
Ants
January 19, 2010, January 19, 2010 09:31, permalink
From http://en.wikipedia.org/wiki/Code_refactoring:
>In software engineering, "refactoring" source code means improving it without changing its overall results. The process could be informally referred to as "cleaning up" or "taking out the garbage." Refactoring neither fixes bugs nor adds new functionality, though it might precede either activity. Rather, it improves the understandability of the code, changes its internal structure and design, and removes dead code.
tardface
January 21, 2010, January 21, 2010 19:56, permalink
assuming 26 letter alphabet
1 2 3
def rot_encode(s, offset=2) s.split('').map{ |char| ((char.ord - '0'.ord + offset) % 26 + '0'.ord).chr }.join end
tardface
January 21, 2010, January 21, 2010 19:57, permalink
That should be 'a'.ord, not '0'.ord. Unless you want numbers, as well, but then it's more complicated.
Rotation encoding with variable steps. This could get interesting