defrotx(x,string,encrypt=true)letters=('a'..'z').to_astring.split('').map!do|letter|ifletter.match(/[a-zA-Z]/)# Looking for indexindex=letter==letter.downcase?letters.index(letter):letters.index(letter.downcase)# Building rotated index either by moving forward or backwardrotated_index=encrypt?(index+x)%26:(index-x)%26# Building new letter based on rotated indexletter==letter.downcase?letters[rotated_index]:letters[rotated_index].upcaseelseletterendend.joinenddescribe'Test #rotx'doit'should rotate the string and encrypt as default'dorotx(10,'Hello, World').shouldeql'Rovvy, Gybvn'endit'should rotate back the string if encrypt is false'dorotx(10,'Rovvy, Gybvn',false).shouldeql'Hello, World'endit'should return the same results if roration number is added 26'dorotx(36,'Hello, World').shouldeql'Rovvy, Gybvn'endend
A simple compromise implementation, and this will build the foundation in removing callbacks in node.js app.
Promise=function(){this.value;this.callbacks=[];};Promise.prototype.resolve=function(result){if(this.value){thrownewError('A promise should only be able to be resolved once !');}else{this.value=result;}if(this.callbacks.length>0){this.triggerCallbacks();}};Promise.prototype.success=function(fn){this.callbacks.push(fn);if(this.value){this.triggerCallbacks();}};Promise.prototype.triggerCallbacks=function(){varself=this;this.callbacks.forEach(function(callback){callback.call(self,self.value);});}varfoo=newPromise();varbar=newPromise();foo.resolve('hello');setTimeout(function(){bar.resolve('world');},500);foo.success(function(result){console.log(result);});bar.success(function(result){console.log(result);});// Throw errors if one promise tries to resolve twicevarfoobar=newPromise();foobar.resolve('hello');foobar.resolve('world');