Ruby 中的普通旧对象?

发布于 2024-08-20 20:35:01 字数 140 浏览 3 评论 0原文

我注意到在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我做我的改变 2024-08-27 20:35:01

我认为数组与对象是错误的二分法。

这是完全合理的,当 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.

葮薆情 2024-08-27 20:35:01

任何时候都是物体。关键是返回的对象是否具有与其关联的行为。这样做很好:

  def read_first_and_last_name(data_source)
    [data_source.read_string, data_source.read_string]
  end

但是当您发现存在与这些数据项相关的行为时......

  def print_name(first_name, last_name)
    puts "#{first_name} #{last_name}"
  end

  def read_and_print_name
    first_name, last_name = read_first_and_last_name(data_source)
    print_name(first_name, last_name)
  end

那么它们应该是一个类:

  class FullName

    def FullName.read(data_source)
      FullName.new(data_source.read_string, data_source.read_strng)
    end

    def initialize(first_name, last_name)
      @first_name = first_name
      @last_name = last_name
    end

    def print
      puts "#{@first_name} #{@last_name}"
    end

  end

通过很好地封装名称的行为,用法变得简单如下:

  def read_and_print_name
    FullName.read(data_source).print
  end

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:

  def read_first_and_last_name(data_source)
    [data_source.read_string, data_source.read_string]
  end

But the moment you find there is behavior associated with those data items...

  def print_name(first_name, last_name)
    puts "#{first_name} #{last_name}"
  end

  def read_and_print_name
    first_name, last_name = read_first_and_last_name(data_source)
    print_name(first_name, last_name)
  end

...then they should be a class:

  class FullName

    def FullName.read(data_source)
      FullName.new(data_source.read_string, data_source.read_strng)
    end

    def initialize(first_name, last_name)
      @first_name = first_name
      @last_name = last_name
    end

    def print
      puts "#{@first_name} #{@last_name}"
    end

  end

With a name's behavior nicely encapsulated, usage becomes as simple as:

  def read_and_print_name
    FullName.read(data_source).print
  end
╭ゆ眷念 2024-08-27 20:35:01

这些结果数组包含什么?答案是,在 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:

"We wondered why people were so
against using regular objects in their
systems and concluded that it was
because simple objects lacked a fancy
name. So we gave them one, and it's
caught on very nicely."

Fortunately in Ruby it has always been natural for people to just practise object-oriented programming without the need to invent terminologies around it.

昔梦 2024-08-27 20:35:01

我个人在几乎所有我写的不是完整的一次性脚本的内容中都使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文