简体   繁体   English

Date.new的参数错误

[英]Argument Error for Date.new

I wrote this code: 我写了这段代码:

[1,2,3,4].each do |f|
    @date = Date.new(2014,f * 3,1)
    @begin = @date.beginning_of_quarter
    @end = @date.end_of_quarter

    puts  f + " Quartal von " + @begin + " bis " + @end
end

Somehow i get the error: 我以某种方式得到错误:

 DateTest.rb:2:in `initialize': wrong number of arguments (3 for 0) (ArgumentError)
    from DateTest.rb:2:in `new'
    from DateTest.rb:2:in `block in <main>'
    from DateTest.rb:1:in `each'
    from DateTest.rb:1:in `<main>'

What did i wrong? 我怎么了 Thanks! 谢谢!

You passed three arguments to the original Date.new , which does not take any argument. 您向原始Date.new传递了三个参数,该参数没有任何参数。 The one that takes arguments is the one overwritten by the "date" library. 接受参数的是被"date"库覆盖的参数。 In order to use it, you need to do 为了使用它,您需要做

require "date"

use DateTime instead of Date to initialize it with the parameters. 使用DateTime而不是Date来使用参数对其进行初始化。 Check the API. 检查API。 Date & DateTime 日期日期时间

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

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