How do I use `yield`?

Viewed 1796

I have a list:

list = ["mango", "apple", "pearl", "peach"]

and I need to use yield so that this line of code:

answer = myIndexOf(list) {|e| e == "apple"}

returns the value 1, which is the index of "apple" in the array.

I have this, but I don't understand yield.

def myIndexOf(list)  
  yield answer if block_given?  
  result = list.index(answer)  
  return answer  
end  

Can anyone shed some light on this?

3 Answers
Related