我正在尝试RubyMonk测试以通过https://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/50-debugging/lessons/119-the-debugging-primaries
描述说:
现在,让我们看一个稍微复杂的练习。我实现了类DrivingLicenseAuthority,它决定是否向申请者颁发驾照。但不幸的是,它有缺陷。我不会告诉你所有的细节,所以你必须找出问题出在哪里以及为什么--测试相当乏味。目标是让所有的测试都通过。记住,p是你的朋友。
暗示说
You may want to inspect all the parameters that are being passed to DrivingLicenseAuthority
You may want to ensure that the parameters are being assigned to instance variables correctly.
You may want to inspect the parameters being passed into VisualAcuity, and their types.
You may want to make sure the conditions for passing the driving test are complete.
You may want to make sure the incoming values are converted to the appropriate types before doing numeric operations that might be a float
我试了这个,解决了3个测试中的2个。
class VisualAcuity
def initialize(subject, normal)
@subject = subject.to_f
@normal = normal.to_f
puts "subject: #{@subject}, normal: #{@normal}"
end
def can_drive?
(@subject / @normal) >= 0.5
end
def to_s
"VisualAcuity: subject = #{@subject}, normal = #{@normal}"
end
end
p VisualAcuity
class DrivingLicenseAuthority
def initialize(name, age, visual_acuity)
@name = name
@age = age
@visual_acuity = visual_acuity
puts "name: #{@name}, age: #{@age}, visual_acuity: #{@visual_acuity}"
end
def valid_for_license?
@age \>= 18
end
def verdict
if valid_for_license?
"#{@name} can be granted driving license"
else
"#{@name} cannot be granted driving license"
end
end
end
STDOUT:主题:20.0,正常:20.0姓名:市香,年龄:20,视力:视力:受试者= 20.0,正常= 20.0受试者:20.0,正常:200.0名称:Maita,年龄:20,视力:视力:受试者= 20.0,正常= 200.0受试者:20.0,正常:30.0姓名:Muluc,年龄:20,视力:视力:受试者= 20.0,正常= 30.0
问题是Maita视力不佳,受试者/正常= 0.10,不满足≥ 0.50的要求
Maita视力不好,不应该被允许驾驶,预期“Maita不能被授予驾驶执照”得到“Maita可以被授予驾驶执照”(使用==进行比较)
Maita正在通过can_drive?测试。非常感谢您的帮助
1条答案
按热度按时间u0sqgete1#
def valid_for_license? @age >= 18 && @visual_acuity.can_drive? end
这就解决了对Maita的考验