gpt4 book ai didi

ruby - 如何重构此Ruby代码以检查有效参数?

转载 作者:行者123 更新时间:2023-12-03 08:04:32 25 4
gpt4 key购买 nike

有没有更好(更有说服力的)方法来检查有效参数?

def load_data filename, start_percent, end_percent
raise 'Values must be [0,1]' if start_percent < 0
raise 'Values must be [0,1]' if end_percent < 0
raise 'Values must be [0,1]' if start_percent > 1
raise 'Values must be [0,1]' if end_percent > 1
...

Ruby 1.9.3

编辑: start_percentend_percent应该是浮点数。

最佳答案

发生此错误时,您需要了解详细信息(什么样的错误,什么参数,什么无效值):

def load_data filename, start_percent, end_percent
raise ArgumentError, "start_percent must be [0,1]; received #{start_percent}." unless start_percent.between?(0, 1)
raise ArgumentError, "end_percent must be [0,1]; received #{end_percent}." unless end_percent.between?(0, 1)
end

关于ruby - 如何重构此Ruby代码以检查有效参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13534727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com