Skip to content

Instantly share code, notes, and snippets.

@jch
Created October 26, 2011 19:04
Show Gist options
  • Save jch/1317437 to your computer and use it in GitHub Desktop.
Save jch/1317437 to your computer and use it in GitHub Desktop.
skip every 3 numbers in a list
(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