Questions tagged [openstruct]

An OpenStruct is a Ruby data structure, similar to a Hash.

An OpenStruct is a data structure, similar to a Hash, that allows the definition of arbitrary attributes with their accompanying values. This is accomplished by using Ruby’s metaprogramming to define methods on the class itself.

39 questions
1
vote
2 answers

How to loop OpenStruct key/value pair

I have the following OpenStruct data structure, I'm trying to get key/value pair > #, # operator="jp",…
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
1
vote
2 answers

Can I append attributes to Ruby OpenStruct on the go?

I am new to Ruby and this is a really basic question, when I searched for adding/appending values to OpenStruct, I couldn't find any resource. I'm trying to wrap the response body with extra params and the code in place uses OpenStruct. Now I need…
1
vote
1 answer

How to update OpenStruct value with condition

How can I update values of OpenStruct when a conditions is met? I thought like this: o = OpenStruct.new(a: 1, b: 2) o.each_pair{|k,v| v = 3 if v.even? } But this code doesn't work. I could update by this code, but it's quite hard to…
ironsand
  • 14,329
  • 17
  • 83
  • 176
1
vote
1 answer

Pass Gmail Connection to sidekiq

require gmail gmail = Gmail.connect("testemail@gmail.com", "password") SidekiqWorker.perform_async(gmail, item.user_id) gmail.logout() I want to pass a object to sidekiq,It is a gmail client object,And I don't want to create that object in perform…
AJinkya
  • 21
  • 8
1
vote
1 answer

How OpenStruct stored in session

I have some controller. In this controller I get OpenStruct object and want to save it to app session. Next code works fine: session[:info] = OpenStruct.new(first_field: 1, second_field: 'two') p session[:info] right after this line prints…
Dmitry
  • 351
  • 1
  • 4
  • 10
1
vote
0 answers

Parse JSON string to object with attributes

Currently my app is parsing JSON in following way: OpenStruct.new(JSON.parse(json_string)) but there are problems with this approach: JSON allows root-level arrays like [{id: 1, name: "first"}, {id: 2, name: "second"}] and OpenStruct can't handle…
Paul
  • 25,812
  • 38
  • 124
  • 247
1
vote
1 answer

undefined method 'each_pair' with openstruct

I'm using OpenStruct to manipulate a json like this: OpenStruct.new '{"age":100,"name":"name"}' and I'm getting NoMethodError: undefined method `each_pair' for "{\"age\":100,\"name\":\"name\"":String this piece of code was working some…
Luiz E.
  • 6,769
  • 10
  • 58
  • 98
1
vote
1 answer

Ruby on Rails String to OpenStruct

I have the following OpenStruct object but if I call .class on it is a string. How do I turn it back into a OpenStruct object? [ #
Max Rose-Collins
  • 1,904
  • 5
  • 26
  • 45
1
vote
1 answer

How Can I Convert Nested YAML to nested Arrays and OpenStructs in Ruby

How should I convert a series of nested hashes (nested to arbitrary depth) to a series of nested OpenStructs? I'm loading in a big YAML file and I'm not enjoying accessing['everything']['like']['this']. I have found a few partial solutions using…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
1
vote
2 answers

Why do OpenStruct values change when a hash with its dumped values change?

I'd like to know what's happening. When I do the following: new_struct = OpenStruct.new() new_struct.color = 'Red' new_struct.number = 4 This results in: # If I then create and change some params: params = {…
Lee
  • 601
  • 9
  • 21
1
vote
0 answers

Does OpenStruct has support for nesting ? If Yes, How can I construct XML using Nokogiri? If not, Can this be done using Nokogiri and JSon?

The Earlier solution provided by @Jamie seems to be working fine for only few scenario's. The solution is require 'nokogiri' require 'ostruct' xml = <<-'XML'
user1023627
  • 183
  • 1
  • 3
  • 12
0
votes
1 answer

How to programmatically add attributes to objects?

I have an array of hashes, which I would rather be turned into an array of objects with attributes programmatically added to those objects. I'm try this at the moment obj = OpenStruct.new resulthash["users"].collect { |u| u.each do |k,v| …
Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84
0
votes
0 answers

OpenStruct Behavioural Changes while upgrading ruby 2.7 to 3.1

I was having a OpenStruct value like #. It was updated to #[]}, modifiable=false> automatically. Has anyone else encountered this issues before? Can you please provide a solution. There is nothing…
0
votes
0 answers

Rails 7 create camelcase output for dry::struct object

I've to build a massive JSON file based on few different API calls. I want to use Dry::Struct To easily manipulate data like: obj = Test::DataStruct.new obj.send_envelope_description = 1 but expected output should be camelcase: #
mr_muscle
  • 2,536
  • 18
  • 61
0
votes
1 answer

How to modify OpenStruct in the instantiated class

I have the following code where I instantiate an object that returns an OpenStruct result. require 'ostruct' class TestModel attr_accessor :result def initializer end def result OpenStruct.new(successful?: false) end def…
anna
  • 65
  • 6