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
0
votes
0 answers

Sending mail 'to' OpenStruct through mailer

I have an app where users can sign up for workshops and admin has a possibility to write an e-mail to all the participants through the app. The fragment of code to send mail message to the group looks like this workshop.students_all.each do…
bochen421
  • 161
  • 3
  • 14
0
votes
1 answer

Rails 4 overwrite ActiveRecord Model variables after init

I store somewhat large JSON strings as text fields in my model but would like to have the data accessible as an OpenStruct variable when the instance of the model is initialized. In other words: Model: CrawlCache Field: results, type: text #Storing…
Killerpixler
  • 4,200
  • 11
  • 42
  • 82
0
votes
1 answer

catch-all getter method on openstruct?

If I have an OpenStruct: require 'ostruct' open_struct = OpenStruct.new I can overwrite [] which works in some cases open_struct.define_singleton_method(:[]) do |*args| puts args.map(&:class) puts args end open_struct.a = 1 open_struct[:a] #…
max pleaner
  • 26,189
  • 9
  • 66
  • 118
0
votes
0 answers

how to return random sampling of data from hash

I've parsed a json file (which consists of a single array called "items" containing 4 key-value pairs) into an OpenStruct so that I can treat my data as if they were objects. I would now like to display each object at random on a web page. # read…
calyxofheld
  • 1,538
  • 3
  • 24
  • 62
0
votes
0 answers

How to convert string to openstruct

I have a string which is an openstruct object. How can I convert it to again to an openstruct? #> This is a string how can I again convert it to the openstruct object, so that I can…
AJinkya
  • 21
  • 8
0
votes
2 answers

Overriding getter of OpenStruct in order to print it as a Hash

GOAL: Values of an OpenStruct object should be printed as a hash rather than an object POSSIBLE SOLUTION: Override getter of the OpenStruct class MyOpenStruct overrides new, to_h and [] of OpenStruct. class MyOpenStruct < OpenStruct def…
Animesh Pandey
  • 5,900
  • 13
  • 64
  • 130
0
votes
3 answers

Unique objects in grouped hash

I have a hash, created by .group_by method, with strings as keys and AR objects as values. And I want to get rid of AR objects with duplicated field(other fields might or might not be the same). To clarify the question I've created this…
Rustam Gasanov
  • 15,290
  • 8
  • 59
  • 72
0
votes
2 answers

Converting OpenStruct/Hash to XML

I have a collection of OpenStruct elements using which I need to build an XML with help of Nokogiri. collection = [ OpenStruct.new(:catalogStoreNumber => '657758', :catalogStoreId => 'CTH6536', :catalogStoreLocation => 'UnitedStates',…
user1023627
  • 183
  • 1
  • 3
  • 12
-1
votes
1 answer

Defining OpenStruct attribute with same name as instance method

I'm running into an issue when trying to create an open struct with an attribute that has the same name as one of the OpenStruct instance methods. Specifically, i'd like to create an open struct that has an attribute capture. I'm using this as a…
PhilVarg
  • 4,762
  • 2
  • 19
  • 37
1 2
3