No key set in openstruct ?

J

jney

Hello,

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

Robert Klemme

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

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

jney

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

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

Thank you Robert.
The problem with this method is that the field definition is still
remaining even after delete :
x = OpenStruct.new
=> # said:
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=]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top