Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created March 15, 2010 18:20
Show Gist options
  • Select an option

  • Save lukeredpath/333128 to your computer and use it in GitHub Desktop.

Select an option

Save lukeredpath/333128 to your computer and use it in GitHub Desktop.
class Array
def rotate(number_of_times = 1)
returning(self) do
number_of_times.times { unshift(pop) }
end
end
end
# why would you use this?
#
# an array representing the hours in the day, in sequence up to the last hour:
#
# hours_in_day = (0..23).to_a
# hours_up_to_last = hours_in_day.rotate(24 - Time.now.hour)
#
# # assuming its currently 6:30pm
# # => [18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment