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

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

问题描述

我通过在 Ruby 中编写与 Kent Beck 的 xUnit 等效的代码来扩展我对 Ruby 的理解.Python(由 Kent 编写)在该语言中具有一个广泛使用的 assert() 方法.红宝石没有.我认为添加它应该很容易,但内核是放置它的正确位置吗?

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

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

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