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
32
votes
2 answers

When should I use an OpenStruct instead of a Hash?

I like the "definition of arbitrary attributes" and I think the OpenStruct in ruby sometimes feels cleaner than using a hash, but I'm curious as to whether there are other specific advantages or use cases that make an OpenStruct a better choice than…
mkelley33
  • 5,323
  • 10
  • 47
  • 71
11
votes
6 answers

Raise exception when accessing attributes that doesn't exist in OpenStruct

I'm trying to figure out how to make it so that a subclass of OpenStruct (or any class for that matter), or hash, will raise a custom exception if I try to access an attribute that hasn't been set. I couldn't get define_method and method_missing to…
Seralize
  • 1,117
  • 11
  • 27
10
votes
2 answers

Create nested object from YAML to access attributes via method calls in Ruby

I am completely new to ruby. I have to parse a YAML file to construct an object YAML File projects: - name: Project1 developers: - name: Dev1 certifications: - name: cert1 - name: Dev2 certifications: …
Harshul Pandav
  • 1,016
  • 11
  • 23
8
votes
2 answers

Can i extend a Ruby class to behave like OpenStruct dynamically?

I have a Ruby class which includes a module. I want the including class to behave like OpenStruct. How do i achieve this without explicitly inheriting from OpenStruct? class Book include MyModule end module MyModule def self.included(klass) …
Sathish
  • 20,660
  • 24
  • 63
  • 71
6
votes
5 answers

How to convert recursive/nested OpenStruct Object to Hash

I have an OpenStruct object and needs to convert to JSON data. Sample Hash (from RSPEC helper): def test_order { "id": 505311428702, "email": "test@gmail.com", "closed_at": "", "discount_codes": { "id": 507328175, "text":…
aldrien.h
  • 3,437
  • 2
  • 30
  • 52
5
votes
1 answer

Construct nested OpenStruct object

I have to mimic a Google API response and create a 2-level deep data structure that is traversable by . like this: => user.names.first_name Bob Is there any smarter/better way than this: user = OpenStruct.new(names: OpenStruct.new(first_name:…
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
4
votes
1 answer

OpenStruct.new stores attribute but doesn't retrieve it

After creating a new Ruby OpenStruct object, I am able to store attributes but not to retrieve them (I get a blank line and it returns nil instead): obj = OpenStruct.new # => # obj.x = 10 obj.y = 20 obj # => #
andersonvom
  • 11,701
  • 4
  • 35
  • 40
3
votes
1 answer

Use JSON.parse to parse OpenStruct or a hash

I tried to parse a simple JSON like that: JSON.parse({"pong": "ok"}) and it failed 2.4.0 :014 > JSON.parse({"pong": "ok"}) TypeError: no implicit conversion of Hash into String from (irb):14 What's wrong here ? Why should I convert to String ?…
belgoros
  • 3,590
  • 7
  • 38
  • 76
3
votes
2 answers

Using marshal_load with OpenStruct

How do I use OpenStruct's marshal_load utility? It doesn't appear to work as intended. The docs give this example, but it doesn't appear to work. require 'ostruct' event = OpenStruct.new hash = { 'time' => Time.now, 'title' => 'Birthday Party'…
Cody A. Ray
  • 5,869
  • 1
  • 37
  • 31
2
votes
1 answer

how to solve extra `table` & `modifiable` merging issue while while json parsing as OpenStruct

JSON.parse(response, object_class: OpenStruct) rescue response My rails application has above line of code which is causing an weird issue. Here response is coming from an external API call and looks like…
Dev
  • 794
  • 1
  • 9
  • 21
2
votes
1 answer

Ruby Parsing json array with OpenStruct

I'm trying to parse a json file with OpenStruct. Json file has an array for Skills. When I parse it I get some extra "garbage" returned. How do I get rid of it? json { "Job": "My Job 1", "Skills": [{ "Name": "Name 1", "ClusterName":…
runski74
  • 277
  • 2
  • 4
  • 10
2
votes
1 answer

OpenStruct issue with Ruby 2.3.1

In Ruby 2.1.5 and 2.2.4, creating a new Collector returns the correct result. require 'ostruct' module ResourceResponses class Collector < OpenStruct def initialize super @table = Hash.new {|h,k| h[k] = Response.new } end …
Brit200313
  • 728
  • 5
  • 20
2
votes
3 answers

Ruby: Can you determine if an object is having one of its methods called?

I'm not sure if I'm even asking the right question. I may be approaching the problem incorrectly, but basically I have this situation here: obj = get_user(params) obj.profile => {:name => "John D", :age => 40, :sex => "male"} #Has to be of class…
user1561753
  • 357
  • 2
  • 3
  • 13
2
votes
0 answers

Validate presence/validity of a key/value in Ruby Openstruct

Is there a standard way to validate key/values (presence of a key, whether it's a valid value) in Ruby's Openstruct? In a pure Ruby, no Rails/Sinatra, but in a similar way to validations in ActiveRecord or ActiveModel::Validations is doing. For…
nacrafts
  • 51
  • 1
2
votes
1 answer

Ruby and OpenStruct allowing access to only some data

I have an array of OpenStruct data structures. But when I try and access them I can only access country array = [ #
Max Rose-Collins
  • 1,904
  • 5
  • 26
  • 45
1
2 3