Skip to content

Instantly share code, notes, and snippets.

@mattdaw
Created December 23, 2009 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattdaw/262677 to your computer and use it in GitHub Desktop.
Save mattdaw/262677 to your computer and use it in GitHub Desktop.
From 22a82748695ee808264d7a39c7b55092f548f346 Mon Sep 17 00:00:00 2001
From: Matt Daw <matt@matt-daws-macbook-pro.local>
Date: Tue, 29 Dec 2009 20:26:22 -0500
Subject: [PATCH] Fixes for Struct#eql? and Struct#new.
* In Struct#new raise TypeError if objects aren't symbols or strings.
* In Struct#eql? do object comparison rather than ==, and use to_a on both sides to confirm equality of values.
---
kernel/common/struct.rb | 17 +++++++++++------
spec/tags/ruby/core/struct/eql_tags.txt | 4 +---
spec/tags/ruby/core/struct/new_tags.txt | 2 +-
3 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/kernel/common/struct.rb b/kernel/common/struct.rb
index 606b821..13d1991 100644
--- a/kernel/common/struct.rb
+++ b/kernel/common/struct.rb
@@ -48,10 +48,15 @@ class Struct
end
end
- begin
- attrs = attrs.map { |attr| attr.to_sym }
- rescue NoMethodError => e
- raise TypeError, e.message
+ attrs = attrs.map do |attr|
+ case attr
+ when Symbol
+ attr
+ when String
+ attr.to_sym
+ else
+ raise TypeError, "#{attr.inspect} is not a symbol"
+ end
end
raise ArgumentError if attrs.any? { |attr| attr.nil? }
@@ -221,9 +226,9 @@ class Struct
# fields are equal (using <tt>eql?</tt>).
def eql?(other)
- return true if self == other
+ return true if equal? other
return false if self.class != other.class
- to_a.eql? other
+ to_a.eql? other.to_a
end
def each(&block)
diff --git a/spec/tags/ruby/core/struct/eql_tags.txt b/spec/tags/ruby/core/struct/eql_tags.txt
index d234b10..c607049 100644
--- a/spec/tags/ruby/core/struct/eql_tags.txt
+++ b/spec/tags/ruby/core/struct/eql_tags.txt
@@ -1,3 +1 @@
-fails:Struct#eql? handles recursive structures by returning false if a difference can be found
-fails:Struct#eql? returns false if any corresponding elements are not #eql?
-fails:Struct#eql? returns false if any corresponding elements are not #eql?
+fails:Struct#eql? handles recursive structures by returning false if a difference can be found
diff --git a/spec/tags/ruby/core/struct/new_tags.txt b/spec/tags/ruby/core/struct/new_tags.txt
index 0b88dd4..8b13789 100644
--- a/spec/tags/ruby/core/struct/new_tags.txt
+++ b/spec/tags/ruby/core/struct/new_tags.txt
@@ -1 +1 @@
-fails:Struct.new raises a TypeError if object is not a Symbol
+
--
1.6.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment