require 'delegate'
class Fred < SimpleDelegator
def initialize(s)
super
end
end
puts Fred.new([]) == [] # ==> true
puts Fred.new({}) == {} # ==> true
puts Fred.new(nil) == nil # ==> true
但Ruby測試::的單元測試零SimpleDelegator
require 'test/unit'
class FredTest < Test::Unit::TestCase
def test_nilliness
assert_nil Fred.new(nil)
end
end
回報...... 運行測試:
˚F
成品測試中0.000501s,1996.0080測試/秒, 1996.0080斷言/ s。
1)失敗: test_nilliness:20 預計nil爲零。
1測試中,1個斷言,1次失敗,0失誤,0跳過
咦? assert_nil是否檢查NilClass?這在這種情況下會失敗。
由於你沒有 指定一個紅寶石版本下面的2個答案應該照顧這個對你的依賴。 – engineersmnky 2014-10-03 21:00:46