English 中文(简体)
如何从档案中装载哈希人?
原标题:How to load a Hash from a File?

是否有一种可允许我以(Abbreviated => Abr)格式装上缩略词的鲁比拉工具? 然后,我需要从另一个档案中读到每个字。 如果该词与缩略语中的字数一致,我就需要将其改为缩略语。 我认为,我可以使用哈希语,但我不知道如何从档案中装载。

最佳回答

YAML是储存数据的一种非常普遍的形式,可在相片和节目制作之间转让。 ,JSON是另一个候补人员,在网站上非常常见。

我将阿盟用于像组合文件这样的东西,因为它很容易阅读和修改。 例如,这一鲁比结构:

irb(main):002:0> foo = {  a  => 1,  b  => [2, 3],  c  => {  d  => 4,  e  => 5 } }
{
    "a" => 1,
    "b" => [
        [0] 2,
        [1] 3
    ],
    "c" => {
        "d" => 4,
        "e" => 5
    }
}

类似情况,如果转化成阿盟:

irb(main):003:0> puts foo.to_yaml
--- 
a: 1
b: 
- 2
- 3
c: 
  d: 4
  e: 5

你们可以把这一点保存到档案中:

File.open( foo.yaml ,  w ) { |fo| fo.puts foo.to_yaml }

改为:

bar = YAML.load_file( foo.yaml )
{
    "a" => 1,
    "b" => [
        [0] 2,
        [1] 3
    ],
    "c" => {
        "d" => 4,
        "e" => 5
    }
}

同样,利用JSON:

puts foo.to_json
{"a":1,"b":[2,3],"c":{"d":4,"e":5}}

你们可以把JSON拯救到与你一样的档案中,然后把它重新装上,然后把它退回到暗中。 它是不同的星号:

irb(main):011:0> File.open( foo.json ,  w ) { |fo| fo.puts foo.to_json }
nil
irb(main):012:0> bar = JSON.parse(File.read( foo.json ))
{
    "a" => 1,
    "b" => [
        [0] 2,
        [1] 3
    ],
    "c" => {
        "d" => 4,
        "e" => 5
    }
}

无论使用哪一种语言,都能够读懂阿马尼马语,或者可以读到这些文档并使用数据,或者读写。

如果你有一台散射机,便很容易取而代之,因为Ruby String s gsub理解斜体。 http://www.ruby-doc.org/core-1.9.3/String.html#method-i-gsub”rel=“noreferer” The docs:

If the second argument is a Hash, and the matched text is one of its keys, the corresponding value is the replacement string.

这意味着你可以做这样的事情:

foo =  Jackdaws love my giant sphinx of quartz 
bar = {  quartz  =>  rosy quartz ,  sphinx  =>  panda  }
foo.gsub(Regexp.union(bar.keys), bar)
"Jackdaws love my giant panda of rosy quartz"

您确实必须注意替代物的二字越境碰撞,但照样的<><>>旗帜可有所帮助。 使这一工作成为读者的工作。

问题回答

Marshal.load/code>和Marshal.dump

另外,你还可以研究将散草编成日志的序号,然后通过阿盟阅读。

I found this http://www.rubydoc.info/gems/bosh-director/1.1868.0/Bosh%2FDirector%2FConfig.load_hash after looking up how to load hashes myself.

我的例子是在铁路运输中与MongoDB打交道。

因此,我确定了一种初步方法,使Im的收集工作得以进行。

def initialize @collection = self.class.collection end

页: 1 让我们把人作为榜样。

装载方法如下:

def load_collection(file_path)
  hash_values = self.class.load_hash(file_path)
  @collection.insert_many(hash)
end

在具有约束力的内部。 ry

[1] pry(#<People>)> file_path
=> "people_results.json"
[2] pry(#<People>)> hash
=> [{"number"=>0, "first_name"=>"SHAUN", "last_name"=>"JOHNSON", "gender"=>"M", "group"=>"15 to 19", "secs"=>1464}, {"number"=>1, "first_name"=>"TUAN", "last_name"=>"JOHNSON", "gender"=>"M", "group"=>"masters", "secs"=>3994}, {"number"=>2, "first_name"=>"EARLINE", "last_name"=>"BROWN", "gender"=>"F", "group"=>"masters", "secs"=>1415}...
[3] pry(#<People>)> self
=> #<People:0x007fc80301b5f8 @coll=#<Mongo::Collection:0x70248510355560 namespace=test.people>>
[4] pry(#<People>)> self.class
=> People

因此,在这种情况下,<代码>载荷_hash 这种方法算出一个参数,在这种情况下,在一份json案卷中通过,其中有一些数据。 然后,我把这些数据列入我的收集。





相关问题
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 ...

热门标签