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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With