开发者

Ruby Date Methods Issue

ruby-1.9.2-p290 :001 > require 'date' => true 
ruby-1.9.2-p290 :002 > date = '01/23/2011'=> "01/23/2011" 
ruby-1.9.2-p290 :003 > Date.strptime(date, "%m/%d/%Y") => 
#<Date: 2011-01-23 (4911169/2,0,2299161)> 
ruby-1.9.2-p290 :004 > Date.mon
NoMethodError: undefined method `mon' for Date:Class
    from (irb):4
    from /Users/noahclark/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
ruby-1.开发者_运维知识库9.2-p290 :005 > 

Why does it do this? I've looked in the documentation and .mon or .month is a valid method.

Thanks in advance.


mon and month are valid methods of Date instances. You're calling them on Date - that is, a class.


I suggest you take a look at the difference between class and instance methods.

strptime belongs to the former, mon to the latter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜