Skip to content

Instantly share code, notes, and snippets.

@enderahmetyurt
Created August 21, 2025 12:33
Show Gist options
  • Save enderahmetyurt/9901715aff837ecfa91c4cde9598ab5e to your computer and use it in GitHub Desktop.
Save enderahmetyurt/9901715aff837ecfa91c4cde9598ab5e to your computer and use it in GitHub Desktop.
Write a generator function `createLaundryItem()` that returns an object representing a laundry item. This object should have a method nextCycle() which, when called, advances the item through a series of laundry cycles in order: "soak", "wash", "rinse", "spin", and "dry". After the final cycle, subsequent calls to nextCycle() should return "done".
class LaundryItem
CYCLES = %i[soak wash rinse spin dry].freeze
def initialize
@current_index = -1
end
def next_cycle
if @current_index < CYCLES.size - 1
@current_index += 1
CYCLES[@current_index].to_s.capitalize
else
"Done"
end
end
end
item = LaundryItem.new
puts item.next_cycle
puts item.next_cycle
puts item.next_cycle
puts item.next_cycle
puts item.next_cycle
puts item.next_cycle
puts item.next_cycle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment