Skip to content

Instantly share code, notes, and snippets.

@rafaelss
Created November 21, 2013 20:59
Show Gist options
  • Save rafaelss/7589524 to your computer and use it in GitHub Desktop.
Save rafaelss/7589524 to your computer and use it in GitHub Desktop.
// node promise.js
function Promise() {
this.callbacks = [];
this.resolved = false;
this.resolve = function(value) {
if(this.resolved) {
throw "Promise already resolved";
}
else {
this.callbacks.forEach(function(cb) {
cb(value);
});
this.resolved = true;
this.value = value;
}
};
this.success = function(cb) {
if(this.resolved) {
cb(this.value);
}
else {
this.callbacks.push(cb);
}
};
}
var foo = new Promise();
var bar = new Promise();
foo.resolve('hello');
setTimeout(function(){
bar.resolve('world');
}, 500);
foo.success(function(result){
console.log(result);
});
bar.success(function(result){
console.log(result);
});
# ruby rotx.rb
def rotx(x, string, encrypt = true)
x = x - 26 while x > 26
x = (26 - x).abs unless encrypt
string.chars.map do |chr|
x.times do
next unless chr =~ /[A-Za-z]/
chr = chr.succ[0]
end
chr
end.join("")
end
raise "wrong!" unless rotx(10, "Hello, World") == "Rovvy, Gybvn"
raise "wrong!" unless rotx(10, "Rovvy, Gybvn", false) == "Hello, World"
raise "wrong!" unless rotx(13, "HELLO") == "URYYB"
raise "wrong!" unless rotx(13, "URYYB", false) == "HELLO"
raise "wrong!" unless rotx(36, "Hello, World") == "Rovvy, Gybvn"
raise "wrong!" unless rotx(36, "Rovvy, Gybvn", false) == "Hello, World"
raise "wrong!" unless rotx(100, "Everlane") == "Aranhwja"
raise "wrong!" unless rotx(100, "Aranhwja", false) == "Everlane"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment