cjl85
cjl85

Reputation: 165

Ruby: Dealing with Booleans

TDD

gem 'minitest', '~> 5.2'
require 'minitest/autorun'
require 'minitest/pride'
require_relative 'santa'

class SantaTest < Minitest::Test

 def test_santa_fits_down_the_chimney
  santa = Santa.new
  assert santa.fits?, "Santa fits down the chimney"
 end

 def test_if_santa_eats_too_many_cookies_he_does_not_fit_down_the_chimney

  santa = Santa.new
  santa.eats_cookies
  assert santa.fits?, "He still fits"

  santa.eats_cookies
  assert santa.fits?, "It's a bit of a sqeeze"

  santa.eats_cookies
  refute santa.fits?, "Good thing his suit is stretchy or that wouldn't 
  fit in that either"
 end
end

CODE

class Santa
 attr_reader :eats_cookies

 def initialize
  @eats_cookies = eats_cookies
 end

 def fits?
  true unless @eats_cookies > 2
 else
  false
 end
end

Any direction as to what I can write to get the test to pass on the last test? I'm having an issue organizing the if/then, unless/else statements to make the test pass.

Am I on the right track or am I far off? Appreciate any help

Upvotes: 0

Views: 51

Answers (1)

dinjas
dinjas

Reputation: 2125

I'm inferring what you're trying to do here, but I think I would do something like this:

class Santa
  attr_accessor :cookies_eaten

  def initialize
    @cookies_eaten = 0
  end

  def eat_cookies
    self.cookies_eaten += 1
  end

  def fits?
    cookies_eaten <= 2
  end
end

Upvotes: 1

Related Questions