Dynamically Create Singleton Method

P

pachl

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
 
D

dblack

Hi --

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 | (e-mail address removed)
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
 
P

pachl

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
 
V

Vincent Fourmond

pachl said:
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

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(c)
end

a = a('biniou')
p a.a # -> "biniou"

Cheers,

Vince
 
G

Gregory Seidman

}
} # 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
 
D

dblack

Hi --

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

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

Good morning :)

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


David

--
David A. Black | (e-mail address removed)
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top