Printing out prime numbers from an array given a max number Is there a shorter or maybe a cleaner way of doing:


  Using your is_prime? method, write a new method, primes that takes a (non-negative, integer) number max and returns an array of all prime numbers less than max.


def is_prime?(max)
  i = 2
  while i &lt; max
    is_divisible = ((max % i) == 0)
    if is_divisible
      # divisor found; stop and return false!
      return false
    end

    i += 1
  end

  # no divisors found
  true
end
def primes(max)
  primes_arr = []

  i = 2
  while i &lt; max
    if is_prime?(i)
      # i is prime; add it to the array
      primes_arr &lt;&lt; i
    end

    i += 1
  end

  # return primes_arr
  primes_arr
end
Here it is using Prime#prime?:


  Suppose I want to get all the prime numbers less than 9


require 'prime'

a = (1..12).to_a
p a.select{|e| e.prime? and e &lt; 9 }
# &gt;&gt; [2, 3, 5, 7]


Here is a method

require 'prime'

def prime_below_max(a,max)
   a.select{|e| e.prime? and e &lt; max }
end

ary = (1..12).to_a
p prime_below_max(ary,9)
# &gt;&gt; [2, 3, 5, 7]