Ruby Koans:这个返回值中的引号在哪里? [英] Ruby Koans: Where are the quotes in this return value?

查看:54
本文介绍了Ruby Koans:这个返回值中的引号在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究以下 Ruby Koan:

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

第一个 assert_equal 的前半部分是我想填的.这段代码给出了错误:

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.

帮助?

推荐答案

在盯着它看了一会儿之后,我再次弄清楚他们在课程中的去向.将第一个断言更改为assert_equal fido, fidos_self"使测试通过.我被错误抛出,给出与检查方法相同的输出,没有引号.感谢您帮助我解决问题.

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.

这篇关于Ruby Koans:这个返回值中的引号在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆