【发布时间】:2017-10-24 21:58:56
【问题描述】:
我在 Ruby 中遇到了一些关于 DateTime 的问题 我的行看起来像这样(在 .txt 文件中)
DateTime.new(1979,1,1) DateTime.new(2012,3,29)
我得到这个的函数看起来像这样
def split_line
array = line.split(' ')
@date_of_birth = array[0]
@date_of_death = array[1]
end
但是@date_of_birth 和@date_of_death 类是字符串。我怎样才能将它们作为 DateTime?
【问题讨论】:
-
您是否将行传递给函数?当前行未定义
-
你有一个字符串
"DateTime.new(1979,1,1) DateTime.new(2012,3,29)"?为什么? -
这很可能是一个“XY Problem”。为什么你的文本文件中有那一行?
标签: ruby-on-rails ruby datetime