Skip to main content
2 of 2
ruby version added to info
ecoologic
  • 785
  • 2
  • 10
  • 19

how to pass a block to a function as a parameter

I'd like to refactor the following code:

case @request.path
when '/'
  Rack::Response.new do |response|
    do_stuff
  end
when '/env' then Rack::Response.new(@env.to_html)
end

And extract Rack::Response.new, something like the following:

Rack::Response.new case @request.path
when '/'    then Proc.new {|response| do_stuff }
when '/env' then @env.to_html
end

But I get

stringable or iterable required

What's the correct implementation (Ruby 1.9.2)?

ecoologic
  • 785
  • 2
  • 10
  • 19