# = ostruct.rb: OpenStruct implementation
# Author:: Yukihiro Matsumoto
# Documentation:: Gavin Sinclair
# OpenStruct allows the creation of data objects with arbitrary attributes.
# See OpenStruct for an example.
# OpenStruct allows you to create data objects and set arbitrary attributes.
# record = OpenStruct.new
# record.name = "John Smith"
# puts record.name # -> "John Smith"
# puts record.address # -> nil
# It is like a hash with a different way to access the data. In fact, it is
# implemented with a hash, and you can initialize it with one.
# hash = { "country" => "Australia", :population => 20_000_000 }
# data = OpenStruct.new(hash)
# p data # -> <OpenStruct country="Australia" population=20000000>
# Create a new OpenStruct object. The optional +hash+, if given, will
# generate attributes and values. For example.
# hash = { "country" => "Australia", :population => 20_000_000 }
# data = OpenStruct.new(hash)
# p data # -> <OpenStruct country="Australia" population=20000000>
# By default, the resulting OpenStruct object will have no attributes.
# Duplicate an OpenStruct object members.
def initialize_copy(orig)
@table.each_key{|key| new_ostruct_member(key)}
raise TypeError, "can't modify frozen #{self.class}", caller(2)
def new_ostruct_member(name)
unless self.respond_to?(name)
class << self; self; end.class_eval do
define_method(name) { @table[name] }
define_method("#{name}=") { |x| modifiable[name] = x }
def method_missing(mid, *args) # :nodoc:
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
modifiable[new_ostruct_member(mname)] = args[0]
raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
# Remove the named field from the object.
@table.delete name.to_sym
InspectKey = :__inspect_key__ # :nodoc:
# Returns a string containing a detailed summary of the keys and values.
ids = (Thread.current[InspectKey] ||= [])
if ids.include?(object_id)
str << " #{k}=#{v.inspect}"
attr_reader :table # :nodoc:
# Compare this object and +other+ for equality.
return false unless(other.kind_of?(OpenStruct))
return @table == other.table