Dynamically Create Singleton Method

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

-pachl

Hi –

On Sat, 9 Dec 2006, pachl wrote:

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

On Dec 9, 6:26 am, [email protected] wrote:

end
endTry 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

Excactly what I was looking for; compact and fairly elegant.

Thanks for the very quick response David and good morning to you.

-pachl

pachl wrote:

end
Well, you need a closure, which is not the case in what you would
like. The following code seems to do what you’re looking for, just adapt
it:

def a(a)
b = Object.new
c = Module.new
c.send(:define_method,:a) do # needed as define_method is private
return a
end
b.extend©
end

a = a(‘biniou’)
p a.a # -> “biniou”

Cheers,

Vince

On Sat, Dec 09, 2006 at 10:10:09PM +0900, 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

For what I think you’re trying to do, try this:

def simulate_cgi_upload(mime_type = ‘image/jpeg’)
cgi_file = Tempfile.new(‘simulate-cgi’)
(class << cgi_file; self; end).send(:attr_accessor, :content_type)
cgi_file.content_type = mime_type
cgi_file
end

} -pachl
–Greg

Hi –

On Sat, 9 Dec 2006, pachl wrote:

cgi_file
cgi_file
Excactly what I was looking for; compact and fairly elegant.

Thanks for the very quick response David and good morning to you.

Good morning :slight_smile:

It would be more elegant if we had Kernel#singleton_class – then you
wouldn’t have to do the little workaround. Fingers crossed…

David