[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Get OpenStruct attributes

Edgardo Hames

3/12/2008 7:05:00 PM

Dear rubyists,

What's the best way to get the list of attributes of an OpenStruct?

Thanks in advance,
Edgardo
9 Answers

Aaron Patterson

3/12/2008 7:53:00 PM

0

On Thu, Mar 13, 2008 at 04:04:57AM +0900, Ed Hames wrote:
> Dear rubyists,
>
> What's the best way to get the list of attributes of an OpenStruct?

Not perfect, but this might do the trick:

>> require 'ostruct'
=> true
>> x = OpenStruct.new(:foo => 'asdf')
=> #<OpenStruct foo="asdf">
>> x.methods - OpenStruct.instance_methods
=> ["foo", "foo="]
>>

--
Aaron Patterson
http://tenderlovem...

Jason Roelofs

3/12/2008 7:55:00 PM

0

Given this setup:

>> require 'ostruct'
=> false
>> o = OpenStruct.new
=> #<OpenStruct>
>> o.field = "this"
=> "this"
>> o.fold = "that"
=> "that"
>> o
=> #<OpenStruct field="this", fold="that">


The "nice" way:

>> o.methods - Object.methods - OpenStruct.instance_methods
=> ["fold", "fold=", "field", "field="]

Or maybe go one more level, to kick out the attr= methods:

(o.methods - Object.methods - OpenStruct.instance_methods).reject
{|method| method =~ /=$/ }

The "dirty" way:

>> o.instance_variable_get("@table")
=> {:field=>"this", :fold=>"that"}

Jason

On Wed, Mar 12, 2008 at 3:04 PM, Ed Hames <ehames@gmail.com> wrote:
> Dear rubyists,
>
> What's the best way to get the list of attributes of an OpenStruct?
>
> Thanks in advance,
> Edgardo
>
>

Martin DeMello

3/12/2008 7:59:00 PM

0

On Wed, Mar 12, 2008 at 12:04 PM, Ed Hames <ehames@gmail.com> wrote:
> Dear rubyists,
>
> What's the best way to get the list of attributes of an OpenStruct?

A bit hackish, but

object.methods(nil).grep /[^=]$/

martin

Martin DeMello

3/12/2008 8:02:00 PM

0

On Wed, Mar 12, 2008 at 12:54 PM, Jason Roelofs <jameskilton@gmail.com> wrote:

> The "dirty" way:
>
> >> o.instance_variable_get("@table")
> => {:field=>"this", :fold=>"that"}

That's actually the cleanest way - it's proof against singleton
methods being added to the object.

martin

Trans

3/12/2008 8:04:00 PM

0



On Mar 12, 3:04 pm, Ed Hames <eha...@gmail.com> wrote:
> Dear rubyists,
>
> What's the best way to get the list of attributes of an OpenStruct?

o = OpenStruct.new(:a=>1,:b=>2)
=> #<OpenStruct a=1, b=2>
>> o.instance_variable_get("@table")
=> {:a=>1, :b=>2}

T.

Arlen Cuss

3/13/2008 11:26:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Thu, Mar 13, 2008 at 7:04 AM, Trans <transfire@gmail.com> wrote:

> o = OpenStruct.new(:a=>1,:b=>2)
> => #<OpenStruct a=1, b=2>
> >> o.instance_variable_get("@table")
> => {:a=>1, :b=>2}
>

Looking into internals sounds dangerous. How about -

>> a = OpenStruct.new
=> #<OpenStruct>
>> a.field = "this"
=> "this"
>> a.old = "that"
=> "that"
>> a.methods false
=> ["field=", "old=", "field", "old"]
>>

Arlen

Trans

3/13/2008 3:12:00 PM

0



On Mar 13, 7:26 am, "Arlen Cuss" <cel...@sairyx.org> wrote:
> Hi,
>
> On Thu, Mar 13, 2008 at 7:04 AM, Trans <transf...@gmail.com> wrote:
> > o = OpenStruct.new(:a=>1,:b=>2)
> > => #<OpenStruct a=1, b=2>
> > >> o.instance_variable_get("@table")
> > => {:a=>1, :b=>2}
>
> Looking into internals sounds dangerous. How about -

It may be the only reliable way. Try #dup on an OpenStruct and see
what happens.

T.

Arlen Cuss

3/13/2008 10:53:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Fri, Mar 14, 2008 at 2:11 AM, Trans <transfire@gmail.com> wrote:

> It may be the only reliable way. Try #dup on an OpenStruct and see
> what happens.


Very good point. Thank you. I think it'd be good if OpenStruct told us about
those fields, though. (e.g. by creating the methods on dup)

Arlen

Trans

3/14/2008 4:31:00 PM

0



On Mar 13, 6:53 pm, "Arlen Cuss" <cel...@sairyx.org> wrote:
> Hi,
>
> On Fri, Mar 14, 2008 at 2:11 AM, Trans <transf...@gmail.com> wrote:
> > It may be the only reliable way. Try #dup on an OpenStruct and see
> > what happens.
>
> Very good point. Thank you. I think it'd be good if OpenStruct told us about
> those fields, though. (e.g. by creating the methods on dup)

I agree, which is why I created OpenObject (in Facets). OpenObject
will also happily override any built in method, unlike OpenStruct. On
the other hand I was also convinced that a light weight solution is
often all that is needed, so I recently added OpenHash as well. So
that's another alternative.

T.