如何循环OpenStruct密钥/值对



我有以下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">

Ruby代码:

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">

如果你只是想获得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

您正试图在哈希数组上循环。

请参阅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"]>]

功能上没有区别。

相关内容

  • 没有找到相关文章

最新更新