diff options
author | Jeremy Evans <[email protected]> | 2019-08-11 13:14:38 -0700 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2020-03-06 20:54:33 +0900 |
commit | e79fc05a4ca672816c6b737d00a85fea4aa6c2b7 (patch) | |
tree | 41d17c47d1322b1920ad4706b056d586d0730a80 /lib/prime.rb | |
parent | 2630757fb53f955d45bb67ccf8187d41b581bca1 (diff) |
[ruby/prime] Fix Prime.include?
Previously, it would be an infinite loop if passed a non-prime
integer.
Also, Prime.include? should also provide similar results to
Module#include? if passed a Module, so handle that.
For consistency with Enumerable#include?, return false if passed
other object types.
Fixes Ruby Bug 10167.
https://github.com/ruby/prime/commit/55dda6aa7f
Diffstat (limited to 'lib/prime.rb')
-rw-r--r-- | lib/prime.rb | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/prime.rb b/lib/prime.rb index 5be12f24f5..44129d2dd7 100644 --- a/lib/prime.rb +++ b/lib/prime.rb @@ -141,6 +141,18 @@ class Prime generator.each(&block) end + # Return true if +obj+ is an Integer an is prime. Also returns + # true if +obj+ is a Module that is an ancestor of +Prime+. + def include?(obj) + case obj + when Integer + prime?(obj) + when Module + Module.instance_method(:include?).bind(Prime).call(obj) + else + false + end + end # Returns true if +value+ is a prime number, else returns false. # |