Project

General

Profile

Actions

Bug #10167

closed

Prime#include?(mod) hangs up

Added by nitoyon (Kenichi Saita) over 9 years ago. Updated about 4 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
[ruby-core:64546]
Tags:

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

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

Updated by znz (Kazuhiro NISHIYAMA) over 9 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) over 9 years ago

  • Description updated (diff)

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

Updated by nitoyon (Kenichi Saita) over 9 years 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.

Updated by nitoyon (Kenichi Saita) over 9 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?

Actions #6

Updated by naruse (Yui NARUSE) over 6 years ago

  • Target version deleted (2.2.0)
Actions #8

Updated by jeremyevans0 (Jeremy Evans) about 4 years ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0