Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
OpenStruct.new
() -> ::OpenStruct
new(hash=nil)
lib/ostruct.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.4.1/lib/ostruct.rb, line 134
def initialize(hash=nil)
if HAS_PERFORMANCE_WARNINGS && Warning[:performance]
warn "OpenStruct use is discouraged for performance reasons", uplevel: 1, category: :performance
end
if hash
update_to_values!(hash)
else
@table = {}
end
end
Creates a new OpenStruct object. By default, the resulting OpenStruct object will have no attributes.
The optional hash, if given, will generate attributes and values (can be a Hash, an OpenStruct or a Struct). For example:
Example
require "ostruct" hash = { "country" => "Australia", :capital => "Canberra" } data = OpenStruct.new(hash) data # => #<OpenStruct country="Australia", capital="Canberra">