Created
August 25, 2015 04:48
-
-
Save carolineartz/7d2f72386599a2622fdc to your computer and use it in GitHub Desktop.
example
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
require 'forwardable' | |
GroceryItem = Struct.new(:name, :quantity, :in_cart) do | |
def to_s | |
text = "#{quantity} : #{name}" | |
in_cart ? "√ #{text}" : " #{text}" | |
end | |
end | |
class GroceryList | |
extend Forwardable | |
def_delegator :@list, :push, :add | |
def_delegator :@list, :delete, :remove | |
def initialize | |
@list = [] | |
end | |
def check(item) | |
item.in_cart = true | |
end | |
def display | |
banner | |
@list.each { |item| puts item } | |
end | |
private | |
def banner | |
puts "~~~~~~~~ Grocery List ~~~~~~~~" | |
30.times { print "~" }; puts | |
end | |
end | |
my_list = GroceryList.new | |
bread = GroceryItem.new("bread", "1 loaf") | |
milk = GroceryItem.new("milk", "1 half gallon") | |
cheese = GroceryItem.new("cheddar cheese", "1 block") | |
cereal = GroceryItem.new("chocolate chex", "18 boxes") | |
fruit = GroceryItem.new("apples", "1 bushel") | |
my_list.add(bread, milk, cheese, cereal, fruit) | |
my_list.display | |
puts | |
my_list.remove(fruit) | |
my_list.display | |
puts | |
my_list.check(milk) | |
my_list.check(cereal) | |
my_list.display |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment