ruby 如何发出“尚未实施”的信号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13668068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to signal "not implemented yet"?
提问by Franco Rondini
In the initial drafting of a new gem I need to leave some method implementations empty ( to be implemented in the next )
在新 gem 的初始起草中,我需要将一些方法实现留空(将在下一个实现)
Therefore, I would like to signal a "not implemented yet" exception
因此,我想发出一个“尚未实现”的异常信号
I'm wondering if there is a best practiceor standard conventionsspecific to the Ruby language to code this kind of placeholder / exception.
我想知道是否有特定于 Ruby 语言的最佳实践或标准约定来编码这种占位符/异常。
i.e: something like:
即:类似于:
- UnsupportedOperationExceptionin Java
- NotImplementedExceptionin .Net Framework (C#)
- Java 中的UnsupportedOperationException
- .Net Framework (C#) 中的NotImplementedException
回答by pasha.zhukov
回答by knut
You may use the todonotes-gem
您可以使用todonotes-gem
There is a documentationwith some examples.
有一个包含一些示例的文档。
It doesn't implement an exception, but a logging mechanism and a possibility for temporary solutions.
它不实现异常,而是实现日志记录机制和临时解决方案的可能性。
回答by Chris
Looks like the original answer, which suggested raising NotImplementedError, was removed. I'll take a crack at it: write documentation.
看起来建议 raise 的原始答案NotImplementedError已被删除。我会尝试一下:写文档。
Don't add code that's just a placeholder. You wouldn't want folks coding against that API so don't even give them a chance (yourself included). Instead, document the road map you are currently planning on in the class and/or README. Then be open to it changing. Odds are by the time you get around to solving whatever problem is in the road map you'll have new thoughts on what is the appropriate solution. I think this is the right course of action in any language/framework, but I think Ruby in particular encourages us to not write code that you don't plan on having executed.
不要添加只是占位符的代码。您不希望人们针对该 API 进行编码,因此甚至不要给他们机会(包括您自己)。相反,在课程和/或自述文件中记录您当前计划的路线图。然后对它的变化持开放态度。很可能当您开始解决路线图中的任何问题时,您将对什么是适当的解决方案有新的想法。我认为这在任何语言/框架中都是正确的做法,但我认为 Ruby 特别鼓励我们不要编写您不打算执行的代码。
回答by sawa
Do not mention the unimplemented methods in the documents, or mention that they are not implemented yet. That is all.
不要在文档中提及未实现的方法,或者提及它们尚未实现。就这些。
回答by J?rg W Mittag
Ruby will raise a NoMethodErrorfor you anyway, when calling a non-existing method. That should be good enough for most cases.
NoMethodError无论如何,当调用一个不存在的方法时,Ruby 都会为你引发一个。对于大多数情况,这应该足够了。

