Class: Prime::PseudoPrimeGenerator
- Includes:
- Enumerable
- Defined in:
- lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb
Overview
An abstract class for enumerating pseudo-prime numbers.
Concrete subclasses should override succ, next, rewind.
Direct Known Subclasses
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterates the given block for each prime numbers.
-
#initialize(ubound = nil) ⇒ PseudoPrimeGenerator
constructor
A new instance of PseudoPrimeGenerator.
-
#next ⇒ Object
alias of
succ
. -
#rewind ⇒ Object
Rewinds the internal position for enumeration.
-
#succ ⇒ Object
returns the next pseudo-prime number, and move the internal position forward.
- #upper_bound ⇒ Object
- #upper_bound=(ubound) ⇒ Object
-
#with_object(obj) ⇒ Object
see
Enumerator
#with_object.
Methods included from Enumerable
#chunk, #count, #cycle, #drop, #drop_while, #each_entry, #each_with_index_with_optional_args_and_block, #each_with_object, #entries_with_optional_arguments, #find_index, #first, #flat_map, #group_by, #inject_with_symbol, #lazy, #max_by, #min_by, #minmax, #minmax_by, #none?, #one?, #reverse_each, #slice_before, #sum, #take, #take_while, #to_a_with_optional_arguments
Constructor Details
#initialize(ubound = nil) ⇒ PseudoPrimeGenerator
Returns a new instance of PseudoPrimeGenerator.
228 229 230 |
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 228 def initialize(ubound = nil) @ubound = ubound end |
Instance Method Details
#each(&block) ⇒ Object
Iterates the given block for each prime numbers.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 260 def each(&block) return self.dup unless block if @ubound last_value = nil loop do prime = succ break last_value if prime > @ubound last_value = block.call(prime) end else loop do block.call(succ) end end end |
#next ⇒ Object
alias of succ
.
248 249 250 |
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 248 def next raise NotImplementedError, "need to define `next'" end |
#rewind ⇒ Object
Rewinds the internal position for enumeration.
See Enumerator
#rewind.
255 256 257 |
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 255 def rewind raise NotImplementedError, "need to define `rewind'" end |
#succ ⇒ Object
returns the next pseudo-prime number, and move the internal position forward.
PseudoPrimeGenerator
#succ raises NotImplementedError
.
243 244 245 |
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 243 def succ raise NotImplementedError, "need to define `succ'" end |
#upper_bound ⇒ Object
235 236 237 |
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 235 def upper_bound @ubound end |
#upper_bound=(ubound) ⇒ Object
232 233 234 |
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 232 def upper_bound=(ubound) @ubound = ubound end |
#with_object(obj) ⇒ Object
see Enumerator
#with_object.
280 281 282 283 284 285 |
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 280 def with_object(obj) return enum_for(:with_object) unless block_given? each do |prime| yield prime, obj end end |