# open\_struct

Published articles for open\_struct.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Rails Form Technique - Using collection\_select with Open Structs

DevFeed: [Rails Form Technique - Using collection\_select with Open Structs](<https://devfeed.tech/articles/rails-form-technique-using-collection-select-with-open-structs-28264.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/rails/2020/03/01/rails-form-technique-using-collection-select-with-open-structs.html>)

Author: Fuzzygroup

Published: 2020-03-01T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Data structures](<https://devfeed.tech/topics/data-structures.md>)

Tags: [array](<https://devfeed.tech/tags/array.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [list](<https://devfeed.tech/tags/list.md>), [open-struct](<https://devfeed.tech/tags/open-struct.md>), [rails](<https://devfeed.tech/tags/rails.md>)

### AI overview

A Rails tutorial explains how to use collection_select with an array of OpenStruct objects when the selectable data is not a database collection. It shows matching the expected id and name methods in the controller and view.

### Source excerpt

I've written about Open Structs before: Sorting Responding to Method Calls To review, an Open Struct is a hash like data structure that responds to dot methods (i.e. .id or .name). I use these in a lot of different places and I just finished using them for a form collection_select operation. The Rails collection_select form helper is really designed to work with database objects that you need to make pick lists from. The canonical usage is like this: <%= form.label :project_id %> <%= form.collection_select :project_id, current_user.projects.all, :id, :name %> The expression current_user.projects.all is a list of all projects as an iterable ActiveRecord collection and what collection_select does under the hood is loop over the collection and call the .id and .name methods (.id is the value in the collection and .name is what's displayed). And this works great - until what you have to select isn't a database collection. And that, dear reader, is where open structs come out to play! Here's what I just did: Controller Side @contexts = [] @contexts << OpenStruct.new(id: "web_browser_on_computer", name: "Web Browser (Desktop Computer)") @contexts << OpenStruct.new(id: "mobile_app", name: "Mobile App") This defines an array of open struct objects. And an array is an iterable collection just like the database collection so this implementation, despite being an entirely different underlying data structure, works just like the ActiveRecord collection based version. View Side And here's the view side: <%= form.label :context %> <%= form.collection_select :context, @contexts, :id, :name %> Appearance Here's how this looks by default and then when dropped down:

## When You Need a Ruby Data Structure that Responds to Method Calls - Use OpenStruct

DevFeed: [When You Need a Ruby Data Structure that Responds to Method Calls - Use OpenStruct](<https://devfeed.tech/articles/when-you-need-a-ruby-data-structure-that-responds-to-method-calls-use-openstruct-28305.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/ruby/2020/02/09/when-you-need-a-ruby-data-structure-that-responds-to-method-calls-use-openstruct.html>)

Author: Fuzzygroup

Published: 2020-02-09T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Data structures](<https://devfeed.tech/topics/data-structures.md>), [Mocking](<https://devfeed.tech/topics/mocking.md>), [API](<https://devfeed.tech/topics/api.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [data-structure](<https://devfeed.tech/tags/data-structure.md>), [mocking](<https://devfeed.tech/tags/mocking.md>), [open-struct](<https://devfeed.tech/tags/open-struct.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>)

### AI overview

A Ruby developer uses OpenStruct to simulate an API response while a server-side component needs configuration data before the real API is available. The article explains that OpenStruct supports dot-method access and belongs to Ruby's standard library, with a require needed in pure Ruby.

### Source excerpt

I'm in the position of building a system where there is one source of truth for configuration - an API from the the user interface component of the system (this is the part that will always be online so it made sense for it to serve the api). The problem is that I have a server side component that needs to go online today as in now. And that API isn't yet available. I basically have a section in my code where I get the configuration data. My normal approach is to create a ruby Mechanize object and do a page fetch against the configuration API. This returns a response object with a .body method that can be parsed something like this: response = agent.get(dest_url) JSON.parse(response.body) My means of working around this without an API is to use an OpenStruct as follows: response = OpenStruct.new response.body = '{ "kafka_servers":["b-2.AAA.ccc.c4.kafka.us-east-2.amazonaws.com:9092","b-1.BBB.ccc.c4.kafka.us-east-2.amazonaws.com:9092"]}' JSON.parse(response.body) OpenStruct is a hash like data structure that responds to ruby dot messages so my "response" object above in the second code block will respond to ".body". Overall OpenStructs are a very, very useful tool for this type of pseudo mocking behavior. Note: OpenStruct is built into Rails so you can just a "response = OpenStruct.new" at any point. If you are building pure Ruby software then you need to first require it with: require 'ostruct' and then use it with "response = OpenStruct.new". There is no Gem component to use OpenStruct - it is a part of the Ruby standard library but it isn't available until you do a require.