2017-10-24 21:08:15 +03:00
|
|
|
# frozen_string_literal: true
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
|
|
|
# = 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.
|
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# An OpenStruct is a data structure, similar to a Hash, that allows the
|
|
|
|
# definition of arbitrary attributes with their accompanying values. This is
|
2011-08-27 02:22:37 +04:00
|
|
|
# accomplished by using Ruby's metaprogramming to define methods on the class
|
2011-05-23 03:46:08 +04:00
|
|
|
# itself.
|
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# == Examples
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# require "ostruct"
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# person = OpenStruct.new
|
2017-03-12 00:32:46 +03:00
|
|
|
# person.name = "John Smith"
|
|
|
|
# person.age = 70
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# person.name # => "John Smith"
|
|
|
|
# person.age # => 70
|
|
|
|
# person.address # => nil
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# An OpenStruct employs a Hash internally to store the attributes and values
|
|
|
|
# and can even be initialized with one:
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# australia = OpenStruct.new(:country => "Australia", :capital => "Canberra")
|
|
|
|
# # => #<OpenStruct country="Australia", capital="Canberra">
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# Hash keys with spaces or characters that could normally not be used for
|
|
|
|
# method calls (e.g. <code>()[]*</code>) will not be immediately available
|
|
|
|
# on the OpenStruct object as a method for retrieval or assignment, but can
|
2020-09-14 21:03:56 +03:00
|
|
|
# still be reached through the Object#send method or using [].
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
|
|
|
# measurements = OpenStruct.new("length (in inches)" => 24)
|
2020-09-14 21:03:56 +03:00
|
|
|
# measurements[:"length (in inches)"] # => 24
|
2017-03-12 00:32:46 +03:00
|
|
|
# measurements.send("length (in inches)") # => 24
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# message = OpenStruct.new(:queued? => true)
|
|
|
|
# message.queued? # => true
|
|
|
|
# message.send("queued?=", false)
|
|
|
|
# message.queued? # => false
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# Removing the presence of an attribute requires the execution of the
|
|
|
|
# delete_field method as setting the property value to +nil+ will not
|
|
|
|
# remove the attribute.
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# first_pet = OpenStruct.new(:name => "Rowdy", :owner => "John Smith")
|
|
|
|
# second_pet = OpenStruct.new(:name => "Rowdy")
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# first_pet.owner = nil
|
|
|
|
# first_pet # => #<OpenStruct name="Rowdy", owner=nil>
|
|
|
|
# first_pet == second_pet # => false
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# first_pet.delete_field(:owner)
|
2017-03-12 00:32:46 +03:00
|
|
|
# first_pet # => #<OpenStruct name="Rowdy">
|
|
|
|
# first_pet == second_pet # => true
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2020-09-14 21:03:56 +03:00
|
|
|
# == Caveats
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2013-02-27 07:36:02 +04:00
|
|
|
# An OpenStruct utilizes Ruby's method lookup structure to find and define the
|
2017-03-12 00:32:46 +03:00
|
|
|
# necessary methods for properties. This is accomplished through the methods
|
|
|
|
# method_missing and define_singleton_method.
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
|
|
|
# This should be a consideration if there is a concern about the performance of
|
2011-08-27 02:22:37 +04:00
|
|
|
# the objects that are created, as there is much more overhead in the setting
|
|
|
|
# of these properties compared to using a Hash or a Struct.
|
2020-09-14 21:03:56 +03:00
|
|
|
# Creating an open struct from a small Hash and accessing a few of the
|
|
|
|
# entries can be 200 times slower than accessing the hash directly.
|
|
|
|
#
|
|
|
|
# This may also be the source of incompatibilities between Ruby versions:
|
|
|
|
#
|
|
|
|
# o = OpenStruct.new
|
|
|
|
# o.then # => nil in Ruby < 2.6, enumerator for Ruby >= 2.6
|
|
|
|
#
|
|
|
|
# Builtin methods may be overwritten this way, which may be a source of bugs
|
|
|
|
# or security issues:
|
|
|
|
#
|
|
|
|
# o = OpenStruct.new
|
|
|
|
# o.methods # => [:to_h, :marshal_load, :marshal_dump, :each_pair, ...
|
|
|
|
# o.methods = [:foo, :bar]
|
|
|
|
# o.methods # => [:foo, :bar]
|
|
|
|
#
|
|
|
|
# To help remedy clashes, OpenStruct uses only protected/private methods ending with `!`
|
|
|
|
# and defines aliases for builtin public methods by adding a `!`:
|
|
|
|
#
|
|
|
|
# o = OpenStruct.new(make: 'Bentley', class: :luxury)
|
|
|
|
# o.class # => :luxury
|
|
|
|
# o.class! # => OpenStruct
|
|
|
|
#
|
|
|
|
# It is recommended (but not enforced) to not use fields ending in `!`.
|
|
|
|
#
|
|
|
|
# For all these reasons, consider not using OpenStruct at all.
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
class OpenStruct
|
2020-07-28 18:31:52 +03:00
|
|
|
VERSION = "0.2.0"
|
2015-12-29 06:48:36 +03:00
|
|
|
|
2020-09-14 21:03:56 +03:00
|
|
|
instance_methods.each do |method|
|
|
|
|
new_name = "#{method}!"
|
|
|
|
alias_method new_name, method
|
|
|
|
end
|
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# Creates a new OpenStruct object. By default, the resulting OpenStruct
|
|
|
|
# object will have no attributes.
|
|
|
|
#
|
2012-10-29 01:19:50 +04:00
|
|
|
# The optional +hash+, if given, will generate attributes and values
|
|
|
|
# (can be a Hash, an OpenStruct or a Struct).
|
2011-05-23 03:46:08 +04:00
|
|
|
# For example:
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# require "ostruct"
|
|
|
|
# hash = { "country" => "Australia", :capital => "Canberra" }
|
2004-02-19 16:42:30 +03:00
|
|
|
# data = OpenStruct.new(hash)
|
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# data # => #<OpenStruct country="Australia", capital="Canberra">
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
def initialize(hash=nil)
|
|
|
|
@table = {}
|
|
|
|
if hash
|
2012-10-29 01:19:50 +04:00
|
|
|
hash.each_pair do |k, v|
|
2020-09-14 20:48:29 +03:00
|
|
|
set_ostruct_member_value!(k, v)
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-12 00:32:46 +03:00
|
|
|
# Duplicates an OpenStruct object's Hash table.
|
|
|
|
def initialize_copy(orig) # :nodoc:
|
2020-09-14 21:03:56 +03:00
|
|
|
orig.table.each_key{|key| new_ostruct_member!(key)}
|
2004-02-20 13:11:25 +03:00
|
|
|
super
|
|
|
|
@table = @table.dup
|
|
|
|
end
|
|
|
|
|
2018-12-22 20:05:03 +03:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# ostruct.to_h -> hash
|
|
|
|
# ostruct.to_h {|name, value| block } -> hash
|
2012-04-24 07:46:55 +04:00
|
|
|
#
|
|
|
|
# Converts the OpenStruct to a hash with keys representing
|
2017-03-12 00:32:46 +03:00
|
|
|
# each attribute (as symbols) and their corresponding values.
|
2012-04-24 07:46:55 +04:00
|
|
|
#
|
2018-12-22 20:05:03 +03:00
|
|
|
# If a block is given, the results of the block on each pair of
|
|
|
|
# the receiver will be used as pairs.
|
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# require "ostruct"
|
|
|
|
# data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
|
|
|
|
# data.to_h # => {:country => "Australia", :capital => "Canberra" }
|
2018-12-22 20:05:03 +03:00
|
|
|
# data.to_h {|name, value| [name.to_s, value.upcase] }
|
|
|
|
# # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" }
|
2012-04-24 07:46:55 +04:00
|
|
|
#
|
2018-12-22 20:05:03 +03:00
|
|
|
def to_h(&block)
|
|
|
|
if block_given?
|
|
|
|
@table.to_h(&block)
|
|
|
|
else
|
|
|
|
@table.dup
|
|
|
|
end
|
2012-04-24 07:46:55 +04:00
|
|
|
end
|
|
|
|
|
2012-10-29 01:18:53 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# :call-seq:
|
|
|
|
# ostruct.each_pair {|name, value| block } -> ostruct
|
|
|
|
# ostruct.each_pair -> Enumerator
|
2012-10-29 01:18:53 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# Yields all attributes (as symbols) along with the corresponding values
|
|
|
|
# or returns an enumerator if no block is given.
|
|
|
|
#
|
|
|
|
# require "ostruct"
|
|
|
|
# data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
|
|
|
|
# data.each_pair.to_a # => [[:country, "Australia"], [:capital, "Canberra"]]
|
2012-10-29 01:18:53 +04:00
|
|
|
#
|
|
|
|
def each_pair
|
2013-10-23 19:13:57 +04:00
|
|
|
return to_enum(__method__) { @table.size } unless block_given?
|
2012-10-29 01:18:53 +04:00
|
|
|
@table.each_pair{|p| yield p}
|
2017-02-03 21:25:28 +03:00
|
|
|
self
|
2012-10-29 01:18:53 +04:00
|
|
|
end
|
|
|
|
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2012-10-29 01:18:11 +04:00
|
|
|
# Provides marshalling support for use by the Marshal library.
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2004-12-03 07:56:25 +03:00
|
|
|
def marshal_dump
|
|
|
|
@table
|
|
|
|
end
|
2011-05-23 03:46:08 +04:00
|
|
|
|
|
|
|
#
|
2012-10-29 01:18:11 +04:00
|
|
|
# Provides marshalling support for use by the Marshal library.
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2004-12-03 07:56:25 +03:00
|
|
|
def marshal_load(x)
|
2020-09-14 21:03:56 +03:00
|
|
|
x.each_key{|key| new_ostruct_member!(key)}
|
2004-12-03 07:56:25 +03:00
|
|
|
@table = x
|
2004-12-02 18:17:35 +03:00
|
|
|
end
|
|
|
|
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2012-10-29 01:18:32 +04:00
|
|
|
# Used internally to defined properties on the
|
2011-05-23 03:46:08 +04:00
|
|
|
# OpenStruct. It does this by using the metaprogramming function
|
2012-10-29 01:19:32 +04:00
|
|
|
# define_singleton_method for both the getter method and the setter method.
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2016-03-03 08:09:02 +03:00
|
|
|
def new_ostruct_member!(name) # :nodoc:
|
2020-09-14 21:03:56 +03:00
|
|
|
unless @table.key?(name)
|
2012-10-29 01:19:32 +04:00
|
|
|
define_singleton_method(name) { @table[name] }
|
2020-09-14 21:03:56 +03:00
|
|
|
define_singleton_method("#{name}=") {|x| @table[name] = x}
|
2004-12-02 18:17:35 +03:00
|
|
|
end
|
|
|
|
end
|
2016-03-03 08:09:02 +03:00
|
|
|
private :new_ostruct_member!
|
|
|
|
|
2020-09-14 21:03:56 +03:00
|
|
|
def freeze
|
|
|
|
@table.freeze
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2020-09-14 21:06:49 +03:00
|
|
|
private def method_missing(mid, *args) # :nodoc:
|
1998-01-16 15:19:09 +03:00
|
|
|
len = args.length
|
2015-04-14 06:35:18 +03:00
|
|
|
if mname = mid[/.*(?==\z)/m]
|
1998-01-16 15:19:09 +03:00
|
|
|
if len != 1
|
2019-04-14 16:47:24 +03:00
|
|
|
raise ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2020-09-14 20:48:29 +03:00
|
|
|
set_ostruct_member_value!(mname, args[0])
|
2020-09-08 22:08:50 +03:00
|
|
|
elsif len == 0
|
2019-04-14 16:47:24 +03:00
|
|
|
elsif @table.key?(mid)
|
|
|
|
raise ArgumentError, "wrong number of arguments (given #{len}, expected 0)"
|
1998-01-16 15:19:09 +03:00
|
|
|
else
|
2017-03-28 12:21:58 +03:00
|
|
|
begin
|
|
|
|
super
|
|
|
|
rescue NoMethodError => err
|
|
|
|
err.backtrace.shift
|
|
|
|
raise
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
2002-11-03 14:04:35 +03:00
|
|
|
|
2012-10-29 01:20:10 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# :call-seq:
|
|
|
|
# ostruct[name] -> object
|
|
|
|
#
|
|
|
|
# Returns the value of an attribute.
|
|
|
|
#
|
|
|
|
# require "ostruct"
|
|
|
|
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
|
|
|
|
# person[:age] # => 70, same as person.age
|
2012-10-29 01:20:10 +04:00
|
|
|
#
|
|
|
|
def [](name)
|
|
|
|
@table[name.to_sym]
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# :call-seq:
|
|
|
|
# ostruct[name] = obj -> obj
|
|
|
|
#
|
|
|
|
# Sets the value of an attribute.
|
2012-10-29 01:20:10 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# require "ostruct"
|
|
|
|
# person = OpenStruct.new("name" => "John Smith", "age" => 70)
|
|
|
|
# person[:age] = 42 # equivalent to person.age = 42
|
|
|
|
# person.age # => 42
|
2012-10-29 01:20:10 +04:00
|
|
|
#
|
|
|
|
def []=(name, value)
|
2020-09-14 21:03:56 +03:00
|
|
|
name = name.to_sym
|
|
|
|
new_ostruct_member!(name)
|
|
|
|
@table[name] = value
|
2012-10-29 01:20:10 +04:00
|
|
|
end
|
2020-09-14 20:48:29 +03:00
|
|
|
alias_method :set_ostruct_member_value!, :[]=
|
|
|
|
private :set_ostruct_member_value!
|
2012-10-29 01:20:10 +04:00
|
|
|
|
2017-03-12 00:32:46 +03:00
|
|
|
# :call-seq:
|
2020-08-13 21:16:27 +03:00
|
|
|
# ostruct.dig(name, *identifiers) -> object
|
2017-03-12 00:32:46 +03:00
|
|
|
#
|
2020-08-13 21:16:27 +03:00
|
|
|
# Finds and returns the object in nested objects
|
|
|
|
# that is specified by +name+ and +identifiers+.
|
|
|
|
# The nested objects may be instances of various classes.
|
2020-08-15 02:55:04 +03:00
|
|
|
# See {Dig Methods}[rdoc-ref:doc/dig_methods.rdoc].
|
2017-03-12 00:32:46 +03:00
|
|
|
#
|
2020-08-13 21:16:27 +03:00
|
|
|
# Examples:
|
2017-03-12 00:32:46 +03:00
|
|
|
# require "ostruct"
|
|
|
|
# address = OpenStruct.new("city" => "Anytown NC", "zip" => 12345)
|
|
|
|
# person = OpenStruct.new("name" => "John Smith", "address" => address)
|
2020-08-13 21:16:27 +03:00
|
|
|
# person.dig(:address, "zip") # => 12345
|
|
|
|
# person.dig(:business_address, "zip") # => nil
|
2015-11-17 08:36:03 +03:00
|
|
|
def dig(name, *names)
|
|
|
|
begin
|
|
|
|
name = name.to_sym
|
|
|
|
rescue NoMethodError
|
2015-12-13 00:40:50 +03:00
|
|
|
raise TypeError, "#{name} is not a symbol nor a string"
|
2015-11-17 08:36:03 +03:00
|
|
|
end
|
|
|
|
@table.dig(name, *names)
|
|
|
|
end
|
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# Removes the named field from the object. Returns the value that the field
|
2012-02-15 07:31:30 +04:00
|
|
|
# contained if it was defined.
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# require "ostruct"
|
|
|
|
#
|
|
|
|
# person = OpenStruct.new(name: "John", age: 70, pension: 300)
|
|
|
|
#
|
|
|
|
# person.delete_field("age") # => 70
|
|
|
|
# person # => #<OpenStruct name="John", pension=300>
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# Setting the value to +nil+ will not remove the attribute:
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# person.pension = nil
|
|
|
|
# person # => #<OpenStruct name="John", pension=nil>
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
def delete_field(name)
|
2010-11-03 08:17:25 +03:00
|
|
|
sym = name.to_sym
|
2015-09-27 02:16:37 +03:00
|
|
|
begin
|
2018-07-26 21:46:10 +03:00
|
|
|
singleton_class.remove_method(sym, "#{sym}=")
|
2015-09-27 02:16:37 +03:00
|
|
|
rescue NameError
|
|
|
|
end
|
|
|
|
@table.delete(sym) do
|
|
|
|
raise NameError.new("no field `#{sym}' in #{self}", sym)
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
|
2005-09-05 12:29:52 +04:00
|
|
|
InspectKey = :__inspect_key__ # :nodoc:
|
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
|
|
|
# Returns a string containing a detailed summary of the keys and values.
|
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
def inspect
|
2009-01-13 15:52:23 +03:00
|
|
|
ids = (Thread.current[InspectKey] ||= [])
|
|
|
|
if ids.include?(object_id)
|
2017-10-24 21:08:15 +03:00
|
|
|
detail = ' ...'
|
|
|
|
else
|
|
|
|
ids << object_id
|
|
|
|
begin
|
|
|
|
detail = @table.map do |key, value|
|
|
|
|
" #{key}=#{value.inspect}"
|
|
|
|
end.join(',')
|
|
|
|
ensure
|
|
|
|
ids.pop
|
2005-09-05 12:29:52 +04:00
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2017-10-24 21:08:15 +03:00
|
|
|
['#<', self.class, detail, '>'].join
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2005-09-05 12:29:52 +04:00
|
|
|
alias :to_s :inspect
|
2003-09-25 04:03:11 +04:00
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
attr_reader :table # :nodoc:
|
2015-01-05 08:27:31 +03:00
|
|
|
protected :table
|
2016-03-03 08:09:02 +03:00
|
|
|
alias table! table
|
2003-09-25 04:03:11 +04:00
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2011-05-23 03:46:08 +04:00
|
|
|
# Compares this object and +other+ for equality. An OpenStruct is equal to
|
2012-10-29 01:18:11 +04:00
|
|
|
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
|
2011-05-23 03:46:08 +04:00
|
|
|
# equal.
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# require "ostruct"
|
|
|
|
# first_pet = OpenStruct.new("name" => "Rowdy")
|
|
|
|
# second_pet = OpenStruct.new(:name => "Rowdy")
|
|
|
|
# third_pet = OpenStruct.new("name" => "Rowdy", :age => nil)
|
|
|
|
#
|
|
|
|
# first_pet == second_pet # => true
|
|
|
|
# first_pet == third_pet # => false
|
|
|
|
#
|
2003-09-25 04:03:11 +04:00
|
|
|
def ==(other)
|
2012-10-29 01:19:15 +04:00
|
|
|
return false unless other.kind_of?(OpenStruct)
|
2016-03-03 08:09:02 +03:00
|
|
|
@table == other.table!
|
2012-10-29 01:19:15 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Compares this object and +other+ for equality. An OpenStruct is eql? to
|
|
|
|
# +other+ when +other+ is an OpenStruct and the two objects' Hash tables are
|
|
|
|
# eql?.
|
|
|
|
#
|
|
|
|
def eql?(other)
|
|
|
|
return false unless other.kind_of?(OpenStruct)
|
2016-03-03 08:09:02 +03:00
|
|
|
@table.eql?(other.table!)
|
2012-10-29 01:19:15 +04:00
|
|
|
end
|
|
|
|
|
2017-03-12 00:32:46 +03:00
|
|
|
# Computes a hash code for this OpenStruct.
|
|
|
|
# Two OpenStruct objects with the same content will have the same hash code
|
|
|
|
# (and will compare using #eql?).
|
|
|
|
#
|
|
|
|
# See also Object#hash.
|
2012-10-29 01:19:15 +04:00
|
|
|
def hash
|
|
|
|
@table.hash
|
2003-09-25 04:03:11 +04:00
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|