Project

General

Profile

Bug #10167 ยป prime-include.diff

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

View differences:

lib/prime.rb
99 99
    warn "Prime::new is obsolete. use Prime::instance or class methods of Prime."
100 100
  end
101 101

  
102
  def include?(n) # :nodoc:
103
    prime?(n) # avoid infinite loop
104
  end
105

  
102 106
  class << self
103 107
    extend Forwardable
104 108
    include Enumerable
109

  
110
    def include?(n) # :nodoc:
111
      if n.is_a?(Module)
112
        # Class#include?
113
        included_modules.include?(n)
114
      else
115
        # Enumerable#include?
116
        prime?(n) # avoid infinite loop
117
      end
118
    end
119

  
105 120
    # Returns the default instance of Prime.
106 121
    def instance; @the_instance end
107 122

  
test/test_prime.rb
171 171

  
172 172
    assert_not_include Prime.each(7*37).to_a, 7*37, "[ruby-dev:39465]"
173 173
  end
174

  
175
  def test_include?
176
    assert Prime.include?(2)
177
    assert !Prime.include?(4)
178

  
179
    assert Prime.instance.include?(2)
180
    assert !Prime.instance.include?(4)
181

  
182
    # Class#include?
183
    assert Prime.include?(Enumerable)
184
    assert !Prime.include?(String)
185
  end
174 186
end