1

I am new to Ruby and don't know how to access data members within a function (syntax-wise). Here is my question. I have a function that will put values into an array of numbers, from a user given range, that are prime. The function looks like so:

#Search for primes within a range
def find_primes(starting, ending)

    #Make an empty array
    a = []

    for x in starting..ending
        if is_prime(x)
            a << x #Store results in array
        end
    end

    yield
end

The catch is that I must use the yield keyword to call another function to get the data from array 'a'. For example, I need to print out consecutive prime numbers, that are stored in 'a', and I have this code to do this (except I don't know how to get at the values of 'a' from the code below. This is called closure, I believe)

find_primes(0,50) do 

    i = 0

    while i < a.size - 1
        print "[#{a[i]} #{a[i+1]} "
    end
end

This is all very new to me and I can't find a good source on how to do what I am tasked to do. Thank you in advance

1
  • 1
    good to see - Prime also. Commented Feb 25, 2014 at 18:18

2 Answers 2

1

You have a whole wrong idea. Using for in Ruby is code smell. This is how you do it if you want the iterator built in:

def find_primes(starting, ending, &pr)
  (starting..ending).select{|x| is_prime(x)}.each(&pr)
end

find_primes(1, 10){|x| puts x}

But the code above lacks flexibility. For flexibility, you should just make it return an array (or an enumerator). Then, you can use different iterators:

def find_primes(starting, ending)
  (starting..ending).select{|x| is_prime(x)}
end

find_primes(1, 10).each{|x| puts x}
find_primes(1, 10).each_cons(2){|x, y| puts "#{x} #{y}"}
Sign up to request clarification or add additional context in comments.

3 Comments

I believe you have the wrong idea, I need to pass in a block of code into the function 'find_primes' implicitly to manipulate the data. Even if this is inefficient, its the way I was told to do this
What do you mean by "implicitly pass in a block of code"?
I take back my previous comment, I think you did in fact answer this but I didn't recognize it. Ruby syntax problems.
0

If you want to return the variable a to the block you should use yield a instead of yield, then you can use find_primes like so:

require 'prime'

def find_primes(from, to)
  yield (from..to).select { |n| Prime.prime? n }
end

find_primes(0, 10) do |primes|
  p primes # => [2, 3, 5, 7]
end

Having said that, you can just use find_primes to return the array like a normal method if you want. You don't have to use yield with blocks. That is:

def find_primes(from, to)
  (from..to).select { |n| Prime.prime? n }
end

primes = find_primes(0,10)
# Do something with primes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.