Created
October 26, 2011 19:04
-
-
Save jch/1317437 to your computer and use it in GitHub Desktop.
skip every 3 numbers in a list
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
(1..18).step(3) {|i| puts (i..i+2).to_a.inspect if i.even?} | |
# Breaking it down: | |
# #step(3) yields every 3rd element in the range, so you start with 1, 4, 7, 10... | |
# in the block, we want to print lists of 3's, so we do (i..i+2).to_a. Giving us: [1,2,3], [4,5,6], [7,8,9]... | |
# now we want to skip every other list of 3, so we only print if i is even. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment