Tag: 哈希

是否保证Ruby哈希文字的顺序?

Ruby,自v1.9起,在循环哈希时支持确定性顺序; 首先添加的条目将首先返回。 这是否适用于文字,即{ a: 1, b: 2 }总是在b之前产生? 我做了一个Ruby 2.1(MRI)的快速实验,它实际上是一致的,但是语言在多大程度上保证了所有Ruby实现的工作?

ruby:如何有效地迭代哈希中的元素

我有一个非常大的哈希,我想迭代它。 Hash.each似乎太慢了。 有没有有效的方法来做到这一点? 如何将此哈希转换为数组? 在每个循环中,我正在做非常简单的字符串: name_hash.each {|name, str| record += name.to_s + “\|” + str +”\n” } 并且哈希使用人名作为键,一些相关内容作为值: name_hash = {:”jose garcia” => “ca:tw#2@1,2@:th#1@3@;ar:tw#1@4@:fi#1@5@;ny:tw#1@6@;”}

如何在ruby中按位置获取哈希值?

我希望像数组一样按位置获取哈希值。 示例: h = Hash[“a” => 100, “b” => 200] 在这个数组中,当我们调用h [0]时,它返回给定数组中的第一个元素。 哈希可能有同样的事情吗? 如果是,那怎么样? 先谢谢Prasad。

如何获取其值等于ruby中给定参数的哈希的键?

所以我试图从rubeque http://www.rubeque.com/problems/related-keys-of-hash/解决这个问题。 Basicaly我只需要得到哈希的键,其值等于给定的参数。我想知道你们是否可以给我一些提示? 解决这个问题,非常感谢你 这就是我到目前为止所拥有的 class Hash def keys_of(*args) key = Array.new args.each { |x| key << x} key.each { |x,y| x if y == key} end end assert_equal [:a], {a: 1, b: 2, c: 3}.keys_of(1) assert_equal [:a, :d], {a: 1, b: 2, c: 3, d: 1}.keys_of(1) assert_equal [:a, :b, :d], {a: 1, b: 2, […]

从ruby中的哈希数组打印表到文件

我想把以下数组作为表输出到file.txt。 arr=[{:food=>””, :calories=>”Calories”, :carbs=>”Carbs”, :fat=>”Fat”, :protein=>”Protein”, :cholest=>”Sodium”}, {:food=>”New York Bagel Co – Wholemeal Bagels*, 90 g bagel”, :calories=>”223″, :fat=>”2″, :protein=>”9″, :cholest=>”360″}, {:food=>”Nescafe – Decaf Coffee With Semi Skimmed Milk, 1 mug”, :calories=>”12″, :fat=>”0″, :protein=>”1″, :cholest=>”0″}, {:food=>””, :calories=>”235″, :fat=>”2″, :protein=>”10″, :cholest=>”360″}] 我想要的结果是: Food Calories Fat Protein Cholest New York Bagel Co – Wholemeal Bagels*, 90 g […]

基于键合并散列数组中的散列值

我有一系列与此类似的哈希: [ {“student”: “a”,”scores”: [{“subject”: “math”,”quantity”: 10},{“subject”: “english”, “quantity”: 5}]}, {“student”: “b”, “scores”: [{“subject”: “math”,”quantity”: 1 }, {“subject”: “english”,”quantity”: 2 } ]}, {“student”: “a”, “scores”: [ { “subject”: “math”, “quantity”: 2},{“subject”: “science”, “quantity”: 5 } ] } ] 有没有更简单的方法来获得类似于此的输出,除了循环数组并找到重复然后组合它们? [ {“student”: “a”,”scores”: [{“subject”: “math”,”quantity”: 12},{“subject”: “english”, “quantity”: 5},{“subject”: “science”, “quantity”: 5 } ]}, {“student”: “b”, […]

哈希的compare_by_identity如何运作?

我首先运行下面的代码第一部分并获得所需的输出,现在只是为了解决它并做一些研究我做了第二部分。 第一部分 irb(main):001:0> h1 = { “a” => 100, “b” => 200, :c => “c” } => {“a”=>100, “b”=>200, :c=>”c”} irb(main):002:0> h1[“a”] => 100 irb(main):002:0> h1[:c] => “c” 第二部分 irb(main):003:0> h1.compare_by_identity => {“a”=>100, “b”=>200, :c=>”c”} irb(main):004:0> h1.compare_by_identity? => true irb(main):005:0> h1[“a”] => nil irb(main):006:0> h1[:c] => “c” irb(main):007:0> h1[“a”]如何在第一部分和第二部分中给出不同的值,但在h1[:c]却不一样? 我正在使用Ruby 1.9.3。

在Ruby中比较数组的最有效方法

下面的代码应该找到arr_1中缺少的arr_2中的arr_2 。 def compare_1 (arr_1, arr_2) output = [] temp = arr_2.each_with_object(Hash.new(0)) { |val, hsh| hsh[val] = 0 } arr_1.each do |element| if !temp.has_key? (element) output << element end end puts output end def compare_2 (arr_1, arr_2) out = [] arr_1.each do |num| if (!arr_2.include?(num)) out << num end end puts out end 根据’基准’,第一种方法更快,大概是通过使用哈希。 有没有更简洁的方法来写这些或实现这一点? […]

Ruby Hash:为不存在的元素创建默认值

我在这里从这个答案中了解到这是可能的: h = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) } h[‘bar’] # => {} h[‘tar’][‘star’][‘par’] # => {} 有人能解释它是如何工作的吗?

散列成分组数组

我不是很有经验的ruby,所以我很难格式化一块数据。 我有这个哈希,它包含一些具有相同值的键,例如: {“key” => “value1”, “key2” => “value2”, “key3” => “value3”, “key4” => “value1”, “key5” => “value2” ..} 我正在尝试将其转换为包含按值分组的键的数组 [[“key”,”key4″],[“key2″,”key5”],[“key3”]] 任何帮助,将不胜感激。