我需要改变日期物体。 我发现的资源表明,这是我需要做的事,但我并不肯定与本法典有什么关系。 我怎么说? 感谢阅读。
module DateTimePatch
def getlocal
"works!"
end
end
DateTime.send(:include, DateTimePatch)
EDIT: So this is what I have
/lib/date_time.rb
class DateTime
def getlocal
"it works"
end
end
青少年
ruby-1.9.2-p0 > Time.now.getlocal
=> 2010-11-09 23:40:36 +1000
ruby-1.9.2-p0 > DateTime.now
=> Tue, 09 Nov 2010 23:40:57 +1000
ruby-1.9.2-p0 > DateTime.now.getlocal
NoMethodError: undefined method `getlocal for Tue, 09 Nov 2010 23:41:02 +1000:DateTime
EDIT:
Ok:
ruby-1.9.2-p0 > require ./lib/date_time.rb
=> true
ruby-1.9.2-p0 > DateTime.now.getlocal
NoMethodError: undefined method `now for DateTime:Class
from (irb):2
from /Users/benhartney/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>
ruby-1.9.2-p0 > DateTime.now
NoMethodError: undefined method `now for DateTime:Class
from (irb):3
from /Users/benhartney/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>
我试图在混合体中添加一个临时的<代码>now方法,以了解该错误是否仍然存在,是否确实发生。 日期:现在,在我装上 mix子之前,做的是罚款,但不能确定为什么会这样做?