Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

OpenStruct#delete_field

delete_field(name)

Removes the named field from the object. Returns the value that the field contained if it was defined.

Example
require "ostruct"

person = OpenStruct.new(name: "John", age: 70, pension: 300)

person.delete_field!("age")  # => 70
person                       # => #<OpenStruct name="John", pension=300>

Setting the value to nil will not remove the attribute:

Example
person.pension = nil
person                 # => #<OpenStruct name="John", pension=nil>
See also #as_json #dig #[]= #each_pair also in 3.4 · 3.3 · 3.2 · 3.1