Ruby 使用Hashie Gem搜索具有组的嵌套哈希

Ruby 使用Hashie Gem搜索具有组的嵌套哈希,ruby,hash,Ruby,Hash,我有一个PDF格式的嵌套哈希: [ { :page => 1, :lines => [ { :y => 774.0, :text_groups => [ { :x => 18.0, :width => 421.59599999999995, :text => "XXXX" } ] }, # ... ] }, { :page => 2, :lines =>

我有一个PDF格式的嵌套哈希:

[ { :page => 1, 
    :lines => [
      { :y => 774.0,
        :text_groups => [ { :x => 18.0, :width => 421.59599999999995, :text => "XXXX" } ]
      },
      # ...
    ]
  },
  { :page => 2, 
    :lines => [
      { :y => 774.0,
        :text_groups => [ { :x => 18.0, :width => 421.59599999999995, :text => "XXXX" } ]
      },
      # ...
    ],
    # ...
  }
]
我想从所有4页中获取给定
:text
:x
:y

我试过这个:

require 'hashie'

coordinates.extend(Hashie::Extensions::DeepLocate)
@hash_array = Hash.new
@hash_array = coordinates.deep_locate -> (key, value, object) { key == :text && value == "XXXX" }
这给了我:

[ { :x => 18.0, :width => 421.59599999999995, :text => "XXXX" } }, 
  { :x => 18.0, :width => 421.59599999999995, :text => "XXXX" }, 
  { :x => 18.0, :width => 421.59599999999995, :text => "XXXX" }, 
  { :x => 18.0, :width => 421.59599999999995, :text => "XXXX" } ]
但是我需要
:x
:y
这样显示:

x = " " and y = " "

我将使用这些值进行进一步验证。

我不知道您是否会接受不使用Hashie的解决方案,但我会这样做:

data = [
  { :page => 1, 
    :lines => [
      { :y => 774.0,
        :text_groups => [ { :x => 18.0, :width => 421.59599999999995, :text => "XXXX" } ]
      },
      # ...
    ]
  },
  { :page => 2, 
    :lines => [
      { :y => 774.0,
        :text_groups => [ { :x => 18.0, :width => 421.59599999999995, :text => "XXXX" } ]
      },
      # ...
    ],
    # ...
  }
]

SEARCH_TEXT = "XXXX"

coords = data.each_with_object([]) do |page, res|
  page[:lines].each do |line|
    line[:text_groups].each do |group|
      next unless group[:text] == SEARCH_TEXT
      res << { x: group[:x], y: line[:y] }
    end
  end
end

p coords
# => [ { :x => 18.0, :y => 774.0 },
#      { :x => 18.0, :y => 774.0 } ]
数据=[
{:page=>1,
:行=>[
{:y=>774.0,
:text_groups=>[{:x=>18.0,:width=>421.5959999995,:text=>“XXXX”}]
},
# ...
]
},
{:page=>2,
:行=>[
{:y=>774.0,
:text_groups=>[{:x=>18.0,:width=>421.5959999995,:text=>“XXXX”}]
},
# ...
],
# ...
}
]
搜索\u TEXT=“XXXX”
coords=数据。每个带有对象([])的对象都有页面,res|
第[:行]页。每行|
行[:文本组]。每个do组|
下一个除非组[:text]==搜索文本
res[{:x=>18.0,:y=>774.0},
#{:x=>18.0,:y=>774.0}]

不清楚您的代码与您提供的示例数据的关系。您的数据没有与“您的到期日更改请求”相同的值。。我更新了问题。我不想显示任何可能是机密的文本。我对显示的结果有疑问。又是散列数组吗?还是一个字符串?结果是一个哈希数组。您可以在这一行上看到散列的创建位置:
res如果我的答案解决了您的问题,请随意接受。您的解决方案确实帮助我解决了我的问题。非常感谢。我只是不知道如何接受你的回答。我现在接受了。