# Stupid OpenStruct question

**URL:** <https://www.ruby-forum.com/t/stupid-openstruct-question/174267>\
**Category:** Ruby\
**Created:** [August 24, 2009, 5:05am UTC](https://www.ruby-forum.com/t/stupid-openstruct-question/174267 "2009-08-24T05:05:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![itsterry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2bfe46/32.png) [@itsterry](https://www.ruby-forum.com/u/itsterry)\
**Post date:** [August 24, 2009, 5:05am UTC](https://www.ruby-forum.com/t/stupid-openstruct-question/174267/1 "2009-08-24T05:05:19Z")

</div>

Ok - I know how to get the fields of an Openstruct object like so

class OpenStruct  
def fields  
@table.keys.map{|k| k.to\_s}  
end  
end

a = OpenStruc.new()  
a.foo = 1  
a.bar = 2

a.fields -\> [“foo”, “bar”]

Great - ok how do I WORK with these once I have them?

call doesn’t work [] doesn’t work…

I KNOW this is one of those obvious ones that I am somehow  
overlooking…

Please tell me the simple straightforward thing I am missing here…

---

<div class="post-metadata">

**Author:** ![itsterry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2bfe46/32.png) [@itsterry](https://www.ruby-forum.com/u/itsterry)\
**Post date:** [August 24, 2009, 6:02am UTC](https://www.ruby-forum.com/t/stupid-openstruct-question/174267/2 "2009-08-24T06:02:43Z")

</div>

got it

ff = s.fields

ff.each{|g|  
puts s.instance\_eval g  
}  
1  
2

Terry Smith wrote:

> Ok - I know how to get the fields of an Openstruct object like so
> 
> class OpenStruct  
> def fields  
> @table.keys.map{|k| k.to\_s}  
> end  
> end
> 
> a = OpenStruc.new()  
> a.foo = 1  
> a.bar = 2
> 
> a.fields -\> [“foo”, “bar”]
> 
> Great - ok how do I WORK with these once I have them?
> 
> call doesn’t work [] doesn’t work…
> 
> I KNOW this is one of those obvious ones that I am somehow  
> overlooking…
> 
> Please tell me the simple straightforward thing I am missing here…

---

<div class="post-metadata">

**Author:** ![itsterry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2bfe46/32.png) [@itsterry](https://www.ruby-forum.com/u/itsterry)\
**Post date:** [August 24, 2009, 7:31am UTC](https://www.ruby-forum.com/t/stupid-openstruct-question/174267/3 "2009-08-24T07:31:04Z")

</div>

Object#send is your friend:

> **[Class: Object (Ruby 1.8.7)](https://ruby-doc.org/core-1.8.7/Object.html)**
>
> Class : Object - Ruby 1.8.7

---

<div class="post-metadata">

**Author:** ![itsterry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2bfe46/32.png) [@itsterry](https://www.ruby-forum.com/u/itsterry)\
**Post date:** [August 24, 2009, 8:25am UTC](https://www.ruby-forum.com/t/stupid-openstruct-question/174267/4 "2009-08-24T08:25:43Z")

</div>

On 24.08.2009 06:02, Terry Smith wrote:

> Terry Smith wrote:
> 
> > a.bar = 2
> > 
> > a.fields -\> [“foo”, “bar”]
> > 
> > Great - ok how do I WORK with these once I have them?

Just a remark: if your processing is mainly bases on keys this way then  
a Hash might the better data structure. IMHO an OpenStruct is best used  
when you are lazy or do not know all the fields that you are going to  
use beforehand. I find this especially useful during development, you  
can stuff in more and more fields but maybe later change it to a regular  
class (or Struct generated class).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![itsterry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2bfe46/32.png) [@itsterry](https://www.ruby-forum.com/u/itsterry)\
**Post date:** [August 24, 2009, 8:06am UTC](https://www.ruby-forum.com/t/stupid-openstruct-question/174267/5 "2009-08-24T08:06:59Z")

</div>

maybe you’d be interested in OpenHash? _self promotion_  
([GitHub - karottenreibe/ohash: An OpenStruct replacement, since i hate OpenStruct...](http://github.com/karottenreibe/ohash))

or Mash  
(  
[http://www.intridea.com/2008/4/12/mash-mocking-hash-for-total-poser-objects?blog=company](http://www.intridea.com/2008/4/12/mash-mocking-hash-for-total-poser-objects?blog=company)  
)

Both projects try to remove those oddities and incommodities of  
OpenStruct.

Greetz!

2009/8/24 Manuel V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![itsterry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2bfe46/32.png) [@itsterry](https://www.ruby-forum.com/u/itsterry)\
**Post date:** [August 24, 2009, 11:07am UTC](https://www.ruby-forum.com/t/stupid-openstruct-question/174267/6 "2009-08-24T11:07:52Z")

</div>

2009/8/24 Ralf M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Robert K. wrote:
> 
> > On 24.08.2009 06:02, Terry Smith wrote:

> > > > Great - ok how do I WORK with these once I have them?
> > 
> > Just a remark: if your processing is mainly bases on keys this way then a  
> > Hash might the better data structure. IMHO an OpenStruct is best used when  
> > you are lazy or do not know all the fields that you are going to use  
> > beforehand. I find this especially useful during development, you can stuff  
> > in more and more fields but maybe later change it to a regular class (or  
> > Struct generated class).

> I had a similar experience with using OpenStruct and Hashes and in the end,  
> I derived my class from OpenStruct (build up a hash within the contructor  
> and create object with ‘super()’).

I am not sure what you are trying to convey here. Why would I want to  
create a class that inherits OpenStruct and build up a Hash  
internally? Basically this is what OpenStruct does already:

irb(main):012:0\> o = OpenStruct.new  
=\> #  
irb(main):013:0\> o.instance\_variables  
=\> [:@table]  
irb(main):014:0\> o.instance\_variable\_get(“@table”).class  
=\> Hash

If you need to much access via Hash methods chances are that you  
probably rather want to use a Hash in the first place. That’s what I  
meant.

> But what do you exactly mean by ‘Struct  
> generated class’? Is there a smart way of creating abstract classes (without  
> deriving or explicit coding) from Structs?

Struct.new creates new classes:

irb(main):001:0\> NameValue = Struct.new :name, :value  
=\> NameValue  
irb(main):002:0\> NameValue.class  
=\> Class  
irb(main):003:0\> NameValue.ancestors  
=\> [NameValue, Struct, Enumerable, Object, Kernel, BasicObject]  
irb(main):004:0\> nm = NameValue.new “length”, 123  
=\> #\<struct NameValue name=“length”, value=123\>  
irb(main):005:0\> nm.class  
=\> NameValue

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![itsterry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2bfe46/32.png) [@itsterry](https://www.ruby-forum.com/u/itsterry)\
**Post date:** [August 24, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/stupid-openstruct-question/174267/7 "2009-08-24T12:05:50Z")

</div>

Robert K. wrote:

> > > Just a remark: if your processing is mainly bases on keys this way then a  
> > > and create object with ‘super()’).
> 
> I am not sure what you are trying to convey here. Why would I want to  
> create a class that inherits OpenStruct and build up a Hash  
> internally?  
> The io-part of my app reads text files and creates objects from them  
> (csv-like files with named columns).  
> I use many of the Hash methods internaly, but the OpenStruct gives me  
> all the accessors for the api and the possibility to create objects from  
> other input types (eg. hashs or arrays, coming from a database access)  
> with the same constructor.

Your Struct-example seems more elegant to me. I guess, I should start a  
rewrite…

Thanks,  
ralf

---

<div class="post-metadata">

**Author:** ![itsterry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2bfe46/32.png) [@itsterry](https://www.ruby-forum.com/u/itsterry)\
**Post date:** [August 24, 2009, 10:06am UTC](https://www.ruby-forum.com/t/stupid-openstruct-question/174267/8 "2009-08-24T10:06:59Z")

</div>

Robert K. wrote:

> > > a.foo = 1  
> > > development, you can stuff in more and more fields but maybe later  
> > > change it to a regular class (or Struct generated class).

Hi Robert,  
I had a similar experience with using OpenStruct and Hashes and in the  
end, I derived my class from OpenStruct (build up a hash within the  
contructor and create object with ‘super()’). But what do you exactly  
mean by ‘Struct generated class’? Is there a smart way of creating  
abstract classes (without deriving or explicit coding) from Structs?

regards  
ralf

---

<div class="post-metadata">

**Author:** ![itsterry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/2bfe46/32.png) [@itsterry](https://www.ruby-forum.com/u/itsterry)\
**Post date:** [August 24, 2009, 12:59pm UTC](https://www.ruby-forum.com/t/stupid-openstruct-question/174267/9 "2009-08-24T12:59:06Z")

</div>

2009/8/24 Ralf M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Robert K. wrote:

> > 2009/8/24 Ralf M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > Robert K. wrote:

> > > > in more and more fields but maybe later change it to a regular class (or  
> > > > Struct generated class).

> (csv-like files with named columns).  
> I use many of the Hash methods internaly, but the OpenStruct gives me all  
> the accessors for the api and the possibility to create objects from other  
> input types (eg. hashs or arrays, coming from a database access) with the  
> same constructor.

You are aware that OpenStruct can be constructed with a Hash argument.  
If “intern” and “extern” are also separate phases you could work with  
Hashes internally and convert them to OpenStruct instances when  
handing off to external code.

> Your Struct-example seems more elegant to me. I guess, I should start a  
> rewrite…

🙂 You’ll be interested in method #members:

irb(main):001:0\> Struct.new(:foo,:bar).new.members  
=\> [:foo, :bar]

Kind regards

robert
