ostruct icon indicating copy to clipboard operation
ostruct copied to clipboard

Add has_key? method to OpenStruct class

Open djberube opened this issue 10 months ago • 6 comments

Adds has_key? method to OpenStruct class to check if a given name exists as a member, which allows checking for presence of a key without creating an accessor method; it also allows for distinguishing between a method which has never been set and one which been set to nil.

djberube avatar Feb 19 '25 18:02 djberube

What is the usecase?

marcandre avatar Feb 19 '25 19:02 marcandre

@djberube I would argue that this method should be named #include? as in Hash#include?, and subsequently aliased as #has_key? and #key?. What do you think?

ingemar avatar May 30 '25 09:05 ingemar

Hey, for me it's will helpful when i need to validate the structure and serialize JSON and API'S.

I am really interested of that and i want to finish implementing the feature of seeking keys in OpenStruct. Can i just create my own fork and give Co-Authoring to @djberube when it will be done?

o-200 avatar Sep 21 '25 11:09 o-200

@djberube I would argue that this method should be named #include? as in Hash#include?, and subsequently aliased as #has_key? and #key?. What do you think?

Makes sense; we should follow what Hash does.

Adjusted as requested.

@o-200 I have made the requested changes, but feel free to suggest any changes that would help your use case.

djberube avatar Sep 21 '25 13:09 djberube

@o-200 I have made the requested changes,

Thanks! Appreciate that you returned after six months to finish that pull request.

o-200 avatar Sep 21 '25 14:09 o-200

@djberube This is excellent. Thank you!

ingemar avatar Sep 22 '25 06:09 ingemar