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
|
|
|
|
2020-01-03 01:02:26 +03:00
|
|
|
require_relative 'ostruct/version'
|
|
|
|
|
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
|
|
|
|
# still be reached through the Object#send method.
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
|
|
|
# measurements = OpenStruct.new("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
|
|
|
#
|
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# == Implementation
|
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.
|
2004-02-19 16:42:30 +03:00
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
class OpenStruct
|
2015-12-29 06:48:36 +03:00
|
|
|
|
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|
|
|
|
|
k = k.to_sym
|
|
|
|
@table[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:
|
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)
|
|
|
|
@table = x
|
2004-12-02 18:17:35 +03:00
|
|
|
end
|
|
|
|
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2012-10-29 01:18:11 +04:00
|
|
|
# Used internally to check if the OpenStruct is able to be
|
2012-02-15 07:31:30 +04:00
|
|
|
# modified before granting access to the internal Hash table to be modified.
|
2011-05-23 03:46:08 +04:00
|
|
|
#
|
2016-03-03 08:09:02 +03:00
|
|
|
def modifiable? # :nodoc:
|
2009-02-27 08:23:10 +03:00
|
|
|
begin
|
|
|
|
@modifiable = true
|
|
|
|
rescue
|
2018-08-07 15:47:11 +03:00
|
|
|
exception_class = defined?(FrozenError) ? FrozenError : RuntimeError
|
|
|
|
raise exception_class, "can't modify frozen #{self.class}", caller(3)
|
2009-02-15 15:43:46 +03:00
|
|
|
end
|
|
|
|
@table
|
|
|
|
end
|
2016-03-03 08:09:02 +03:00
|
|
|
private :modifiable?
|
|
|
|
|
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:
|
2005-09-05 12:29:52 +04:00
|
|
|
name = name.to_sym
|
2015-12-31 08:37:21 +03:00
|
|
|
unless singleton_class.method_defined?(name)
|
2012-10-29 01:19:32 +04:00
|
|
|
define_singleton_method(name) { @table[name] }
|
2016-03-03 08:09:02 +03:00
|
|
|
define_singleton_method("#{name}=") {|x| modifiable?[name] = x}
|
2004-12-02 18:17:35 +03:00
|
|
|
end
|
2009-02-15 15:43:46 +03:00
|
|
|
name
|
2004-12-02 18:17:35 +03:00
|
|
|
end
|
2016-03-03 08:09:02 +03:00
|
|
|
private :new_ostruct_member!
|
|
|
|
|
2015-12-31 21:26:46 +03:00
|
|
|
def freeze
|
2016-03-03 08:09:02 +03:00
|
|
|
@table.each_key {|key| new_ostruct_member!(key)}
|
2015-12-31 21:26:46 +03:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2017-03-12 00:32:46 +03:00
|
|
|
def respond_to_missing?(mid, include_private = false) # :nodoc:
|
2015-10-14 19:57:21 +03:00
|
|
|
mname = mid.to_s.chomp("=").to_sym
|
2019-05-10 06:48:43 +03:00
|
|
|
defined?(@table) && @table.key?(mname) || super
|
2015-10-14 19:57:21 +03:00
|
|
|
end
|
|
|
|
|
2004-02-19 16:42:30 +03:00
|
|
|
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
|
2016-03-03 08:09:02 +03:00
|
|
|
modifiable?[new_ostruct_member!(mname)] = args[0]
|
2017-03-28 12:21:58 +03:00
|
|
|
elsif len == 0 # and /\A[a-z_]\w*\z/ =~ mid #
|
2015-10-14 19:57:21 +03:00
|
|
|
if @table.key?(mid)
|
2016-03-03 08:09:02 +03:00
|
|
|
new_ostruct_member!(mid) unless frozen?
|
2015-10-14 19:57:21 +03:00
|
|
|
@table[mid]
|
|
|
|
end
|
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)
|
2016-03-03 08:09:02 +03:00
|
|
|
modifiable?[new_ostruct_member!(name)] = value
|
2012-10-29 01:20:10 +04:00
|
|
|
end
|
|
|
|
|
2015-11-17 08:36:03 +03:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# :call-seq:
|
|
|
|
# ostruct.dig(name, ...) -> object
|
|
|
|
#
|
|
|
|
# Extracts the nested value specified by the sequence of +name+
|
|
|
|
# objects by calling +dig+ at each step, returning +nil+ if any
|
|
|
|
# intermediate step is +nil+.
|
|
|
|
#
|
|
|
|
# require "ostruct"
|
|
|
|
# address = OpenStruct.new("city" => "Anytown NC", "zip" => 12345)
|
|
|
|
# person = OpenStruct.new("name" => "John Smith", "address" => address)
|
2015-11-17 08:36:03 +03:00
|
|
|
#
|
2017-03-12 00:32:46 +03:00
|
|
|
# person.dig(:address, "zip") # => 12345
|
|
|
|
# person.dig(:business_address, "zip") # => nil
|
|
|
|
#
|
|
|
|
# data = OpenStruct.new(:array => [1, [2, 3]])
|
|
|
|
#
|
|
|
|
# data.dig(:array, 1, 0) # => 2
|
|
|
|
# data.dig(:array, 0, 0) # TypeError: Integer does not have #dig method
|
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
|