Codewars练习显示在我的系统上正常工作的代码的错误

时间:2015-07-24 18:17:24

标签: ruby

这是问题

更正代码,以便greet函数返回预期值 这是代码

class Person

  def initialize(name)
    @name = name
  end

  def greet(other_name)
   "Hi #{other_name}, my name is #{name}"
  end
end

这是我的解决方案

class Person
  attr_reader :name, :other_name
  def initialize(name)
    @name = name
  end

  def greet(other_name)
    puts " \"Hi #{other_name}, my name is #{@name}\" "
  end
end

object = Person.new("Joe")
object.greet("Kate")

提供以下错误

`expect': Expected: "Hi Kate, my name is Joe", instead got: nil (Test::Error) from `assert_equals' from `
'

1 个答案:

答案 0 :(得分:2)

puts是一种返回nil的方法,因此即使您看到正确的输出,测试也不会那样看。