[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

why does openstruct not respond to [] and []=?

Martin DeMello

12/1/2006 12:15:00 PM

Took me aback - there seems to be no reason for OpenStruct *not* to
permit member access via ostruct[:field] and ostruct['field'].

martin

5 Answers

Robert Klemme

12/1/2006 12:23:00 PM

0

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

Martin DeMello

12/1/2006 12:36:00 PM

0

On 12/1/06, Robert Klemme <shortcutter@googlemail.com> 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

Yukihiro Matsumoto

12/1/2006 1:22:00 PM

0

Hi,

In message "Re: why does openstruct not respond to [] and []=?"
on Fri, 1 Dec 2006 21:35:42 +0900, "Martin DeMello" <martindemello@gmail.com> 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.


Robert Klemme

12/1/2006 1:34:00 PM

0

On 01.12.2006 13:35, Martin DeMello wrote:
> On 12/1/06, Robert Klemme <shortcutter@googlemail.com> 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.

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

Martin DeMello

12/1/2006 2:15:00 PM

0

On 12/1/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> In message "Re: why does openstruct not respond to [] and []=?"
> on Fri, 1 Dec 2006 21:35:42 +0900, "Martin DeMello" <martindemello@gmail.com> 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?

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

martin