[ruby-talk:00328] Re: binding

From: gotoken@... (GOTO Kentaro)
Date: 1999-05-19 09:41:08 UTC
List: ruby-talk #328
Hi

In message "[ruby-talk:00323] binding"
    on 99/05/18, Pros Yeboah <yeboah@tu-harburg.de> writes:
>Can someone please explain to me  the meaning  and usage of
>1)the  built-in method binding.
>2)the Kernel method  callcc.

I picked up two uses of callcc from Japanese speaking list. 
hope this helps...

#
# callcc example 0 -- infinite loop [ruby-list:13688]
#

def Loop
  callcc {|c| $c = c }
  yield
  $c.call
end

Loop { print "hello\n"; sleep 0.5 }
# end of example 0

#
# callcc example 1 -- cursor [ruby-list:13696]
#

class Cursor
  def initialize(iter)
    @iter = iter
    @cont = nil
  end

  def next
    callcc{|@cc|
      @cont.call if @cont
      @iter.call {|obj|
        callcc{|@cont| @cc.call(obj) }
      }
      @cc.call(nil)
    }
  end
end

c = Cursor.new(["deep","space","nine"].method(:each))
p obj = c.next
p obj = c.next
p obj = c.next
p obj = c.next
# end of example 1

-- gotoken

In This Thread

Prev Next