On 01/09/05, Philipp Huber <huber.philipp / gmail.com> wrote:
> hello!
> 
> first of all, i'm new to ruby and this list.
> 
> my question is, i have a class "Queue" which implements a queue as
> linked list of "Item" objects. i'd like to implement an each method,
> so i can do something like
> 
> queue.each do |item|
>    item.do_somethng
> end
> 
> any help would be appreciated!
> thanks,
> philipp
> 
> 

It is a bit hard to help you, if we don't know your classes. But lets
say you have a class
class QueueNode
  attr_accessor :next
  ...
end 

and a class Queue with a variable @root holding the first entry of the
queue you could implement each as

class Queue
  ...

  def each(&block)
    node = @root
    begin
      block[node] 
    end while node = node.next
  end
end

or you could use yield
  
class Queue
  ...

  def each
    node = @root
    begin
      yield node
    end while node = node.next
  end
end

that depends on personal preference.

hope to help,

Brian
-- 
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/