class Fixnum
def repeat
for i in 1..self.to_i
yield
end
end
end
z = Fixnum.new 4上面的程序给出了undefined method new for Fixnum:Class (NoMethodError)。为什么会这样呢?我只是尝试在另一个类中使用它,它可以工作。
谢谢!
发布于 2011-09-18 09:09:43
在我看来,方法和错误没有关系,为什么你要做z= Fixnum.new 4?
该方法应按如下方式使用:
class Fixnum
def repeat
for i in 1..self.to_i
yield
end
end
end
5.repeat{puts "hi"}
#or maybe?
z = 3
z.repeat{puts "bye"}https://stackoverflow.com/questions/7458849
复制相似问题