diff --git a/lib/set.rb b/lib/set.rb index 5a96c81832..e7d1be4f9f 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -464,7 +464,15 @@ class Set # Set['a', 'b', 'z'] & ['a', 'b', 'c'] #=> # def &(enum) n = self.class.new - do_with_enum(enum) { |o| n.add(o) if include?(o) } + if enum.is_a?(Set) + if enum.size > size + each { |o| n.add(o) if enum.include?(o) } + else + enum.each { |o| n.add(o) if include?(o) } + end + else + do_with_enum(enum) { |o| n.add(o) if include?(o) } + end n end alias intersection &