Ruby on rails 如何在Ruby中快速将日期转换为秒进行比较

Ruby on rails 如何在Ruby中快速将日期转换为秒进行比较,ruby-on-rails,ruby,date,datetime,Ruby On Rails,Ruby,Date,Datetime,我正在编写一个方法来返回用户的秒数。我有很多我不理解的错误。内容如下: def in_seconds current = Time.now bday = Date.new(year, month, day) # this is the birthday of the user age = (current - bday).to_s return "You are #{age} years old." end John = Age.new(1985, 04, 27

我正在编写一个方法来返回用户的秒数。我有很多我不理解的错误。内容如下:

def in_seconds
    current = Time.now
    bday = Date.new(year, month, day) # this is the birthday of the user
    age = (current - bday).to_s
    return "You are #{age} years old."
end
John = Age.new(1985, 04, 27) 
# sets @age to 910,993,128 seconds

Angela = Age.new(1991, 03, 15)
# sets @age to 725,405,928 seconds
require 'time'

class Age 

    def initialize(year, month, day)
    # the parameters are assumed to be integers
        @age = Time.parse("#{year}-#{month}-#{day}").to_i
    end

    def in_sec
        return Time.now.to_i - @age
    end

end
eval:1:eval:1:未初始化的常量日期名称错误

我已经创建了一个class Age
def in_seconds
    current = Time.now
    bday = Date.new(year, month, day) # this is the birthday of the user
    age = (current - bday).to_s
    return "You are #{age} years old."
end
John = Age.new(1985, 04, 27) 
# sets @age to 910,993,128 seconds

Angela = Age.new(1991, 03, 15)
# sets @age to 725,405,928 seconds
require 'time'

class Age 

    def initialize(year, month, day)
    # the parameters are assumed to be integers
        @age = Time.parse("#{year}-#{month}-#{day}").to_i
    end

    def in_sec
        return Time.now.to_i - @age
    end

end
我正在考虑修改initialize方法以包含参数YYYY、MM、DD,以便为每个用户保留一个运行的生日日期,如下所示:

def in_seconds
    current = Time.now
    bday = Date.new(year, month, day) # this is the birthday of the user
    age = (current - bday).to_s
    return "You are #{age} years old."
end
John = Age.new(1985, 04, 27) 
# sets @age to 910,993,128 seconds

Angela = Age.new(1991, 03, 15)
# sets @age to 725,405,928 seconds
require 'time'

class Age 

    def initialize(year, month, day)
    # the parameters are assumed to be integers
        @age = Time.parse("#{year}-#{month}-#{day}").to_i
    end

    def in_sec
        return Time.now.to_i - @age
    end

end

我最大的问题是,我似乎无法摆脱上面的错误。Ruby一直否认存在未初始化常量这一事实。什么常数?经过几个小时的研究,我得出了一个完全空白的结论。我怎样才能纠正这个错误?

我不确定你能不能从时间中减去日期。我建议使用Time.new来获取bday变量。这也将消除您的错误-@MarkThomas关于需要该库的说法是正确的

现在还不清楚为什么年龄需要是日期或时间的一个子类,因为要做你想做的事情。如果将此方法添加到任何对象,它将计算从日期到现在的时间(以秒为单位):

def in_seconds(year, month, day)
  back_then = Time.new(year, month, day)
  seconds_since_then = Time.now - back_then
end

由于史蒂夫·罗利(Steve Rowley)的建议,我无法将日期对象与时间对象进行比较,因此我找到了一种解决问题的方法。我决定只使用时间对象将年龄转换为整数,如下所示:

def in_seconds
    current = Time.now
    bday = Date.new(year, month, day) # this is the birthday of the user
    age = (current - bday).to_s
    return "You are #{age} years old."
end
John = Age.new(1985, 04, 27) 
# sets @age to 910,993,128 seconds

Angela = Age.new(1991, 03, 15)
# sets @age to 725,405,928 seconds
require 'time'

class Age 

    def initialize(year, month, day)
    # the parameters are assumed to be integers
        @age = Time.parse("#{year}-#{month}-#{day}").to_i
    end

    def in_sec
        return Time.now.to_i - @age
    end

end
到目前为止,我没有任何问题。看起来最大的问题来自于使用日期模块

date.to_time.to_i

将它转换为1970年1月1日之后的秒数

我真的在尝试用纪元以来的秒数表示两个日期。第一个日期是用户的生日,第二个日期是当前时间。它们都是整数,所以我不明白为什么这么难。有没有想过一起解决日期和时间兼容性问题?时间在内部存储为自纪元以来的秒数。我不明白为什么这里需要日期库。如果您的方法将current定义为Time.now,将bday定义为Time.newyear,month date,那么current-bday将是您的年龄(以秒为单位),就这么简单。我会在答案中举个例子。