Skip to content

Instantly share code, notes, and snippets.

@jonelf
Created April 26, 2013 12:10
Show Gist options
  • Save jonelf/5466993 to your computer and use it in GitHub Desktop.
Save jonelf/5466993 to your computer and use it in GitHub Desktop.
Python list slice steps in Ruby.
class Array
alias_method :brackets, :[]
def [](*args)
return brackets(*args) if args.length != 3
start, stop, step = *args
self.values_at(*(start...stop).step(step))
end
end
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr[1,7,2]
#=> [2, 4, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment