# Why does openstruct not respond to =?

**URL:** <https://www.ruby-forum.com/t/why-does-openstruct-not-respond-to/77902>\
**Category:** Ruby\
**Created:** [December 1, 2006, 1:15pm UTC](https://www.ruby-forum.com/t/why-does-openstruct-not-respond-to/77902 "2006-12-01T13:15:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 1, 2006, 1:15pm UTC](https://www.ruby-forum.com/t/why-does-openstruct-not-respond-to/77902/1 "2006-12-01T13:15:41Z")

</div>

Took me aback - there seems to be no reason for OpenStruct _not_ to  
permit member access via ostruct[:field] and ostruct[‘field’].

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 1, 2006, 1:25pm UTC](https://www.ruby-forum.com/t/why-does-openstruct-not-respond-to/77902/2 "2006-12-01T13:25:42Z")

</div>

On 01.12.2006 13:14, Martin DeMello wrote:

> Took me aback - there seems to be no reason for OpenStruct _not_ to  
> permit member access via ostruct[:field] and ostruct[‘field’].

OpenStruct also does not inherit Enumerable. I guess the story is, if  
you need a Hash then use a Hash. The key point of OpenStruct is that  
you can use arbitrary member setters and getters not indexed access. Is  
there actually a situation where you need both?

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 1, 2006, 1:37pm UTC](https://www.ruby-forum.com/t/why-does-openstruct-not-respond-to/77902/3 "2006-12-01T13:37:45Z")

</div>

On 12/1/06, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 01.12.2006 13:14, Martin DeMello wrote:
> 
> > Took me aback - there seems to be no reason for OpenStruct _not_ to  
> > permit member access via ostruct[:field] and ostruct[‘field’].
> 
> OpenStruct also does not inherit Enumerable. I guess the story is, if  
> you need a Hash then use a Hash. The key point of OpenStruct is that  
> you can use arbitrary member setters and getters not indexed access. Is  
> there actually a situation where you need both?

I was trying to collect all the binary options to my app in a hash (as  
being somewhat less verbose than the standard OptionParser syntax):

{  
:verbose =\> [“-v”, “–[no-]verbose”, “run verbosely”],  
:all =\> [“-A”, “–all”, “select all files”],  
#…  
}.each {|k,v| opt.on(\*v) {|i| opts.send(:“#{k}=”, i) } }

The last line would have been a lot less ugly as opts[k] = i, and as I  
said, there seems no real reason not to allow it.

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 1, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/why-does-openstruct-not-respond-to/77902/4 "2006-12-01T14:23:51Z")

</div>

Hi,

In message “Re: why does openstruct not respond to [] and []=?”  
on Fri, 1 Dec 2006 21:35:42 +0900, “Martin DeMello”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|{  
| :verbose =\> [“-v”, “–[no-]verbose”, “run verbosely”],  
| :all =\> [“-A”, “–all”, “select all files”],  
| #…  
|}.each {|k,v| opt.on(\*v) {|i| opts.send(:“#{k}=”, i) } }  
|  
|The last line would have been a lot less ugly as opts[k] = i, and as I  
|said, there seems no real reason not to allow it.

Is there any reason that you have to use OpenStruct instead of plain  
hash as opts?

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 1, 2006, 3:16pm UTC](https://www.ruby-forum.com/t/why-does-openstruct-not-respond-to/77902/5 "2006-12-01T15:16:05Z")

</div>

On 12/1/06, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there any reason that you have to use OpenStruct instead of plain  
> hash as opts?

In the rest of the code I’d far rather use opts.option than  
opts[:option] - the latter ends up looking cluttered.

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 1, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/why-does-openstruct-not-respond-to/77902/6 "2006-12-01T14:35:55Z")

</div>

On 01.12.2006 13:35, Martin DeMello wrote:

> I was trying to collect all the binary options to my app in a hash (as  
> being somewhat less verbose than the standard OptionParser syntax):
> 
> {  
> :verbose =\> ["-v", “–[no-]verbose”, “run verbosely”],  
> :all =\> ["-A", “–all”, “select all files”],  
> #…  
> }.each {|k,v| opt.on(\*v) {|i| opts.send(:"#{k}=", i) } }
> 
> The last line would have been a lot less ugly as opts[k] = i, and as I  
> said, there seems no real reason not to allow it.

Hm… Personally I would prefer the slightly more verbose but less  
complex definition of options. Just my 0.02EUR.

Btw, you can of course remedy this simply by just defining #[]= on  
instance opts the way you used it here. 🙂

Kind regards

```
robert
```
