1

在此之后,试图比较两个哈希数组:

db = [
{:foo => "bar", :stack => "overflow", :num => 0.5},
{:foo => "bar", :stack => "underlow", :num => 0.5},
{:foo => "bar", :stack => "overflow", :num => 0.1}
]

csv = [
{:foo => "bar", :stack => "overflow", :num => 0.5},
{:foo => "bar", :stack => "underlow", :num => 0.1},
]
 

我正在尝试使用 Ruby Set ( db_set = Set[db], csv_set = Set[csv]) 使用-( db_set - csv_set) 和&( db_set & csv_set) 比较两者,但这些似乎没有执行比较操作。

我误解了使用Set吗?我如何比较这两个哈希数组?

4

1 回答 1

4

这里不需要使用集合。似乎你会很好地使用数组运算符。

db = [
  {:foo => "bar", :stack => "overflow", :num => 0.5},
  {:foo => "bar", :stack => "underlow", :num => 0.5},
  {:foo => "bar", :stack => "overflow", :num => 0.1}
]

csv = [
  {:foo => "bar", :stack => "overflow", :num => 0.5},
  {:foo => "bar", :stack => "underlow", :num => 0.1},
]

db - csv # => [{:foo=>"bar", :stack=>"underlow", :num=>0.5}, {:foo=>"bar", :stack=>"overflow", :num=>0.1}]
db & csv # => [{:foo=>"bar", :stack=>"overflow", :num=>0.5}]
于 2012-08-03T13:05:05.917 回答