resumable exceptions

Discussion in 'Ruby' started by Ñëåïíåâ Âëàäèìèð, Apr 20, 2004.

  1. Hi all,

    maybe I'm touching an old problem here...I wrote a "Hello World"
    program which uses a very simple "resumable exceptions"
    infrastructure. Here we go:


    class ResourceRequest < Exception
    attr_accessor :key, :continuation

    def initialize(key,continuation)
    @key = key
    @continuation = continuation
    super("No resource found for key "+key.inspect)
    end
    end

    def get_resource(key)
    callcc {|cc| raise ResourceRequest.new(key,cc)}
    end

    def with(hash)
    begin
    yield
    rescue ResourceRequest => e
    hash.has_key?(e.key) ? e.continuation.call(hash[e.key]) : raise(e)
    end
    end


    def print_string(str)
    puts(str || get_resource("string to print"))
    end

    with( "string to print" => "Hello World!" ) do print_string(nil) end


    If a function can't find some resource, it throws a request for it,
    which is processed with scoping rules (#with calls can be nested), as
    opposed to calling a global/static resource getter somewhere. We can
    also play with the resource hash - for example, use class names as
    keys... or use allocator methods as values, de-facto giving the block
    a set of "lazily allocated" resources... or easily override the object
    a specific method call interacts with, for testing purposes...

    Is this interesting? =) I've been thinking about how to make functions
    request missing parameters from callers; this is definitely not a
    complete solution...

    Vladimir Slepnev
    Ñëåïíåâ Âëàäèìèð, Apr 20, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    5
    Views:
    29,986
    Chris Smith
    Jul 14, 2004
  2. Paul Miller
    Replies:
    3
    Views:
    1,006
    Alex Martelli
    Nov 12, 2003
  3. Replies:
    3
    Views:
    597
    Sherm Pendley
    Apr 16, 2007
  4. Lie
    Replies:
    3
    Views:
    598
  5. ilejn
    Replies:
    10
    Views:
    381
    Jon Clements
    Jan 21, 2011
Loading...

Share This Page