class OpenStruct

def delete_field(name)


person # => #
person.pension = nil

Setting the value to +nil+ will not remove the attribute:

person # => #
person.delete_field("age") # => 70

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

require "ostruct"

contained if it was defined.
Removes the named field from the object. Returns the value that the field
def delete_field(name)
  sym = name.to_sym
  begin
    singleton_class.remove_method(sym, "#{sym}=")
  rescue NameError
  end
  @table.delete(sym) do
    raise NameError.new("no field `#{sym}' in #{self}", sym)
  end
end