Method: OpenStruct#initialize

Defined in:
lib/ostruct.rb

#initialize(hash = nil) ⇒ OpenStruct

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:

require "ostruct"
hash = { "country" => "Australia", :capital => "Canberra" }
data = OpenStruct.new(hash)

data   # => #<OpenStruct country="Australia", capital="Canberra">


134
135
136
137
138
139
140
141
142
143
144
# File '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