Fixnum的未定义方法`new':Class(NoMethodError)

时间:2011-09-18 01:03:07

标签: ruby nomethoderror

    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)。为什么这样?我只是尝试在另一个类中使用它并且它可以工作。

谢谢!

1 个答案:

答案 0 :(得分:3)

在我看来,方法和错误没有关系,你为什么要做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"}
相关问题