Skip to content

Instantly share code, notes, and snippets.

@MaxDavila
Last active December 22, 2015 04:19
Show Gist options
  • Select an option

  • Save MaxDavila/6416363 to your computer and use it in GitHub Desktop.

Select an option

Save MaxDavila/6416363 to your computer and use it in GitHub Desktop.
class Washer
attr_reader :name, :status
@@num_washers = 0
@@num_washers_washing = 0
@@num_washers_rinsing = 0
def initialize(args = nil)
@@num_washers += 1
@name = args[:name]
end
def self.report
[@@num_washers, @@num_washers_washing, @@num_washers_rinsing]
end
def wash!
@@num_washers_washing += 1
@status = :washing
end
def endwash!
@@num_washers_washing -= 1
@status = :inactive
end
def rinse!
@@num_washers_rinsing += 1
@status = :rinsing
end
def endrinse!
@@num_washers_rinsing -= 1
@status = :inactive
end
def to_s
"#{name}"
end
end
w1 = Washer.new(:name => "washer 1")
w2 = Washer.new(:name => "washer 2")
w3 = Washer.new(:name => "washer 3")
w4 = Washer.new(:name => "washer 4")
washers = [w1, w2, w3, w4]
p washers.map{|w| w.wash!}
p washers
p Washer.report
p washers.sample(2).map{|w| w.endwash!}
p washers.map{|w| w.status}
p Washer.report
p washers.select{|w| w.status == :inactive}.map{|w| w.rinse!}
p washers.map{|w| w.status}
p Washer.report
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment