简体   繁体   English

Fixnum:Class的未定义方法“ new”(NoMethodError)

[英]undefined method `new' for Fixnum:Class (NoMethodError)

    class Fixnum
      def repeat
        for i in 1..self.to_i
          yield
        end
      end
    end

    z = Fixnum.new 4

The program above is giving undefined method new for Fixnum:Class (NoMethodError) . 上面的程序undefined method new for Fixnum:Class (NoMethodError)提供了undefined method new for Fixnum:Class (NoMethodError) Why so? 为什么这样? I just tried using it in another class and it works. 我只是尝试在另一个类中使用它,并且可以工作。

Thanks! 谢谢!

It looks to me that the method and the error have no relation, why are you doing z = Fixnum.new 4? 在我看来,该方法与错误无关,为什么要执行z = Fixnum.new 4?

The method should be used like: 该方法应按如下方式使用:

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"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM