From: Rob Biedenharn Date: 2006-12-27T12:23:52+09:00 Subject: Re: Peculiar Behavior for a Newby to Undertstand On Dec 26, 2006, at 8:20 PM, Charles A Gray wrote: > I created a class for prime numbers as so: > > class Primes > def initialize > end > > def prime?(number) > # Method returns true if number is prime. > limit = Math.sqrt(number).ceil > flag = true > if number % 2 == 0 > flag = false > else > 3.step(limit,2) {|i| > if number % i == 0 > flag = false > break > end > } > end > return flag > end > > def show_primes(lower, upper) > # Prints all primes between lower and upper > # Lower is incremented 1 if it is even. > # The arcane "(((lower/2).floor)*2+1)" performs this task > (((lower/2).floor)*2+1).step(upper,2) {|i| > if prime?(i) == true > print i.to_s + " " > end > } > end > end > Then when I enter > a=primes.new > a.show_primes(1000000,1000100) I get > 1000003 1000033 1000037 1000039 1000081 1000099 1000001 > Where is that trailing 1000001 coming from? It is not a prime > number and > in fact is the lower limit. > If I enter the same methods outside of class Primes and enter > show_primes(1000000,1000100) I don't get the lower limit at the end of > the printed values. > > I am running version 1.18.4 using scite in Ubuntu. > -- > Charles Gray -- Phoenix, AZ; Where you can bake the chill out of your > bones The value of the #step method is the initial value so that is being returned from a.show_primes and presumably printed by whatever you're using to interpret your statements (like irb): With your code in a file names "primes.rb" $ irb -rprimes >> a=Primes.new => # >> a.show_primes(1_000_000, 1_000_100) 1000003 1000033 1000037 1000039 1000081 1000099 => 1000001 >> a.show_primes(1_000_000, 1_000_100); nil 1000003 1000033 1000037 1000039 1000081 1000099 => nil >> a.show_primes(1_000_000, 1_000_100); puts "" 1000003 1000033 1000037 1000039 1000081 1000099 => nil Note that the value of the last expression is displayed by irb itself. (In the last example, the puts supplies a newline and the value of puts as an expression is nil.) To see the documentation for the step method (after first trying Fixnum#step and Integer#step rather than looking it up in the pickaxe ;-) $ ri -T Numeric#step ----------------------------------------------------------- Numeric#step num.step(limit, step ) {|i| block } => num ------------------------------------------------------------------------ Invokes _block_ with the sequence of numbers starting at _num_, incremented by _step_ on each call. The loop finishes when the value to be passed to the block is greater than _limit_ (if _step_ is positive) or less than _limit_ (if _step_ is negative). If all the arguments are integers, the loop operates using an integer counter. If any of the arguments are floating point numbers, all are converted to floats, and the loop is executed _floor(n + n*epsilon)+ 1_ times, where _n = (limit - num)/step_. Otherwise, the loop starts at _num_, uses either the +<+ or +>+ operator to compare the counter against _limit_, and increments itself using the +++ operator. 1.step(10, 2) { |i| print i, " " } Math::E.step(Math::PI, 0.2) { |f| print f, " " } _produces:_ 1 3 5 7 9 2.71828182845905 2.91828182845905 3.11828182845905 -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com