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

o.inspect == “#”
=> true

o.one_field = true
=> true

o.inspect == “#”
=> false

o.delete_field ‘one_field’
=> nil

o.inspect == “#”
=> true

On 23.10.2010 19:16, jney wrote:

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

On Oct 23, 8:03pm, Robert K. [email protected] wrote:

=> true
=> [:foo, :foo=]


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

On Sat, Oct 23, 2010 at 10:15 PM, jney [email protected]
wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs