English 中文(简体)
我如何利用这种组合来改变日期物体?
原标题:How do I use this mixin to alter the DateTime object?
  • 时间:2010-11-09 06:47:28
  •  标签:
  • ruby

我需要改变日期物体。 我发现的资源表明,这是我需要做的事,但我并不肯定与本法典有什么关系。 我怎么说? 感谢阅读。

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子之前,做的是罚款,但不能确定为什么会这样做?

问题回答

因此,请在<代码>/code>类别中添加一种方法? Try:

class DateTime
  def get_local
    "works!"
  end
end

不妨请您通过<代码>查询。 页: 1

irb(main):002:0> DateTime.now.getlocal
NoMethodError: undefined method `getlocal  for #
        from (irb):2
        from d:/Ruby/bin/irb.bat:20:in ` 
irb(main):003:0> require  ./lib/date_time.rb 
=> true
irb(main):004:0> DateTime.now.getlocal
=> "it works"

UPD: and requiredate , in order to use Mouy s Datetime category.





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签