Skip to content

Instantly share code, notes, and snippets.

@kidGodzilla
Last active August 29, 2015 14:12
Show Gist options
  • Save kidGodzilla/d12d9590eeb9f6a979d9 to your computer and use it in GitHub Desktop.
Save kidGodzilla/d12d9590eeb9f6a979d9 to your computer and use it in GitHub Desktop.
let rubyInfluenceJavaScript
//let rubyInfluenceJavaScript
Number.prototype.times = function(f){for(a=-1;++a<this;){f(a)}return+this};
Number.prototype.upto = function(a,f){b=this;if(a<b){return+b}for(;b<=a;){f(b++)}return+b};
Number.prototype.downto = function(a,f){b=this;if(a>b){return+b}for(;b>=a;){f(b--)}return+b};
Number.prototype.to = function(a,f){a>this?this.upto(a, f):this.downto(a, f)};
Number.prototype.toS = function(){return ""+this};
String.prototype.toI = function(){return parseInt(this)};
String.prototype.toF = function(){return parseFloat(this)};
// ruby-inspired looping
(5).times(function(i){console.log(i)});
(1).upto(0, function(i){console.log(i)});
(15).downto(16, function(i){console.log(i)});
// made-up but legit-sounding to method
(1).to(10, function(i){console.log(i)});
(10).to(5, function(i){console.log(i)});
// Conversion methods
console.log((5).toS())
console.log("75".toI())
console.log("75.4563435".toF())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment