Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NathanSass/8374717 to your computer and use it in GitHub Desktop.
Save NathanSass/8374717 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class Drawer
attr_reader :contents
# Are there any more methods needed in this class?
def initialize
@contents = []
@open = true
end
def open
@open = true
end
def close
@open = false
end
def add_item(item)
@contents << item
end
def remove_item(item = @contents.pop) #what is `#pop` doing? - default value of removing the last item in the contents array
#puts "#{item} was removed"
@contents.delete(item) #Not working
#could throw an error if the item is not founf
end
def dump # what should this method return? empty, and reset the value of contents to indicate empty
puts "Your drawer is empty."
@contents = []
end
def new_drawer
@contents << Silverware.new("fork")
@contents << Silverware.new("knife")
@contents << Silverware.new("spoon")
end
def view_contents
puts "The drawer contains:"
@contents.each {|silverware| puts "- " + silverware.type }
#if statement if the drawer is empty
end
end
class Silverware
attr_reader :type
# Are there any more methods needed in this class?
def initialize(type, clean = true)
@type = type
@clean = clean
end
def eat
puts "eating with the #{type}"
@clean = false
end
def polish
puts "the #{type} has been polished"
end
def stab
if @type == "knife"
puts "You just killed a man"
@clean = false
else
puts "A #{type} is not great for stabbing, try something else"
end
end
def clean_silverware
puts "the #{type} has been washed with soap"
@clean = true
#could have .chomp statment asking if you want to polish an item.
end
end
# knife1 = Silverware.new("knife") #returns an instance of the class
silverware_drawer = Drawer.new #also returns an instance of class, does not need anything to initialize
# silverware_drawer.add_item(knife1)
# silverware_drawer.add_item(Silverware.new("spoon"))
# silverware_drawer.add_item(Silverware.new("fork"))
# silverware_drawer.view_contents #shows the array formatted as indicated in the method
# silverware_drawer.remove_item
# silverware_drawer.view_contents #array with last item removed
# sharp_knife = Silverware.new("sharp_knife")
# silverware_drawer.add_item(sharp_knife)
# silverware_drawer.view_contents #formatted array with sharp knife as 3rd element
# removed_knife = silverware_drawer.remove_item(sharp_knife)
# removed_knife.eat #formated statement with sharp_knife
# removed_knife.clean_silverware #need a new method similar to #eat
# silverware_drawer.view_contents
# silverware_drawer.dump
# silverware_drawer.view_contents #What should this return? should be empty
fork = Silverware.new("fork")
silverware_drawer.add_item(fork)
silverware_drawer.view_contents
# silverware_drawer.remove_item
# silverware_drawer.remove_item(fork) #add some puts statements to help you trace through the code...
# puts silverware_drawer.view_contents
# fork.eat
fork.stab
silverware_drawer.new_drawer
silverware_drawer.view_contents
#BONUS SECTION
puts fork.clean_silverware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment