Ruby 日期方法问题
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 >
为什么要这样做?我查看了文档, .mon 或 .month 是有效的方法。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mon
和month
是Date
实例的有效方法。您在Date
上调用它们 - 即类。mon
andmonth
are valid methods ofDate
instances. You're calling them onDate
- that is, a class.我建议您看一下类方法和实例方法之间的区别。
strptime
属于前者,mon
属于后者。I suggest you take a look at the difference between class and instance methods.
strptime
belongs to the former,mon
to the latter.