Skip to content

Instantly share code, notes, and snippets.

@joshnesbitt
Created October 12, 2010 15:26
Show Gist options
  • Select an option

  • Save joshnesbitt/622360 to your computer and use it in GitHub Desktop.

Select an option

Save joshnesbitt/622360 to your computer and use it in GitHub Desktop.
module IntegerAssertions
def negative?
self < 0
end
def positive?
self > 0
end
end
module Rotate
def rotate(index)
range = if index.positive?
(0..index).to_a
elsif index.negative?
(index..0).to_a
else
[]
end
rotated_indexes = range.to_a.inject([]) do |rotated, i|
(v = self.pop).nil? ? rotated : rotated.push(v)
end
self + rotated_indexes
end
end
Integer.send(:include, IntegerAssertions)
Array.send(:include, Rotate)
puts [1,2,3,4,5].rotate(-5).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment