English 中文(简体)
返回掩体数据,作为废墟中的散射
原标题:returning struct data as a hash in ruby
  • 时间:2011-11-10 16:02:16
  •  标签:
  • ruby

Is there a valid reason for there not being a method to return the data of a standard ruby struct as a hash (member, value pairs)? Seeing that structs and hashes have very similar use cases, I m surprised that no such method exists. Or does it, and I m just too blind?

易于执行(而且现在我已经这样做),但标准校准缺乏这种功能,我认为我可能没有真正掌握废墟中的障碍概念。

最佳回答

Ruby <= 1.9.3 has OpenStruct#marshall_dump and Struct#each_pair:

Person = Struct.new(:name, :age)
person = Person.new("Jamie", 23)
person_hash = Hash[person.each_pair.to_a]
#=> {:age=>23, :name=>"Jamie"}

页: 1 开放标准编号:h:

Person = Struct.new(:name, :age)
person = Person.new("Jamie", 23)
person_hash = person.to_h
#=> {:age=>23, :name=>"Jamie"}
问题回答

我并不认为用于斜体和结构的情况非常相似。 A'h 允许你储存各种关键价值乳制品,如果你想要的话,适合储存数千个奶制品。 没有任何具体的关键保障。 有了“Struct”,你总是知道一套“钥匙”是什么,而且通常很小(不到20)。

可以用哈希语将一些信息与大量不同的物体联系起来。 用于规定一项职能的任择参数。 当你想要把一些明确的信息放在一个目标中时,可以使用这些指示。

我永远不想把脚石改成 has子,因此,我很想知道你为什么这样做。

EDIT 1: Did you know you can use this un-hash-like syntax with Structs?

P = Struct.new(:x,:y)
p = P.new(1,2)
p.x  # => x

http://www.ohchr.org。 也可以用来快速研究物体。 <代码>obj_hashed_by_namename>可比obj_arra.find{> >[>>>>. 姓名=更快。

我猜测我不知道你为什么要这样做,但:

s.members.inject({}) { |m, f| m[f] = s[f]; m }

或者,使用<代码>each_with_object:

s.members.each_with_object({}) { |m, h| h[m] = s[m] }

得到接受的回答对我不可行,而是使用以下方法:

require  ostruct 
require  date 

lid = OpenStruct.new(:status=>0,:rowversion=>0,:cre_dt=>DateTime.now.to_date,:cre_user=> 9999999 )
p Hash[lid.each_pair.to_a] #=> {}
p lid.marshal_dump #=>{:status=>0, :rowversion=>0, :cre_dt=>#<Date: 2014-03-03 ((2456720j,0s,0n),+0s,2299161j)>, :cre_user=>"9999999"}




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签