Reputation: 20078
I have the following OpenStruct data structure, I'm trying to get key/value pair
> #<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 code:
dataResult = nil
dataResult = data['condition'].include?('out_of_country']
is that how you extract key/value pair from OpenStruct?
UPDATE:
you're right I was using the to_s
and I remove and here is what I'm trying to access the key/value
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">
then I try to access field
and values
myresult.each_pair{ |key, value| puts "#{key}: #{value}" }
I get this error:
undefined method "each_pair" for #
Upvotes: 1
Views: 2974
Reputation: 744
You are trying to loop over an array of hashes.
See this example answer on SO - How do I iterate over an array of hashes and return the values in a single string?
@max didn't like that I didn't post a long-form explanation, so I'm going to expand on my above answer.
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
Both OpenStructs and Hashes respond to the method that @max recommends - making them no different functionally in the case that you are describing.
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"]>]
There is no functional difference.
Upvotes: 0
Reputation: 101821
If you are just looking to get a single attribute of an OpenStruct you just call the getter method on it:
country = OpenStruct(name: 'Japan', alpha_2: "jp", alpha_3: "jpn")
puts country.name # japan
puts country.alpha_3 # jpn
This is kind of the whole point of an OpenStruct. You get an object that behaves like an instance of a class without actually having a class or having to define the attributes in advance like on a normal Struct.
You can use OpenStruct#each_pair to loop though its attributes like you would with a hash:
os = OpenStruct.new a: 1, b: 2, c: 3
os.each_pair{ |key, value| puts "#{key}: #{value}" }
# Outputs:
# a: 1
# b: 2
# c: 3
Upvotes: 2