Skip to content

Instantly share code, notes, and snippets.

@Shmuwol
Last active August 29, 2015 14:17
Show Gist options
  • Save Shmuwol/179132028882c23dafa5 to your computer and use it in GitHub Desktop.
Save Shmuwol/179132028882c23dafa5 to your computer and use it in GitHub Desktop.
Enumerable#cycle.rb
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

Now if we do this.

days_of_week.cycle {|day| puts day}

it will return all the days of the week over and over, forever.

Now if we do this.

days = days_of_week.cycle

we can then do this;

days.next

and it will cycle through the week and start over again once it cycled through like this;

days.next

will give you "Monday"

days.next

will give you "Tuesday"

days.next

wil give you "Wednesday"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment