简体   繁体   English

Ruby哈希值数组到字符串

[英]Ruby array of hashes values to string

I have an array of hashes (#1) that looks like this: 我有一个哈希数组(#1),看起来像这样:

data = [{"username"=>"Luck", "mail"=>"root@localhost.net", "active"=>0}]

that I am trying to compare with following array of hashes (#2): 我正在尝试与以下哈希数组(#2)进行比较:

test = [{"username"=>"Luck", "mail"=>"root@localhost.net", "active"=>"0"}]

where #1 I obtained from database by mysql2 (what actually is in the database) and #2 from my cucumber scenario (what I minimally expect ot be there). 其中#1是我通过mysql2从数据库中获得的(实际上是数据库中的内容),而#2是我的黄瓜方案中的(我至少希望它在那里的)。

By definition #2 must be a subset of #1 so I follow with this code: 根据定义,#2必须是#1的子集,因此我遵循以下代码:

data = data.to_set
test = test.to_set
assert test.subset?(data)

The problem is in data array the value of active is NOT a string. 问题在于数据数组中active的值不是字符串。 In case of data it is Fixnum , and in case of test , it is String . 如果是data ,则为Fixnum ;如果是test ,则为String

I need a solution that will work even for more than one hash in the array. 我需要一种即使对于数组中的多个哈希也可以使用的解决方案。 (As the database can return more than one row of results) That is why I convert to sets and use subset? (因为数据库可以返回多个结果行)所以这就是为什么我转换为集合并使用subset?

From other questions I got: 从其他问题中我得到:

data.each do |obj|
  obj.map do |k, v|
    {k => v.to_s}
  end
end

However it does not work for me. 但是,它对我不起作用。 Any ideas? 有任何想法吗?

Assumptions you can make: 您可以做出的假设:

  • All the keys in data will always be Strings. data所有键将始终为字符串。
  • All the keys in test will always be Strings. test所有键将始终为字符串。 And always be the identical to data . 并始终与data相同。
  • All the values in test will always be Strings. test所有值将始终为字符串。

Don't ask me why it works, but I found A solution: 不要问我为什么它起作用,但是我找到了一个解决方案:

data.map! do |obj|
  obj.each do |k, v|
    obj[k] = "#{v}"
  end
end

I think it has something to do with what functions on arrays and hashes change the object itself and not create a changed copy of the object. 我认为这与数组和散列上的哪些功能会更改对象本身有关,而不是创建对象的已更改副本。

Here are a couple of approaches that should do it, assuming I understand the question correctly. 假设我正确理解了这个问题,可以采取以下两种方法。

#1: convert the hash values to strings #1:将哈希值转换为字符串

def stringify_hash_values(h)
  h.each_with_object({}) { |(k,v),h| h[k] = v.to_s }
end

def sorta_subset?(data,test)
  (test.map { |h| stringify_hash_values(data) } -
    data.map { |h| stringify_hash_values(data) }).empty?
end

data = [{"username"=>"Luck", "mail"=>"root@localhost.net", "active"=>0}]
test = [{"username"=>"Luck", "mail"=>"root@localhost.net", "active"=>"0"}]
sorta_subset?(data,test) #=> true

#2 see if keys are the same and values converted to strings are equal #2查看键是否相同以及转换为字符串的值是否相等

require 'set'

def hashes_sorta_equal?(h,g)
  hk = h.keys
  (hk.to_set == g.keys.to_set) &&
    (h.values_at(*hk).map(&:to_s) == g.values_at(*hk).map(&:to_s))
end

def sorta_subset?(data,test)
  test.all? { |h| data.any? { |g| hashes_sorta_equal?(g,h) } }
end

sorta_subset?(data,test) #=> true

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

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