【发布时间】:2015-03-11 14:40:25
【问题描述】:
我环顾四周,找不到这个问题:
对于 ruby koan 280,它告诉我以下下划线部分应该是错误的:
def test_to_str_allows_objects_to_be_treated_as_strings
assert_equal __, File.exist?(CanBeTreatedAsString.new) # test passes, if __ is changed to false
end
好的,好的。但是如何测试to_str 是否允许将对象视为字符串?这是 CanBeTreatedAsString 类,它包含一个 to_str 方法:
class CanBeTreatedAsString
def to_s
"string-like"
end
def to_str
to_s
end
end
...但这与上面的 assert_equal 代码有什么关系? .exist? 需要一个字符串吗?
本页:
http://www.ruby-doc.org/core-2.2.0/File.html#method-c-exist-3F
表示参数可以是 IO 对象。某些方法是否特定于它们接收的参数类型?如果是这样,我怎么知道?
【问题讨论】:
标签: ruby