-
-
Save MaxPleaner/f3cb68a9faa76250b0f393e6c5498359 to your computer and use it in GitHub Desktop.
Ruby .times & .upto & .downto methods in Coffeescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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