No key set in openstruct?

Hello,

  Is there a cleaner way to check if no field have been set than the
following code ?

o = OpenStruct.new

=> #<OpenStruct>

o.inspect == "#<OpenStruct>"

=> true

o.one_field = true

=> true

o.inspect == "#<OpenStruct>"

=> false

o.delete_field 'one_field'

=> nil

o.inspect == "#<OpenStruct>"

=> true

I'd consider this a tad cleaner - although it is not really rock solid:

irb(main):015:0> x.methods - x.class.instance_methods
=> [:foo, :foo=]
irb(main):016:0> x = OpenStruct.new
=> #<OpenStruct>
irb(main):017:0> x.foo = 1
=> 1
irb(main):018:0> x.methods - x.class.instance_methods
=> [:foo, :foo=]

Kind regards

  robert

···

On 23.10.2010 19:16, jney wrote:

   Is there a cleaner way to check if no field have been set than the
following code ?

o = OpenStruct.new

=> #<OpenStruct>

o.inspect == "#<OpenStruct>"

=> true

o.one_field = true

=> true

o.inspect == "#<OpenStruct>"

=> false

o.delete_field 'one_field'

=> nil

o.inspect == "#<OpenStruct>"

=> true

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

> Is there a cleaner way to check if no field have been set than the
> following code ?

>>>> o = OpenStruct.new
> => #<OpenStruct>
>>>> o.inspect == "#<OpenStruct>"
> => true
>>>> o.one_field = true
> => true
>>>> o.inspect == "#<OpenStruct>"
> => false
>>>> o.delete_field 'one_field'
> => nil
>>>> o.inspect == "#<OpenStruct>"
> => true

I'd consider this a tad cleaner - although it is not really rock solid:

irb(main):015:0> x.methods - x.class.instance_methods
=> [:foo, :foo=]
irb(main):016:0> x = OpenStruct.new
=> #<OpenStruct>
irb(main):017:0> x.foo = 1
=> 1
irb(main):018:0> x.methods - x.class.instance_methods
=> [:foo, :foo=]

Kind regards

    robert

--
remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/

Thank you Robert.
The problem with this method is that the field definition is still
remaining even after delete :

x = OpenStruct.new

=> #<OpenStruct>

x.foo=1

=> 1

x.methods - x.class.instance_methods

=> [:foo, :foo=]

x.delete_field :foo

=> 1

x.methods - x.class.instance_methods

=> [:foo, :foo=]

···

On Oct 23, 8:03 pm, Robert Klemme <shortcut...@googlemail.com> wrote:

On 23.10.2010 19:16, jney wrote:

<snip>

x.foo=1

=> 1

x.methods - x.class.instance_methods

=> [:foo, :foo=]

x.delete_field :foo

=> 1

x.methods - x.class.instance_methods

=> [:foo, :foo=]

Hmm I'd consdider this a bug, maybe report this to ruby-core
Cheers
Robert

···

On Sat, Oct 23, 2010 at 10:15 PM, jney <jeansebastien.ney@gmail.com> wrote:

--
There are worse things than having people misunderstand your work. A
worse danger is that you will yourself misunderstand your work.
-- Paul Graham