Ruby 中的普通旧对象?
我注意到在 Ruby 中,供应商 API 将结果作为数组传回是很常见的?难道普通旧对象(如 Java 中的 POJO)不应该更像是一个标准吗? 如果我编写自己的库,我不应该使用POJO PORO吗?
I notice in Ruby it is very common to for vendor APIs to pass back results as arrays? Shouldn't Plain Old Objects (Like POJOs in Java) be more of a standard? If I write my own library shouldn't I use POJOs POROs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为数组与对象是错误的二分法。
这是完全合理的,当 API 调用返回多个事物时,它是数组的形式(数组是一个相当简单的对象,因此在 Ruby 中可以说是一个“PORO”)
编辑:回应您的评论:
您引用的示例( http://github.com/cjheath/geoip ) 返回不同项目的数组。我同意这不一定是返回数据的最佳格式。在这种情况下,我会认为具有合理命名的键的散列将是更好的结构。
正如 John Topley 所说,Ruby 的 OO 本质意味着人们不必发明“PORO”之类的术语,因为哈希值非常简单。
I think array vs object is a false dichotomy.
It is perfectly reasonable, where an API call is returning more than one of a thing, that it is in the form of an array (and an array is a fairly simple object, and therefore arguably a 'PORO', in Ruby anyway)
Edit: in response to your comments:
The example you cite ( http://github.com/cjheath/geoip ) returns an array of differing items. I agree this is not necessarily the best format to return the data in. In that case I would have thought a hash with sensibly named keys would be a better structure.
As John Topley says, the OO nature of Ruby means people don't have to invent such terminology as 'PORO', as a hash is pretty much as simple as you can get.
任何时候都是物体。关键是返回的对象是否具有与其关联的行为。这样做很好:
但是当您发现存在与这些数据项相关的行为时......
那么它们应该是一个类:
通过很好地封装名称的行为,用法变得简单如下:
It's all objects, all the time. The key is whether the objects being returned have behavior associated with them. It's fine to do this:
But the moment you find there is behavior associated with those data items...
...then they should be a class:
With a name's behavior nicely encapsulated, usage becomes as simple as:
这些结果数组包含什么?答案是,在 Ruby 中它们包含对象,因为 Ruby 中的一切都是对象。
Java 世界中的 POJO 是针对企业 Java(例如 EJB)给世界带来的一些复杂性的反应。引用创造这个词的马丁·福勒的话:
幸运的是,在 Ruby 中,人们总是很自然地练习面向对象编程,而不需要围绕它发明术语。
What do those arrays of results contain? The answer is that in Ruby they contain objects, because everything in Ruby is an object.
POJOs in the Java world were a reaction against some of the complexities inflicted upon the world by enterprise Java e.g. EJBs. To quote Martin Fowler who coined the term:
Fortunately in Ruby it has always been natural for people to just practise object-oriented programming without the need to invent terminologies around it.
我个人在几乎所有我写的不是完整的一次性脚本的内容中都使用 PORO。
我发现自己经常创建一个数据持有者类型的类,该类将管理和保存我的特定类型的多个对象,并包含一些辅助方法。我发现当其他人也必须使用我的代码时这很方便。
我认为这个问题非常主观,因为没有一个答案总是正确的。有时只传回一个数组就可以了,不需要创建额外的类。有时,额外的抽象级别会让用户更清楚地了解某些内容。
I personally use POROs in almost anything I write that isn't a complete throwaway script.
I find myself often creating a data holder type of class that would manage and hold multiple objects of my specific type and include some helper methods. I find this convenient for when someone else has to work with my code as well.
I think this question is very subjective in the sense that there isn't an answer that is always right. Sometimes just passing back an array is fine and there is no need to create an extra class. Sometimes the extra level of abstraction makes something a lot more clear to the user.