Alle lunedì 19 novembre 2007, Alvaro Perez ha scritto:
> Hi there,
>
> something that I think should not be very difficult, but i'm struggling
> with:
>
>   def find(name, where)
>       for father in where
>          if (father.name==name) then
>              found = thing
>              break
>          else
>              find(name, father.children)
>          end
>       end
>       found
>   end
>
> I have an array with parent objects, some of them have children. I want
> to find an object with the same name and return in, otherwise nil.
>
> The problem is that, once I start searching inside the children of some
> father, and I found what I'm looking for, I break the loop inside (array
> father.children). But then the search continues in the level above. I
> would like to break the complete search. How can it be done?
>
> Thank you!!

Not tested (and not sure I understand your code correctly. What's thing?):

def find(name, where)
  where.each do |father|
    found = if father == name then thing
    else find(name, father.children)
    end
    break found if found
  end
end

If this doesn't work, you can try looking at the documentation for 
Kernel#throw and Kernel#catch.

I hope this helps

Stefano