我应该何时使用OpenStruct而不是Hash?

时间:2021-06-10 16:06:53

I like the "definition of arbitrary attributes" and I think the OpenStruct in ruby sometimes feels cleaner than using a hash, but I'm curious as to whether there are other specific advantages or use cases that make an OpenStruct a better choice than simply using a Hash.

我喜欢“任意属性的定义”,我认为ruby中的OpenStruct有时比使用散列更清晰,但我很好奇是否有其他特定优势或用例使OpenStruct成为比单纯使用更好的选择哈希。

2 个解决方案

#1


21  

I think this mostly comes down to a performance decision. From the Ruby Documentation:

我认为这主要取决于绩效决策。来自Ruby文档:

An OpenStruct utilizes Ruby’s method lookup structure to and find and define the necessary methods for properties. This is accomplished through the method method_missing and define_method.

OpenStruct利用Ruby的方法查找结构来查找和定义属性的必要方法。这是通过method_missing和define_method方法完成的。

This should be a consideration if there is a concern about the performance of the objects that are created, as there is much more overhead in the setting of these properties compared to using a Hash or a Struct.

如果对创建的对象的性能存在顾虑,则应该考虑这一点,因为与使用Hash或Struct相比,设置这些属性的开销要大得多。

Additionally, something like a Hash has additional functionality with all of the methods it provides (has_key?, include?, etc.). The OpenStruct is a very simple object from that standpoint, but if you don't have any concerns from a performance standpoint and just want an easy object to work with, OpenStruct is a good choice.

另外,像Hash这样的东西还有其提供的所有方法的附加功能(has_key?,include?等)。从这个角度来看,OpenStruct是一个非常简单的对象,但是如果从性能的角度来看没有任何问题并且只想要一个易于使用的对象,那么OpenStruct是一个不错的选择。

#2


28  

OpenStruct objects are useful when you need something to fit a certain method call interface (i.e. send in a duck-typed object responding to #name and #value), or when you want to encapsulate the implementation details, but also want to avoid over-engineering the solution. They also make an awesome stub object, and I often use them in place of framework stubs when I don't need the overhead of a stub/mock.

当您需要适合某个方法调用接口(即发送响应#name和#value的duck-typed对象)时,或者当您想要封装实现细节时,OpenStruct对象非常有用,但也希望避免过度使用设计解决方案。它们也构成了一个非常棒的存根对象,当我不需要存根/模拟的开销时,我经常使用它们来代替框架存根。

#1


21  

I think this mostly comes down to a performance decision. From the Ruby Documentation:

我认为这主要取决于绩效决策。来自Ruby文档:

An OpenStruct utilizes Ruby’s method lookup structure to and find and define the necessary methods for properties. This is accomplished through the method method_missing and define_method.

OpenStruct利用Ruby的方法查找结构来查找和定义属性的必要方法。这是通过method_missing和define_method方法完成的。

This should be a consideration if there is a concern about the performance of the objects that are created, as there is much more overhead in the setting of these properties compared to using a Hash or a Struct.

如果对创建的对象的性能存在顾虑,则应该考虑这一点,因为与使用Hash或Struct相比,设置这些属性的开销要大得多。

Additionally, something like a Hash has additional functionality with all of the methods it provides (has_key?, include?, etc.). The OpenStruct is a very simple object from that standpoint, but if you don't have any concerns from a performance standpoint and just want an easy object to work with, OpenStruct is a good choice.

另外,像Hash这样的东西还有其提供的所有方法的附加功能(has_key?,include?等)。从这个角度来看,OpenStruct是一个非常简单的对象,但是如果从性能的角度来看没有任何问题并且只想要一个易于使用的对象,那么OpenStruct是一个不错的选择。

#2


28  

OpenStruct objects are useful when you need something to fit a certain method call interface (i.e. send in a duck-typed object responding to #name and #value), or when you want to encapsulate the implementation details, but also want to avoid over-engineering the solution. They also make an awesome stub object, and I often use them in place of framework stubs when I don't need the overhead of a stub/mock.

当您需要适合某个方法调用接口(即发送响应#name和#value的duck-typed对象)时,或者当您想要封装实现细节时,OpenStruct对象非常有用,但也希望避免过度使用设计解决方案。它们也构成了一个非常棒的存根对象,当我不需要存根/模拟的开销时,我经常使用它们来代替框架存根。