Is it possible to interpolate in a method name? I'm trying to make this work def "cancel_#{appointment.id}"
but having no luck. thanks
Use define_method
.
define_method "cancel_#{appointment.id}" do
# your method body
end
Using define_method
is best, but one could instead use the version of Module#class_eval which takes a string as an argument:
class C
end
x = 'cat'
C.class_eval "def #{x}; 'meow'; end"
C.instance_methods.include?(:cat)
#=> true
C.new.cat
#=> 'meow'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.