Hi --

On Sat, 9 Dec 2006, pachl wrote:

> I'm not sure how to explain what I'm looking for so I will show you:
>
> # this works, but not quite the functionality I need
>
> def simulate_cgi_upload
>  class << cgi_file = Tempfile.new('simulate-cgi')
>    def content_type() 'image/jpeg' end
>  end
>  cgi_file
> end
>
>
> # this does NOT work, but has the functionality I need
> # i.e.: dynamically defining content_type's return value using
> # the argument from simulate_cgi_upload method.
>
> def simulate_cgi_upload(mime_type = 'image/jpeg')
>  class << cgi_file = Tempfile.new('simulate-cgi')
>    def content_type() mime_type end
>  end
>  cgi_file
> end

Try this (or something like it in case I've mistyped anything):

   def simulate_cgi_upload(mime_type = 'image/jpeg')
     cgi_file = Tempfile.new('simulate-cgi')
     (class << cgi_file; self; end).class_eval do
       define_method("content_type") { mime_type }
     end
     cgi_file
   end


David

-- 
                   David A. Black | dblack / wobblini.net
Author of "Ruby for Rails"   [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog)        [2] | Co-director, Ruby Central, Inc.   [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com    | [4] http://www.rubycentral.org