Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Koans: Where are the quotes in this return value?

I'm working on the following Ruby Koan:

class Dog7
  attr_reader :name

  def initialize(initial_name)
    @name = initial_name
  end

  def get_self
    self
  end

  def to_s
    __
  end

  def inspect
    "<Dog named '#{name}'>"
  end
end

def test_inside_a_method_self_refers_to_the_containing_object
  fido = Dog7.new("Fido")

  fidos_self = fido.get_self
  assert_equal "<Dog named 'Fido'>", fidos_self
end

def test_to_s_provides_a_string_version_of_the_object
  fido = Dog7.new("Fido")
  assert_equal __, fido.to_s
end

The first half of the first assert_equal is what I am trying to fill in. This code gives the error:

<"<Dog named 'Fido'>"> expected but was  <<Dog named 'Fido'>>.

The problem is, I'm stuck on how to match the return value. It looks to me like a string literal return value, but I don't know how to express that without using quote marks, and/or backslashes. Nothing I try seems to work.

Help?

like image 483
nrflaw Avatar asked Nov 22 '11 12:11

nrflaw


2 Answers

After staring at it for a while, again, I figured out where they were going with the lesson. Changing the first assert to "assert_equal fido, fidos_self" made the test pass. I was thrown by the error giving the same output as the inspect method, sans quotes. Thanks for helping me work through it.

like image 110
nrflaw Avatar answered Nov 16 '22 03:11

nrflaw


Changing test_inside_a_method_self_refers_to_the_containing_object to following works:

def test_inside_a_method_self_refers_to_the_containing_object
  fido = Dog7.new("Fido")

  fidos_self = fido.get_self
  assert_equal "<Dog named 'Fido'>", fidos_self.inspect # .inspect added.
end


Ok, were there more gaps to fill? I have an answer, but it seems you already filled a gap incorrectly.

like image 43
Mario Uher Avatar answered Nov 16 '22 01:11

Mario Uher