我需要用户输入一个新类的名称,用于创建新类。我的代码如下:
这也不起作用。
我要求用户输入一个名称来创建一个新的类。我的代码是:
puts "enter the name for a new class that you want to create"
nameofclass = gets.chomp
nameofclass = Class.new
为什么这个不起作用?
另外,我想要求用户输入我想要添加到该类中的方法的名称。我的代码是:
puts "enter the name for a new method that you want to add to that class"
nameofmethod = gets.chomp
nameofclass.class_eval do
def nameofmethod
p "whatever"
end
end
这也不起作用。
eval
的方式不太好。然而,这似乎是一个非常实验性的案例;你永远不想做这样的事情。eval
的好处在于对于初学者来说很容易理解,并且作为元编程的入门也可以正常工作。 - troelskn