Bug #10167
closedPrime#include?(mod) hangs up
Description
We expect Prime
class implements Module#include?(mod)
. But Prime#include?(mod)
hangs up, because it is overwritten by Enumerable#include?(obj)
and tries to search mod
linearly from infinite sequence of prime numbers.
Reproducible script:
$ ruby -e 'require "prime"; puts Prime.include?(Enumerable)'
- Expected: returns
true
- Actual: hangs up
I found this bug by the following code (hangs up if prime is required)
enumerables = []
ObjectSpace.each_object(Class){|klass|
if klass.include?(Enumerable) then
enumerables.push klass
end
}
Files
Updated by znz (Kazuhiro NISHIYAMA) almost 10 years ago
workaround:
enumerables = []
module_include_p = Module.method(:include?).unbind
ObjectSpace.each_object(Class) do |klass|
if module_include_p.bind(klass).call(Enumerable)
enumerables.push klass
end
end
Updated by nobu (Nobuyoshi Nakada) almost 10 years ago
- Description updated (diff)
Even Prime.include?(4)
hangs up too.
This method seems useless.
Updated by nitoyon (Kenichi Saita) almost 10 years ago
- File prime-include.diff prime-include.diff added
It's difficult to delete Prime.inlude?
method because this method is included by include Enumerable
.
So, I overrided Prime.include?
and Prime.instance.include?
. Please review my patch.
Updated by nitoyon (Kenichi Saita) almost 10 years ago
Review my patch, please!
Updated by marcandre (Marc-Andre Lafortune) over 9 years ago
- Assignee set to yugui (Yuki Sonoda)
Instead of aliasing include?
to prime?
, I'd recommend instead to restore it:
Prime.define_singleton_method(:include?, Module.instance_method(:include?))
Prime.include?(Enumerable) # => true
Probably won't be super useful, but still beats an infinite loop :-)
Assigning to yugui, although I have not seen any activity from her in over a year on the mailing list?
Updated by jeremyevans0 (Jeremy Evans) almost 5 years ago
I've added a pull request for this at https://github.com/ruby/prime/pull/6.
Updated by jeremyevans0 (Jeremy Evans) over 4 years ago
- Status changed from Open to Closed