Calls block once for each element of an array, passing that element as a parameter

Posted by Puru puru rin.. on Stack Overflow See other posts from Stack Overflow or by Puru puru rin..
Published on 2010-03-13T11:33:40Z Indexed on 2010/03/13 11:35 UTC
Read the original article Hit count: 158

Filed under:
|
|

Hello,

I have some difficulties for using Ruby block, passing in a method. As in the following case, I would like to display each element of @array, from Box instance (using .each method):

class Box
  def initialize
    @array = [:foo, :bar]
  end

  def each(&block)
    # well, hm..
  end
end

a = Box.new
a.each { |element| puts element }

Thanks for any help.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about block