Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 2, 2016 23:09
-
-
Save NathanSass/8374717 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle class challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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