Ruby 1.9.3: I am trying to implement a method which takes as argument a class, a symbol and a proc and defines (eventually overwriting) a new instance method for that class, but not a class method so that this test should pass:
require 'test/unit'
include Test::Unit::Assertions
class String
def my_method
'my_method'
end
end
def define_instance_method(klass, method, &block)
# klass.send :define_method, method, &block
# ...
end
define_instance_method(Object, :my_method) { 'define_instance_method my_method' }
define_instance_method(String, :my_method) { 'define_instance_method my_method' }
assert Object.new.my_method == 'define_instance_method my_method'
assert_raise(NoMethodError) { Object.my_method }
assert String.new.my_method == 'define_instance_method my_method'
assert_raise(NoMethodError) { String.my_method }
I did many tryings (define_method
, define_singleton_method
, class_eval
, instance_eval
, instance_exec
, module_eval
...) but with no success; do you have any ideas?