Skip to content

Instantly share code, notes, and snippets.

@VikiAnn
Last active August 29, 2015 13:55
Show Gist options
  • Save VikiAnn/8754948 to your computer and use it in GitHub Desktop.
Save VikiAnn/8754948 to your computer and use it in GitHub Desktop.
jumpstartlab objects-and-methods exercise

Note from Viki: working on exercise 2. I'm on bag.rb. Tests were provided, I'm running bag_test.rb and running into issues on line 90, which is testing my method take in bag.rb starting on line 34.

Objects and Methods

Exercise 1

If you'd like to be walked through the exercise, check out the Objects and Methods workshop on the Jumpstart Lab tutorials site.

Make the tests pass in the following sequence:

  • test/candy_test.rb
  • test/bag_test.rb
  • test/costume_test.rb
  • test/trick_or_treater_test.rb

Run a test file by calling it with ruby:

$ ruby test/bag_test.rb

Exercise 2

Each object has become a little bit more complex.

Make the tests pass in the following sequence:

  • test/candy_test.rb
  • test/bag_test.rb
  • test/costume_test.rb
  • test/trick_or_treater_test.rb
class Bag
def initialize
@candies = []
end
def candies
@candies
end
def empty?
candies.count == 0
end
def count
candies.count
end
def <<(candy)
candies << candy
end
def contains?(type)
candies.any? do |candy|
candy.type == type
end
end
def grab(piece)
candy_piece = candies.find { |candy| candy.type == piece}
candies.delete(candy_piece)
end
def take(number)
removed_candies = candies.take(number)
removed_candies.each { |candy| candies.delete(candy)}
end
end
gem 'minitest', '~> 5.2'
require 'minitest/autorun'
require 'minitest/pride'
require_relative '../lib/bag'
require_relative '../lib/candy'
class BagTest < Minitest::Test
def test_a_new_bag_is_empty
assert Bag.new.empty?
end
def test_count_the_candies_in_an_empty_bag
assert_equal 0, Bag.new.count
end
def test_empty_bag_has_no_candies
assert_equal [], Bag.new.candies
end
def test_put_candy_in_the_bag
bag = Bag.new
candy = Candy.new("Sour frogs")
bag << candy
assert_equal [candy], bag.candies
end
def test_a_bag_with_candies_is_not_empty
bag = Bag.new
bag << Candy.new("Nerds")
refute bag.empty?
end
def test_bag_counts_candies
bag = Bag.new
bag << Candy.new("Caramelized Almonds")
assert_equal 1, bag.count
end
def test_bag_contains_candies_and_candies_have_a_type
bag = Bag.new
bag << Candy.new("Hershey's Kisses")
# You usually don't want to chain a bunch of different
# types of things together like this.
# We'll talk about it more in a few weeks.
# It's important to understand how these methods work, though.
type = bag.candies.first.type
assert_equal "Hershey's Kisses", type
end
def test_ask_bag_if_it_contains_a_particular_type_of_candy
bag = Bag.new
bag << Candy.new("Lindt chocolate")
assert bag.contains?("Lindt chocolate")
refute bag.contains?("Hershey's chocolate")
end
def test_get_a_particular_type_of_candy
bag = Bag.new
bag << Candy.new("Jawbreaker")
bag << Candy.new("Jawbreaker")
bag << Candy.new("Jolly Ranchers")
bag << Candy.new("100 Grand")
candy = bag.grab "Jawbreaker"
assert_equal "Jawbreaker", candy.type
end
def test_grabbing_candy_removes_it_from_the_bag
bag = Bag.new
bag << Candy.new("Reese's Pieces")
bag << Candy.new("Junior Mints")
bag << Candy.new("Reese's Pieces")
assert_equal 3, bag.count
candy = bag.grab "Reese's Pieces"
assert_equal 2, bag.count
end
def test_take_a_number_of_candies_from_the_bag
bag = Bag.new
bag << Candy.new("Swedish Fish")
bag << Candy.new("Milky Way")
bag << Candy.new("Cotton Candy")
assert_equal 3, bag.count
taken = bag.take(2)
assert_equal 2, taken.size
assert_equal 1, bag.count
end
def test_take_one_candy
bag = Bag.new
bag << Candy.new("Lifesavers")
candy = bag.take(1)
assert_equal "Lifesavers", candy.type
end
end
class Candy
def initialize(candy, sugar=100)
@candy = candy
@sugar = sugar
end
def type
@candy
end
def sugar
@sugar
end
end
gem 'minitest', '~> 5.2'
require 'minitest/autorun'
require 'minitest/pride'
require_relative '../lib/candy'
class CandyTest < Minitest::Test
def test_candy_type
candy = Candy.new("Skittles")
assert_equal "Skittles", candy.type
end
def test_other_type_of_candy
candy = Candy.new("Mars")
assert_equal "Mars", candy.type
end
def test_amount_of_sugar_is_100_by_default
candy = Candy.new("Circus Peanuts")
assert_equal 100, candy.sugar
end
def test_amount_of_sugar_is_configurable
candy = Candy.new("Pop Rocks", 78)
assert_equal 78, candy.sugar
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment