Tag: 哈希

通过键数组设置ruby哈希元素值

这是我得到的: hash = {:a => {:b => [{:c => old_val}]}} keys = [:a, :b, 0, :c] new_val = 10 哈希结构和密钥集可以变化。 我需要得到 hash[:a][:b][0][:c] == new_val 谢谢!

为什么我的代码在使用哈希符号而不是哈希字符串时会中断?

我有一个场景,当我尝试使用符号访问哈希键时,它不起作用,但当我用字符串访问它时它工作正常。 我的理解是建议使用符号而不是字符串,所以我试图清理我的脚本。 我在我的脚本中的其他地方使用哈希符号,只是这个特定的场景不起作用。 这是片段: account_params ={} File.open(‘file’, ‘r’) do |f| f.each_line do |line| hkey, hvalue = line.chomp.split(“=”) account_params[hkey] = hvalue end end account_scope = account_params[“scope”] 这是有效的,但如果我使用它不符号,如下所示: account_scope = account_params[:scope] 当我使用符号时,我得到: can’t convert nil into String (TypeError) 我不确定它是否重要,但这个特定哈希值的内容看起来像这样: 12345/ABCD123/12345

为什么我不能从哈希中读取值?

考虑以下代码: hash = {“a”=>[“B”, “C”], “b”=>[“C”], “c”=>[“D”, “E”], “d”=>[“F”]} puts hash[“a”] 这只是打印什么。 puts hash[“a”].class 这打印出NilClass 以下ruby版本中是否存在某种已知错误? ruby 2.0.0p247(2013-06-27修订版41674)[universal.x86_64-darwin13] 我希望有人可以帮助我,这让我发疯。 我的IDE是JetBrains的RubyMine。 我也尝试通过IRB直接运行它。 谢谢 PS。 操作系统是OSX

Ruby:反转哈希以保留非唯一值

我有一个看起来像这样的哈希: {“a” => [1, 2, 3], “b” => [4, 5, 6], “c” => [3, 4, 5], “d” => [7, 2, 3]} 我想要做的是使用包含它的所有键的数组来创建所有现有值的哈希值,例如将上面的内容转换为: {1 => [“a”], 2 => [“a”, “d”], 3 => [“a”, “c”, “d”], 4 => [“b”, “c”]}

Ruby合并哈希将密钥放入csv字符串

有没有一种聪明的方法可以在Ruby中实现以下function? hash1 = { “a” => 1, “b” => 2, “d” => 3} hash2 = { “a” => 4, “b” => 5, “c” => 7} hash3 = { “a” => 4, “d” => 7, “e” => 9} puts hash1.csvMerge(hash2).csvMerge(hash3) 输出为: { “a” => “1,4,4”, “b” => “2,5,0”, “c” => “0,7,0”, “d” => “3,0,7”, “e” => […]

在ruby中使用散列排序的正确方法

我是ruby的新手,我正在尝试编写一个dijkstra函数,但我的哈希类似乎根本不起作用 def distance(start_code, end_code, map) #initialize hash for distance #distance are initialized to -1 dist_hash=Hash.new() start_hash=Hash.new() parent_hash=Hash.new() close_list=Array.new() find=-1 map.citylist.each do |e| dist_hash[e]=[+1.0/0.0] end start_hash[start_code]=0 parent_hash[start_code]=start_code while (start_hash.empty?)==false #sort the hash start_hash.sort_by {|k,v| v} puts ‘value’ puts start_hash.values() #pop the first item in the hash h=start_hash.shift() curr_key=h[0] curr_val=h[1] curr_city=map.findcity(curr_key) close_list<<curr_city.code #for every one in adjacent […]

在子类化Ruby哈希时如何覆盖 =方法?

我有一个扩展Hash的类,我想跟踪修改哈希键的时间。 什么是覆盖[key]=语法方法来实现此目的的正确语法? 我想插入我的代码,然后调用父方法。 这可能与C方法有关吗? 我从文档中看到底层方法是 rb_hash_aset(VALUE hash, VALUE key, VALUE val) 如何将其分配给括号语法?

我的哈希堆叠无序。什么是循环,可以通过他们的哈希ID选择它们?

我的哈希看起来像这样: {“6″=>{“:amount_paid”=>”100.00”, “:date_paid”=>”4/22/2009”}, “0”=>{“:amount_paid”=>”100.00”, “:date_paid”=>”2/27/2008”}, “1”=>{“:amount_paid”=>”80.00”, “:date_paid”=>”3/27/2008”}, “2”=>{“:amount_paid”=>”100.00”, “:date_paid”=>”5/8/2008”}, “3”=>{“:amount_paid”=>”100.00”, “:date_paid”=>”6/20/2008”}, “4”=>{“:amount_paid”=>”100.00”, “:date_paid”=>”9/22/2008”}, “5”=>{“:amount_paid”=>”100.00”, “:date_paid”=>”2/20/2009″}} 当我循环使用时,顺序对我很重要: params[:payments].each_with_index do |item, idx| 通过这种方式,我可以添加日期之前的日期。 是否有一个循环可以找到序列”0”..”6″并保持接近相同的语法? 我能想到的唯一另一种选择是确保这些参数按顺序堆叠。 它们来自这样的forms: = text_field_tag “payments[0][:date_paid]” = text_field_tag “payments[0][:amount_paid]” = text_field_tag “payments[1][:date_paid]” = text_field_tag “payments[1][:amount_paid]” = submit_tag ‘punch it chewy!’

如何在不使用eval的情况下将字符串转换为ruby / rails中的哈希?

这是需要转换为hash的string 。 “{:status => {:label => ‘Status’, :collection => return_misc_definitions(‘project_status’) } }” 我们不能使用eval因为eval将在字符串中执行return_misc_definitions(‘project_status’)方法。 是否有纯字符串操作来完成ruby / rails中的这种转换? 感谢帮助。

从Jekyll插件返回目录中的文件列表?

我无法弄清楚如何在jekyll插件中创建filter或标记,以便我可以返回一个目录并循环其内容。 我找到了这些: http://pastebin.com/LRfMVN5Y http://snippets.dzone.com/posts/show/302 到目前为止我有: module Jekyll class FilesTag < Liquid::Tag def initialize(tag_name, text, tokens) super @text = text end def render(context) #"#{@text} #{Time.now}" Dir.glob("images/*").each { |i| "#{i}" } #Dir.glob("images/*") #Hash[*Dir.glob("images/*").collect { |v| [v, v*2] }.flatten] end end end Liquid::Template.register_tag('files', Jekyll::FilesTag) 我可以成功地将图像列表作为字符串返回并打印出来: {% files test_string %} 但是对于我的生活,无论我如何从Dir.glob返回数组/哈希,我都无法遍历数组。 我只是希望能够做到: {% for image in files %} image […]