在Ruby的Kernel类中添加assert()方法是否是惯用的Ruby? [英] Is it idiomatic Ruby to add an assert( ) method to Ruby's Kernel class?

查看:107
本文介绍了在Ruby的Kernel类中添加assert()方法是否是惯用的Ruby?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过在Ruby中编码等效于Kent Beck的xUnit来扩展我对Ruby的理解. Python(由Kent编写)以广泛使用的语言提供了assert()方法. Ruby没有.我认为添加它应该很容易,但是Kernel是放置它的正确位置吗?

I'm expanding my Ruby understanding by coding an equivalent of Kent Beck's xUnit in Ruby. Python (which Kent writes in) has an assert() method in the language which is used extensively. Ruby does not. I think it should be easy to add this but is Kernel the right place to put it?

顺便说一句,我知道Ruby中存在各种Unit框架-这是一种学习Ruby习语而不是完成某些工作"的练习.

BTW, I know of the existence of the various Unit frameworks in Ruby - this is an exercise to learn the Ruby idioms, rather than to "get something done".

推荐答案

不,这不是最佳实践.在Ruby中对assert()的最好比喻只是提高了

No it's not a best practice. The best analogy to assert() in Ruby is just raising

 raise "This is wrong" unless expr

,如果您想提供更具体的异常处理,则可以实现自己的异常

and you can implement your own exceptions if you want to provide for more specific exception handling

这篇关于在Ruby的Kernel类中添加assert()方法是否是惯用的Ruby?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆