Skip to content

Instantly share code, notes, and snippets.

@MaxPleaner
Forked from FGRibreau/times.js
Last active January 27, 2017 15:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaxPleaner/f3cb68a9faa76250b0f393e6c5498359 to your computer and use it in GitHub Desktop.
Save MaxPleaner/f3cb68a9faa76250b0f393e6c5498359 to your computer and use it in GitHub Desktop.
Ruby .times & .upto & .downto methods in Coffeescript
# Ruby = 5.times { |i| puts i }
# Coffee = (1).times (i) -> console.log i
Number.prototype.times = (cb) ->
i = -1;
while (++i < this) then cb(i)
+this
# Ruby = 1.upto(5) { |i| puts i }
# Coffee = (1).upto 5, (i) -> console.log i
Number.prototype.upto = (t, cb) ->
i = this
if (t < this) then return +this
while (i <= t) then cb(i++)
+this
# Ruby = 15.downto(10) { |i| puts i }
# Coffee = (15).downto 10, (i) -> console.log i
Number.prototype.downto = (t, cb) ->
i = this
if (t > this) then return +this
while (i >= t) then cb(i--)
+this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment