0

我有以下 OpenStruct 数据结构,我正在尝试获取键/值对

> #<OpenStruct conditions=[#<OpenStruct field="Out_of_country", operator="us", values=["true"]>, #<OpenStruct field="Status__c",
> operator="jp", values=["'Approved'"]>, #<OpenStruct field="Status__c",
> operator="gb", values=["'Rejected'"]>], conjunction="and">

红宝石代码:

dataResult = nil
dataResult = data['condition'].include?('out_of_country']

这就是你从 OpenStruct 中提取键/值对的方式吗?

更新:

你是对的,我正在使用to_s并且我删除了,这就是我试图访问键/值的内容

myresult = data['mainCondtion']
p myresult --> I got this result:

  > #<OpenStruct conditions=[#<OpenStruct field="Out_of_country", operator="us", values=["true"]>, #<OpenStruct field="Status__c",
    > operator="jp", values=["'Approved'"]>, #<OpenStruct field="Status__c",
    > operator="gb", values=["'Rejected'"]>], conjunction="and">

然后我尝试访问fieldvalues

myresult.each_pair{ |key, value| puts "#{key}: #{value}" }

我收到此错误:

# 未定义的方法“each_pair”

4

2 回答 2

0

如果您只是想获取 OpenStruct 的单个属性,您只需在其上调用 getter 方法:

country = OpenStruct(name: 'Japan', alpha_2: "jp", alpha_3: "jpn")
puts country.name # japan
puts country.alpha_3 # jpn

这就是 OpenStruct 的重点。您会得到一个行为类似于类实例的对象,而无需实际拥有类或必须像在普通 Struct 上那样提前定义属性。

您可以使用OpenStruct#each_pair来循环其属性,就像使用哈希一样:

os = OpenStruct.new a: 1, b: 2, c: 3
os.each_pair{ |key, value| puts "#{key}: #{value}" }
# Outputs:
# a: 1
# b: 2
# c: 3
于 2020-05-07T17:22:56.380 回答
0

您正在尝试遍历哈希数组。

请参阅 SO 上的示例答案 -如何迭代哈希数组并返回单个字符串中的值?

@max 不喜欢我没有发布长篇解释,所以我将扩展我的上述答案。

irb(main):029:0> o = OpenStruct.new(foo: "bar", bar: "foo")
=> #<OpenStruct foo="bar", bar="foo">
irb(main):032:0> h = Hash.new
=> {}
irb(main):033:0> h[:foo] = "bar"
=> "bar"
irb(main):034:0> h[:bar] = "foo"
=> "foo"
irb(main):035:0> o
=> #<OpenStruct foo="bar", bar="foo">
irb(main):036:0> h
=> {:foo=>"bar", :bar=>"foo"}
irb(main):037:0> o.respond_to?(:each_pair)
=> true
irb(main):038:0> h.respond_to?(:each_pair)
=> true

OpenStructs 和 Hashes 都响应 @max 推荐的方法 - 在您描述的情况下,它们在功能上没有什么不同。

irb(main):040:0> one = OpenStruct.new(field: "Out_of_country", operator: "us", values: ["true"])
=> #<OpenStruct field="Out_of_country", operator="us", values=["true"]>
irb(main):041:0> two = OpenStruct.new(field: "Out_of_country", operator: "jp", values: ["true"])
=> #<OpenStruct field="Out_of_country", operator="jp", values=["true"]>
irb(main):042:0> OpenStruct.new(conditions: [one, two])
=> #<OpenStruct conditions=[#<OpenStruct field="Out_of_country", operator="us", values=["true"]>, #<OpenStruct field="Out_of_country", operator="jp", values=["true"]>]>
irb(main):043:0> c = _
=> #<OpenStruct conditions=[#<OpenStruct field="Out_of_country", operator="us", values=["true"]>, #<OpenStruct field="Out_of_country", operator="jp", values=["true"]>]>
irb(main):044:0> hash_one = {field: "Out_of_country", operator: "us", values: ["true"]}
=> {:field=>"Out_of_country", :operator=>"us", :values=>["true"]}
irb(main):045:0> hash_two = {field: "Out_of_country", operator: "jp", values: ["true"]}
=> {:field=>"Out_of_country", :operator=>"jp", :values=>["true"]}
irb(main):046:0> hash_conditions = [hash_one, hash_two]
=> [{:field=>"Out_of_country", :operator=>"us", :values=>["true"]}, {:field=>"Out_of_country", :operator=>"jp", :values=>["true"]}]

irb(main):052:0> hash_conditions.each { |e| puts e[:operator] }
us
jp
=> [{:field=>"Out_of_country", :operator=>"us", :values=>["true"]}, {:field=>"Out_of_country", :operator=>"jp", :values=>["true"]}]
irb(main):053:0> c.conditions.each { |e| puts e[:operator] }
us
jp
=> [#<OpenStruct field="Out_of_country", operator="us", values=["true"]>, #<OpenStruct field="Out_of_country", operator="jp", values=["true"]>]

没有功能上的区别。

于 2020-05-07T17:01:37.490 回答