Get OpenStruct attributes

Dear rubyists,

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

Thanks in advance,
Edgardo

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="]

···

On Thu, Mar 13, 2008 at 04:04:57AM +0900, Ed Hames wrote:

--
Aaron Patterson
http://tenderlovemaking.com/

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

A bit hackish, but

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

martin

···

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?

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.

···

On Mar 12, 3:04 pm, Ed Hames <eha...@gmail.com> wrote:

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

martin

···

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"}

Hi,

  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

···

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

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

T.

···

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 -

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

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.

···

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)