[ruby-talk:02379] Re: Iterator into array

From: matz@... (Yukihiro Matsumoto)
Date: 2000-04-07 17:59:48 UTC
List: ruby-talk #2379
Hi,

In message "[ruby-talk:02376] Iterator into array"
    on 00/04/07, Dave Thomas <Dave@thomases.com> writes:

|Dave's stupid question of the week:
|
|Given an iterator method, how can I get the results expressed as an
|array? I could do:
|
|  result = []
|  iteratorMethod { |i| result << i }
|
|But this seems ugly - there must be a more functional way of doing this. Any
|ideas? 

I think it may be uglier, but for the idea:

  def iterated_values(method, *args)
    result = []
    method.call(*args) do |x|
      result.push(x)
    end
    result
  end

  p iterated_values([1,2].method(:each))

							matz.

In This Thread