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)
lib/ostruct.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.0.5/lib/ostruct.rb, line 325
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
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>