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)?
In order to pass a block to a function, you need to prefix it with an ampersand.

block_or_string = case @request.path
  when '/'    then Proc.new { |response| do_stuff }
  when '/env' then @end.to_html
end

response = if block_or_string.is_a?(Proc)
  Rack::Response.new(&amp;block_or_string)
else
  Rack::Response.new(block_or_string)
end


This is pretty ugly. It might be an improvement to wrap the strings in simple procs.

block = case @request.path
  when '/'    then Proc.new { |response| do_stuff }
  when '/env' then Proc.new { @end.to_html }
end

Rack::Response.new(&amp;block)