美文网首页
Ruby中的OpenStruct和Struct

Ruby中的OpenStruct和Struct

作者: EvanCui | 来源:发表于2017-08-17 16:53 被阅读0次

OpenStruct

如官方文档介绍, OpenStruct是一个类似Hash的数据结构,它允许我们使用对象自带的值定义任意的属性,
这是通过使用Ruby的元编程来定义类本身的方法来实现的.
说白了就是通过一个数据结构来模拟class,有些数据有自己的属性,但是使用class会比较复杂,使用hash即可满足,但是hash存取值又不太方便,这是OpenStruct就排上用场了。

require 'ostruct'
post = OpenStruct.new(title: "My first post", content: "My first post content")
# 三种读取方式返回相同的值
post.title    #=>  "My first post"
post[:title]  #=> "My first post"
post['title']  #=> "My first post"

OpenStruct类也包含很多类方法:
to_h可以方便的转化为hash, 如:
post.to_h #=> {:title=>"My first post", :content=>"My first post content"}
delete_field可以删除某个元素:

post.delete_field(:content)  #=>  {:title=>"My first post"}

虽然在Ruby中OpenStruct封装的很好用,但是有性能性能,具体参考:https://stackoverflow.com/questions/1177594/when-should-i-use-struct-vs-openstruct#answer-4459132

Struct

Struct本质上是一个简单的类,它允许你在不用定义class的情况下封装属性和accessor方法。

创建一个Struct:

Post = Struct.new(:title, :content)
post = Post.new('my title', 'my content')
# 三种读取方式返回相同的值:
post.title    #=>  "My first post"
post[:title]  #=> "My first post"
post['title']  #=> "My first post"

Struct的方法:

post.values  #=> ["my title", "my content"]
post.to_h  #=> {:title=>"my title", :content=>"my content"}

Struct比OpenStruct更优的地方是在Struct内部可以定义方法:

Post = Struct.new(:title, :content) do
   def display_title
      title.to_s + " created at today"
   end
end
Post.new("my title", "my content").display_title  #=> "my title created at today"

什么时候使用Struct?

Struct像是一个简单的封装了属性和方法的类.

那么Struct的适用场景是什么呢

使用Struct最大的好处是它可以比hash或者array更好的表明数据的意义.

比如,假设你有下面记录坐标数据的数组:

locations = [[40.748817, -73.985428], [40.702565, 73.992537]]

这个数组看起来并不能很清楚的展示其中的值是代表的坐标相关的数据。

现在我们使用Struct来定义这些值:

Location = Struct.new(:longitude, :latitude)
location = Location.new(40.748817, -73.985428)
location.longitude   #=> 40.748817

当要处理的是相关性比较强的数据时,你应该使用用Struct.比如上面的location的属性关联密切, 所以比较适合封装成一个对象.

使用Struct的类比Hash还有一个好处是Struct值require指定的属性,而Hash可以接受任何值.

比如,假设我们正在处理一些关于book的结构化的数据,如果我们使用Hash:

book = {}
book[:tile] = 'Jane Eyre'

这个Hash不管我们拼写的title是否正确,都会加到Hash中.

而使用Struct不会出现这个问题.

同样取坐标的例子:

Location = Struct.new(:longitude, :latitude)
location = Location.new(40.748817, -73.985428)
location.longitud   #=> undefined method `longitud' for struct

参考:http://culttt.com/2015/04/15/working-with-structs-in-ruby/

相关文章

  • Ruby中的OpenStruct和Struct

    OpenStruct 如官方文档介绍, OpenStruct是一个类似Hash的数据结构,它允许我们使用对象自带的...

  • Struct && OpenStruct

    1 Struct本质上是一个简单的类,它允许你在不用定义class的情况下封装属性和accessor方法。 Str...

  • Struct && OpenStruct

    一、需求 最近在做数据同步的应用,需要弄一个配置文件app.yml来进行应用的设置,例如: 读取出来很简单: 使用...

  • ruby:hash to Struct

    h={'k'=>'v'} s=Struct.new(*(h.keys.map {|x| x.downcase.to...

  • 数据库

    struct & class C++中的struct是对C中的struct的扩充。和class一样,struct有...

  • C++ 中的struct

    C++ 中也有struct, 而这个struct 和C中的struct不同,是功能扩展了的struct,当时的背景...

  • C中的struct和typedef struct

    typedef是类型定义,之所以定义结构体使用typedef struct 是为了方便使用这个结构体。如果不使用t...

  • C++面向对象-类

    类 C++中可以使用struct和class来定义一个类,在C++中,struct和class的区别是struct...

  • C++知识点(自用)

    关于struct和class c++中的struct可以有成员函数,能继承,能实现多态。。。那struct和cla...

  • Effective Ruby

    一、 让自己熟悉Ruby 1、理解 Ruby 中的 True 在 Ruby 中,除了 false 和 nil, 其...

网友评论

      本文标题:Ruby中的OpenStruct和Struct

      本文链接:https://www.haomeiwen.com/subject/axrnrxtx.html