English 中文(简体)
在 RubyMotion 中解析字符串到时间
原标题:Parsing string to time in RubyMotion

我试图将一段时间的文字表达方式 分析成一个红宝石时间天体。

Normally in ruby I would do it like this:

require  time 
Time.parse( 2010-12-15T13:16:45Z )
# => 2010-12-15 13:16:45 UTC

在Rubymotion中我无法要求libs和时间。

(main)> require  time 
=> #<RuntimeError: #require is not supported in RubyMotion>
(main)>
(main)> Time.parse
=> #<NoMethodError: undefined method `parse  for Time:Class>

是否有办法要求红宝石提供的时间图书馆不必复制和重写整个代码,使其与RubyMotion兼容?

最佳回答

它不是自动的,但您可以使用 NSDate Formatter

date_formatter = NSDateFormatter.alloc.init
date_formatter.dateFormat = "yyyy-MM-dd T HH:mm:ss Z "
date = date_formatter.dateFromString "2010-12-15T13:16:45Z"
puts date.description
问题回答

当保罗的回答肯定有效的时候, 它让我毛骨悚然, 在我的代码里看到它, 所以我一直在寻找。

Matt Aimonettis MacRuby的《麦克鲁比书》有一些好东西:

当分析简单到如下程度时:

NSDate.dateWithString(<your date string here>)

NSDate.dateWithNaturalLanguageString(<all kinds of date strings>)

如果你必须有一个时间天体 从这个东西:

Time.at(NSDate.date)

< a href=> "http://bubble wrap.io//" rel="nofollow" >BubbleWrap 围绕这个添加一些漂亮的包装:

时间 时间 时间 时间 时间 时间 时间 时间 时间

The 时间 时间 时间 时间 时间 时间 时间 时间 时间 Ruby class was added a class level method to convert a iso8601 formatted string into a 时间 时间 时间 时间 时间 时间 时间 时间 时间 instance.

时间 时间 时间 时间 时间 时间 时间 时间 时间.iso8601("2012-05-31T19:41:33Z")
=> 2012-05-31 21:41:33 +0200

这将给您一个 NSDATE 实例, 简单如此 。

也许这已经固定下来了 我在RubyMotion的翻阅时间没问题

(main)> Time.parse("2016-01-21 10:33:07")
=> "2016-01-21 10:33:07 -0800"

鲁比运动4.8

$ motion --version
4.8




相关问题
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 ...

热门标签