创建动态 class 和函数生成器购买 method_body 部分不起作用

Created a dyanmic class and function generator but method_body part doesnt work

class DynamicClass
  def initialize(class_name)
    @class_name = class_name
    Object.const_set class_name, Class.new
  end
  def def_method(method_name,method_body)
   Object.const_get(@class_name).send(:define_method, method_name, Proc.new{method_body} )
  end

  def call(method_name)
    puts "Hello, Your class #{@class_name} with method #{method_name} is ready. Calling: #{@class_name}.new.#{method_name}:"
    Object.const_get(@class_name).new.send(method_name)
  end
end

p 'Please enter the class name: '
class_name = gets.chomp
p 'Please enter the method name you wish to define: '
method_name = gets.chomp
p "Please enter the method's code: "
method_body = gets


n =DynamicClass.new(class_name)
n.def_method(method_name,method_body)
puts n.call(method_name)

预期输出:

Please enter the class name: User
Please enter the method name you wish to define: greet
Please enter the method's code: "Welcome from #{self.class} class. I am #{self}"

Hello, Your class User with method greet is ready. Calling: User.new.greet:
"Welcome from User class. I am <User#123456>"

实际输出

"Please enter the class name: " 用户

"Please enter the method name you wish to define: " 打招呼

"Please enter the method's code: " 欢迎来自#{self.class} class。我是#{self}

您好,您的 class 方法 greet 用户已准备就绪。呼叫:User.new.greet:

欢迎来自#{self.class} class。我是#{self}

method_body 不进行字符串插值..,我认为错误是使用 gets 方法有什么想法吗?..

你快到了!
只需执行 evaluation 即可执行字符串插值。

def def_method(method_name,method_body)
  Object.const_get(@class_name).send(:define_method, method_name, Proc.new{eval('"' + method_body + '"')} )
end