1

I'm trying to make this test pass and am not sure how to go about making this pass.

TEST

def test_it_is_thirsty_by_default
  vampire = Vampire.new("Count von Count")
  assert vampire.thirsty?
end

def test_it_is_not_thirsty_after_drinking
  vampire = Vampire.new("Elizabeth Bathory")
  vampire.drink
  refute vampire.thirsty?
end

CODE

def thirsty?
  true
end

def drink
  thirsty? === false
end

It is giving a failure message on the last test:

Failed refutation, no message given

What am I missing? My thought is that initially, the vampire is thirsty(true) and then defined a method that would then make the vampire not thirsty(false).

EDIT

Even if I reassign the drink method to:

thirsty? = false

I get a syntax error pointing to the = sign.

mph85
  • 1,276
  • 4
  • 19
  • 39
  • You haven't defined the class `Vampire`, for which `thirsty?` and `drink` are presumably instance methods. The class also needs an `initialize` method. Why `===` rather than `==` in `drink`? – Cary Swoveland Jun 03 '19 at 03:35
  • oh i didn't include the rest of the code, but I did define a class `Vampire` and an `initialize` method that has `instance variables` of `name` and `pet` – mph85 Jun 03 '19 at 04:01
  • It's just a personal preference to use `===` over `==` – mph85 Jun 03 '19 at 04:02
  • 1
    Your example code should be complete enough for readers to run it and/or modified code without having to guess how gaps are to be filled in. I suggest you edit. Incidentally, I just happened to see your comments that reply to my earlier comment. For me to be notified by SO you need to include @username. – Cary Swoveland Jun 03 '19 at 05:32
  • 1
    "It's just a personal preference to use `===` over `==`" – Huh? The two mean completely different things, the question of "preference" doesn't even apply. – Jörg W Mittag Jun 03 '19 at 06:37
  • @JörgWMittag i just read up on ruby equality operators and it seems that it's different from javascript. Is `===` only used in case statements? – mph85 Jun 03 '19 at 07:05
  • @mph85 `===` is the method called by [`case`](http://ruby-doc.org/core-2.6.3/doc/syntax/control_expressions_rdoc.html#label-case+Expression), hence "case equality". You rarely call it explicitly. – Stefan Jun 03 '19 at 09:51
  • @Stefan case would the ruby equivalent to Javascript's switch statements? – mph85 Jun 03 '19 at 18:03
  • @mph85 yes, with a few differences. – Stefan Jun 04 '19 at 05:04
  • @Stefan so you're saying that in Javascript you can do things like compare if `1 === '1' (false)`, but you can't do `1 === '1'` in ruby unless this was in a case statement? – mph85 Jun 04 '19 at 05:51
  • @mph85 no no no. JavaScript's `===` and Ruby's implementations for `===` are very different. In Ruby each class can implement its own `===` to provide meaningful behavior when used as a "pattern" in a `case` expression. For example, [`Range#===`](http://ruby-doc.org/core-2.6.3/Range.html#method-i-3D-3D-3D) returns `true` if the argument is an element of the range. This allows you to write `case 1` and match it via `when 0..2`. Internally, `(0..2) === 1` is called, which returns `true`. – Stefan Jun 04 '19 at 06:38
  • @Stefan so in my example above, I was questioned about my use of `===`, what's wrong there? – mph85 Jun 04 '19 at 07:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194415/discussion-between-stefan-and-mph85). – Stefan Jun 04 '19 at 07:29

1 Answers1

1

You're missing a couple things, most importantly some kind of writer method that allows you to store that fact that @thirsty is getting updated inside of your drink method call

There's a couple different ways to do this but I've shown one below with a few notes:

require 'test/unit'

class Vampire
  def initialize(name)
    @name = name
    @thirsty = true # true by default
  end

  def drink
    @thirsty = false # updates @thirsty for the respective instance
  end

  def thirsty?
    @thirsty
  end
end

class VampireTests < Test::Unit::TestCase
  def test_it_is_thirsty_by_default
    vampire = Vampire.new("Count von Count")
    assert vampire.thirsty?
  end

  def test_it_is_not_thirsty_after_drinking
    vampire = Vampire.new("Elizabeth Bathory")
    vampire.drink
    refute vampire.thirsty?
  end
end
Jay Dorsey
  • 3,563
  • 2
  • 18
  • 24
  • hm i tried the `alias_method` and it gave me `alias_method: undefined method `thirsty' for class `Vampire' (NameError)` – mph85 Jun 03 '19 at 04:21
  • how would i solve this if i just keep the `def thirsty?` method – mph85 Jun 03 '19 at 04:22
  • 2
    @mph85 it should probably be `attr_reader :name, :thirsty`. But you can also implement it as a stand-alone method yourself: `def thirsty? ; @thirsty ; end` (semicolon = newline). It merely returns the value of `@thirsty`, i.e. either `true` or `false`. – Stefan Jun 03 '19 at 10:02
  • @Stefan is correct, I copy/pasted a typo. If you update the reader as noted, it should work. I updated my answer to add it as a reader – Jay Dorsey Jun 03 '19 at 13:01
  • I edited this to remove the attr_reader/alias based on feedback from a follow up question. It wasn't necessary and looking back on it I don't even know why I had the alias and the attr_reader to begin with. – Jay Dorsey Jun 09 '19 at 01:35