简体   繁体   English

如何使用to_s将哈希值作为字符串返回

[英]How to return hash values as a string using to_s

This is my code: 这是我的代码:

def return_rider_values(pol_option, pro_endorsement, prop_coverage, *par)

  rider_values
  par.each do |p|
    rider_values << RiderValue.find_all_by_rider_id(p)
  end

  rider_hash = { }
  rider_values.each do |rv|
    if rv.attributes["name"].downcase == "yes"
      rider_hash.merge!({par[0].to_s => rv.attributes['id'].to_s})
    elsif rv.attributes["position"] == pol_option.to_i && rv.attributes["rider_id"] == par[1]
      rider_hash.merge!({par[1].to_s => rv.attributes["id"].to_s})
    elsif rv.attributes["position"] == prop_coverage.to_i && rv.attributes["rider_id"] == par[2]
      rider_hash.merge!({par[2].to_s => rv.attributes["id"].to_s})
    elsif rv.attributes["position"] == pro_endorsement.to_i && rv.attributes["rider_id"] == par[3]
      rider_hash.merge!({par[3].to_s => rv.attributes["id"].to_s})
    end
  end
  rider_hash
end

The output looks like this: 输出看起来像这样:

rider_hash  #=> '22' -> 58
                '23' -> 61
                '25' -> 66
                '26' -> 68

I was expecting, and need apparently since it's not working later down the line: 我在期待,并且显然很需要,因为稍后无法正常工作:

rider_hash  #=> '22' -> '58'
                '23' -> '61'
                '25' -> '66'
                '26' -> '68'

I don't know why the lookup function later in the program wants the ids to be strings instead of ints. 我不知道为什么程序后面的查找功能希望id是字符串而不是ints。 I just know that it does, and I can't change it since lots of other methods use it. 我只知道它可以,而且我不能更改它,因为许多其他方法都在使用它。

I have to_s on both the hash key and value. 我在哈希键和值上都有to_s I realize that in Ruby 1.9 to_s is an alias for inspect but even in the Hash documentation it says that, inspect or to_s is supposed to "Return the contents of this hash as a string." 我意识到,在Ruby 1.9中, to_sinspect的别名,但是即使在Hash文档中,它也指出, inspect or to_s应该“将此哈希的内容作为字符串返回”。

So why is only the key being returned as a string? 那么为什么只将键作为字符串返回呢?

You have an array of hashes so try this: 您有一系列哈希,请尝试以下操作:

def return_rider_values
    par = [1,2,3,6]
    rider_hash = {}
    rider_values = [element1: {:attributes => {:id => 1, :name => 'yes', :position => 1, :rider_id => 1}}, 
                    element2: {:attributes => {:id => 2, :name => 'no', :position => 2, :rider_id => 2}},
                    element3: {:attributes => {:id => 3, :name => 'something', :position => 1, :rider_id => 3}}, 
                    element4: {:attributes => {:id => 4, :name => 'something_else', :position => 2,   :rider_id => 6}}]

    rider_values.each_with_index do |hash, idx|
        rider_values[idx].each_pair do |k, v|
            if v[:attributes][:name].downcase == "yes"
                rider_hash.merge!({par[0].to_s => v[:attributes][:id].to_s})

            elsif v[:attributes][:position] == 2 && v[:attributes][:rider_id] == par[1]
                rider_hash.merge!({par[1].to_s => v[:attributes][:id].to_s})

            elsif v[:attributes][:position] == 3 && v[:attributes][:rider_id] == par[2]
                rider_hash.merge!({par[2].to_s => v[:attributes][:id].to_s})

            elsif v[:attributes][:position] == 4 && v[:attributes][:rider_id] == par[3]
                rider_hash.merge!({par[3].to_s => v[:attributes][:id].to_s})

            end
        end
    end
rider_hash
end

test = return_rider_values

puts test

output: #=> {"1"=>"1", "2"=>"2"} 输出: #=> {"1"=>"1", "2"=>"2"}

I ended up getting what I wanted by adding this: 我最终通过添加以下内容获得了想要的东西:

rider_hash.each{ |_,v| v.replace "'#{v}'"}

But this seems like a dirty solution somehow. 但这似乎是一个肮脏的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM