lib/thrift/struct.rb in thrift-0.6.0 vs lib/thrift/struct.rb in thrift-0.7.0
- old
+ new
@@ -53,19 +53,19 @@
yield self if block_given?
end
def fields_with_default_values
- fields_with_default_values = self.class.instance_variable_get("@fields_with_default_values")
+ fields_with_default_values = self.class.instance_variable_get(:@fields_with_default_values)
unless fields_with_default_values
fields_with_default_values = {}
struct_fields.each do |fid, field_def|
unless field_def[:default].nil?
fields_with_default_values[field_def[:name]] = field_def[:default]
end
end
- self.class.instance_variable_set("@fields_with_default_values", fields_with_default_values)
+ self.class.instance_variable_set(:@fields_with_default_values, fields_with_default_values)
end
fields_with_default_values
end
def inspect(skip_optional_nulls = true)
@@ -112,27 +112,30 @@
oprot.write_field_stop
oprot.write_struct_end
end
def ==(other)
+ return false if other.nil?
each_field do |fid, field_info|
name = field_info[:name]
- return false unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}")
+ return false unless other.respond_to?(name) && self.send(name) == other.send(name)
end
true
end
def eql?(other)
self.class == other.class && self == other
end
+ # This implementation of hash() is inspired by Apache's Java HashCodeBuilder class.
def hash
- field_values = []
+ total = 17
each_field do |fid, field_info|
name = field_info[:name]
- field_values << self.instance_variable_get("@#{name}")
+ value = self.send(name)
+ total = (total * 37 + value.hash) & 0xffffffff
end
- field_values.hash
+ total
end
def differences(other)
diffs = []
unless other.is_a?(self.class)