class String def hello "world" end end String.class_eval { def world "hello" end } "a".world => "hello" "b".hello => "world"
解决方法
通过class_eval,您可以做更多动态的事情:
>> met = "hello" #=> "hello" >> String.class_eval "def #{met} ; 'hello' ; end" #=> nil >> "foo".hello #=> "hello"