Skip to content

Commit e38ab84

Browse files
Durable Programming, LLCdjberube
authored andcommitted
Add has_key? method to OpenStruct class
Adds has_key? method to OpenStruct class to check if a given name exists as a member. This allows checking for presence of a key without creating an accessor method. Method accepts both string and symbol keys, converting them to symbols before checking. Includes comprehensive test coverage for various usage scenarios.
1 parent 0e1c1a7 commit e38ab84

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

lib/ostruct.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,30 @@ def delete_field(name, &block)
383383
end
384384
end
385385

386+
387+
# Returns +true+ if the given name is a member of this OpenStruct object, +false+ otherwise.
388+
# The given +name+ is converted to a symbol before checking.
389+
#
390+
# require "ostruct"
391+
# person = OpenStruct.new("name" => "John Smith", :age => 70)
392+
# person.has_key?(:name) # => true
393+
# person.has_key?("age") # => true
394+
# person.has_key?(:phone) # => false
395+
#
396+
# This method can be used to test for the presence of a value without creating
397+
# an accessor method if it doesn't exist:
398+
#
399+
# person = OpenStruct.new
400+
# person.name = "John"
401+
# person.has_key?(:name) # => true
402+
# person.has_key?(:age) # => false
403+
# person.age # => nil (but creates an accessor)
404+
# person.has_key?(:age) # => false
405+
#
406+
def has_key?(name)
407+
@table.has_key?(name.to_sym)
408+
end
409+
386410
InspectKey = :__inspect_key__ # :nodoc:
387411

388412
#

test/ostruct/test_ostruct.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,51 @@ def test_performance_warning
439439
)
440440
end
441441
end
442+
def setup
443+
end
444+
445+
def test_has_key_with_symbol
446+
o = OpenStruct.new(name: "John Smith", age: 70, pension: 300)
447+
assert_true o.has_key?(:name)
448+
assert_true o.has_key?(:age)
449+
assert_true o.has_key?(:pension)
450+
assert_false o.has_key?(:address)
451+
end
452+
453+
def test_has_key_with_string
454+
o = OpenStruct.new(name: "John Smith", age: 70, pension: 300)
455+
assert_true o.has_key?("name")
456+
assert_true o.has_key?("age")
457+
assert_true o.has_key?("pension")
458+
assert_false o.has_key?("address")
459+
end
460+
461+
def test_has_key_after_deletion
462+
o = OpenStruct.new(name: "John Smith", age: 70, pension: 300)
463+
o.delete_field(:name)
464+
assert_false o.has_key?(:name)
465+
end
466+
467+
def test_has_key_with_nil_value
468+
o = OpenStruct.new(name: "John Smith", age: 70, pension: 300)
469+
o.pension = nil
470+
assert_true o.has_key?(:pension)
471+
end
472+
473+
def test_has_key_with_new_ostruct
474+
os = OpenStruct.new
475+
assert_false os.has_key?(:any_key)
476+
end
477+
478+
def test_has_key_after_setting_value
479+
o = OpenStruct.new(name: "John Smith", age: 70, pension: 300)
480+
o.phone = "123-456-7890"
481+
assert_true o.has_key?(:phone)
482+
end
483+
484+
def test_has_key_case_sensitivity
485+
o = OpenStruct.new(name: "John Smith", age: 70, pension: 300)
486+
assert_true o.has_key?(:name)
487+
assert_false o.has_key?(:Name)
488+
end
442489
end

0 commit comments

Comments
 (0)