gpt4 book ai didi

ruby - 强制方法调用

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:20 24 4
gpt4 key购买 nike

我正在尝试创建 DSL。我正在使用 http://jroller.com/rolsen/entry/building_a_dsl_in_ruby作为指南。

鉴于此 DSL:

question 'Who was the first president of the USA?'
wrong 'Fred Flintstone'
wrong 'Martha Washington'
right 'George Washington'
wrong 'George Jetson'

有没有办法确保 question() 至少被调用一次,right() 正好被调用一次,wrong() 被调用 n 次或更多次?

最佳答案

当然。只需添加行

(@question_count ||= 0) += 1

无论您当前对 question 方法的实现是什么样的(对于 rightwrong 也是类似的),然后检查这些变量。

关于ruby - 强制方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6481843/

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