2012-06-12 198 views
1

部分散列給定一個散列搜索哈希數組在紅寶石

z = [{'a' => 1, 'b' => 2}, {'a' => 3, 'b' => 4}, {'a' => 1, 'b' => 4}] 

如何搜索,如果搜索參數本身是一個哈希例如

{'a' => 3} 

,這樣我可以這樣做z.find_by_hash({ 'A'=> 3})它返回

{'a' => 3, 'b' => 4} 

,也讓喜歡z.find_by_hash陣列的集合({ '一個'=> 1})它返回

[{'a' => 1, 'b' => 2}, {'a' => 1, 'b => 4}] 

由於

回答

1

您可以這樣做:

class Array 
    def find_by_hash(hash) 
    self.select { |h| h.includes_hash?(hash) } 
    end 
end 

class Hash 
    def includes_hash?(other) 
    included = true 

    other.each do |key, value| 
     included &= self[key] == other[key] 
    end 

    included 
    end 
end 

這通過一種方法延伸Hash,以找出是否一個Hash包括另一個(具有多個鍵和值)。 Array擴展與你想要的方式,但它是一個更通用的方法,因爲你可以這樣做:

ary = [ {:a => 1, :b => 3, :c => 5}, {:a => 5, :b => 2, :c => 8} ] 
ary.find_by_hash({ :a => 1, :c => 5 }) 

注意:您也應該考慮使用Symbol S表示Hash鍵,因爲它是在Ruby中的普遍做法,但我的方法也適用於你的鑰匙。

+0

謝謝。這是正確的,因爲它解決了在散列內搜索散列的根本問題 – globetrotter

0
z = [{'a' => 1, 'b' => 2}, {'a' => 3, 'b' => 4}, {'a' => 1, 'b' => 4}] 

class Array 
    def search_hash(hash) 
    key = hash.keys.first 
    value = hash.values.first 
    select { |h| h[key] == value } 
    end 
end 

z.search_hash({'a' => 3}) #=> [{"a"=>3, "b"=>4}] 

,或者你可以不花括號鍵入

z.search_hash('a' => 3) 
+0

謝謝我這樣實現它,但我想知道是否有更通用的解決方案 – globetrotter

0

基本上你需要的是這樣的:

class Array 
    def find_by_hash(h) 
    h.collect_concat do |key, value| 
     self.select{|h| h[key] == value} 
    end 
    end 
end 
+0

我沒有在[Ruby 1.8.7](http://ruby-doc.org/core)中看到這個collect_concat方法-1.8.7/Hash.html)或[Ruby 1.9.3](http://www.ruby-doc.org/core-1.9.3/Hash.html) – globetrotter

+0

查看Enumerable mixin的文檔 http://www.ruby-doc.org/core-1.9.3/Enumerable.html#method-i-collect_concat – Hugo

0

我沒有找到在API的方法,所以我認爲我們必須實現它是我們自己的。
(順便說一句,我認爲@megas'的做法是更好,更可讀的)通過TDD

代碼:

class SearchHashTest < Test::Unit::TestCase 
    def setup 
    @array_with_hash_elements = ArrayWithHashElements.new [{'a' => 1, 'b' => 2}, {'a' => 3, 'b' => 4}, {'a' => 1, 'b' => 4}] 
    end 
    def test_search_an_array_by_hash_parameter_and_return_single_hash 
    assert_equal({'a' => 3, 'b' => 4}, @array_with_hash_elements.search({'a'=>3})) 
    end 
    def test_search_an_array_by_hash_parameter_and_return_an_array 
    assert_equal([{'a' => 1, 'b' => 2}, {'a'=> 1, 'b' => 4}], @array_with_hash_elements.search({'a'=>1})) 
    end 
end 

執行代碼(僅用於演示,不生產)

class ArrayWithHashElements 
    def initialize some_array 
    @elements = some_array 
    end 
    def search(query_hash) 
    puts "search: #{query_hash.inspect}" 
    result = [] 
    @elements.each do | array_element_in_hash_form| 
     query_hash.each_pair do | key, value | 
     if array_element_in_hash_form.has_key?(key) && array_element_in_hash_form[key] == value 
      puts "adding : #{array_element_in_hash_form.inspect} to result" 
      result << array_element_in_hash_form 
     end 
     end 
     result 
    end 
    return result.size != 1 ? result : result[0] 
    end 
end 

結果:

[email protected]:~/workspace/test$ ruby search_hash_test.rb 
Loaded suite search_hash_test 
Started 
search: {"a"=>1} 
adding : {"b"=>2, "a"=>1} to result 
adding : {"b"=>4, "a"=>1} to result 
.search: {"a"=>3} 
adding : {"b"=>4, "a"=>3} to result 
. 
Finished in 0.000513 seconds. 

2 tests, 2 assertions, 0 failures, 0 errors