From: jney Date: 2010-10-24T05:15:15+09:00 Subject: Re: No key set in openstruct ? On Oct 23, 8:03 pm, Robert Klemme wrote: > 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 > > =>  # > >>>> o.inspect == "#" > > =>  true > >>>> o.one_field = true > > =>  true > >>>> o.inspect == "#" > > =>  false > >>>> o.delete_field 'one_field' > > =>  nil > >>>> o.inspect == "#" > > =>  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 > => # > 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 => # >>> 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=]