Bug #10167

Prime#include?(mod) hangs up

Added by Kenichi Saita over 1 year ago. Updated about 1 year ago.

Assignee:Yuki Sonoda
ruby -v:ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux] Backport:2.0.0: UNKNOWN, 2.1: UNKNOWN


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 = []
  if klass.include?(Enumerable) then
    enumerables.push klass

prime-include.diff Magnifier - fix Prime.include? and Prime.instance.include? (1.21 KB) Kenichi Saita, 08/29/2014 05:05 PM


#1 Updated by Kazuhiro NISHIYAMA over 1 year ago


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

#2 Updated by Nobuyoshi Nakada over 1 year ago

  • Description updated (diff)

Even Prime.include?(4) hangs up too.
This method seems useless.

#3 Updated by Kenichi Saita over 1 year ago

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.

#4 Updated by Kenichi Saita about 1 year ago

Review my patch, please!

#5 Updated by Marc-Andre Lafortune about 1 year ago

  • Assignee set to 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?

Also available in: Atom PDF