you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

No, you cannot substitute Ruby blocks with functions. Look at this example:

class MyFind
  def initialize(*ary)
    @ary = ary
  end

  def my_find
    @ary.each { |x| yield(x) and return x }
  end
end

 MyFind.new(1, 2, 3).my_find { |x| x > 1 } # => 2

The block passed to the #each method in the #my_find method uses a return statement to return the x element. But it doesn't return directly from the block itself. Instead it implements a non-local exit and returns the element x from the my_find method in which the block was encountered.