PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Get OpenStruct attributes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Get OpenStruct attributes

Réponse
 
LinkBack Outils de la discussion
Vieux 12/03/2008, 20h04   #1
Ed Hames
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Get OpenStruct attributes

Dear rubyists,

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

Thanks in advance,
Edgardo
  Réponse avec citation
Vieux 12/03/2008, 20h53   #2
Aaron Patterson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get OpenStruct attributes

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://tenderlovemaking.com/

  Réponse avec citation
Vieux 12/03/2008, 20h54   #3
Jason Roelofs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get OpenStruct attributes

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


  Réponse avec citation
Vieux 12/03/2008, 20h59   #4
Martin DeMello
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get OpenStruct attributes

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

  Réponse avec citation
Vieux 12/03/2008, 21h01   #5
Martin DeMello
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get OpenStruct attributes

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

  Réponse avec citation
Vieux 12/03/2008, 21h04   #6
Trans
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get OpenStruct attributes



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.

  Réponse avec citation
Vieux 13/03/2008, 12h26   #7
Arlen Cuss
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get OpenStruct attributes

[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

  Réponse avec citation
Vieux 13/03/2008, 16h11   #8
Trans
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get OpenStruct attributes



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.

  Réponse avec citation
Vieux 13/03/2008, 23h53   #9
Arlen Cuss
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get OpenStruct attributes

[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

  Réponse avec citation
Vieux 14/03/2008, 17h31   #10
Trans
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get OpenStruct attributes



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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18096 seconds with 18 queries