给定一个时间点,希望得到其他时间点
编辑: 面试啦 发布时间: 2023-04-15 00:00:28
问题:给定一个时间点,希望得到其他时间点
参考答案:
Ruby 的所有时间对象都可像数字一样用在值域中。Date和DateTime对象按天递增,而Time对象按秒递增:
require ‘date’
(Date.new(1776, 7, 2)..Date.new(1776, 7, 4)).each { |x| puts x }
# 1776-07-02
# 1776-07-03
# 1776-07-04
span = DateTime.new(1776, 7, 2, 1, 30, 15)..DateTime.new(1776, 7, 4, 7, 0, 0) span.each { |x| puts x }
# 1776-07-02T01:30:15Z
# 1776-07-03T01:30:15Z
# 1776-07-04T01:30:15Z
(Time.at(100)..Time.at(102)).each { |x| puts x }
# Wed Dec 31 19:01:40 EST 1969
# Wed Dec 31 19:01:41 EST 1969
# Wed Dec 31 19:01:42 EST 1969
Ruby 的Date类定义了step和upto两种方便的由数字使用的迭代器:
the_first = Date.new(2004, 1, 1)
the_fifth = Date.new(2004, 1, 5)
the_first.upto(the_fifth) { |x| puts x }
# 2004-01-01
# 2004-01-02
# 2004-01-03
# 2004-01-04
# 2004-01-05
参考答案:
Ruby 的所有时间对象都可像数字一样用在值域中。Date和DateTime对象按天递增,而Time对象按秒递增:
require ‘date’
(Date.new(1776, 7, 2)..Date.new(1776, 7, 4)).each { |x| puts x }
# 1776-07-02
# 1776-07-03
# 1776-07-04
span = DateTime.new(1776, 7, 2, 1, 30, 15)..DateTime.new(1776, 7, 4, 7, 0, 0) span.each { |x| puts x }
# 1776-07-02T01:30:15Z
# 1776-07-03T01:30:15Z
# 1776-07-04T01:30:15Z
(Time.at(100)..Time.at(102)).each { |x| puts x }
# Wed Dec 31 19:01:40 EST 1969
# Wed Dec 31 19:01:41 EST 1969
# Wed Dec 31 19:01:42 EST 1969
Ruby 的Date类定义了step和upto两种方便的由数字使用的迭代器:
the_first = Date.new(2004, 1, 1)
the_fifth = Date.new(2004, 1, 5)
the_first.upto(the_fifth) { |x| puts x }
# 2004-01-01
# 2004-01-02
# 2004-01-03
# 2004-01-04
# 2004-01-05
词条:ruby面试题
上一篇:Ruby如何实现动态方法调用 下一篇:介绍一下Ruby的特点
面试题库导航
- C++面试题
- JAVA面试题
- Oracle面试题
- MySQL面试题
- Linux面试题
- 网络工程师面试题
- 网管面试题
- 软件工程师面试题
- PHP面试题
- .net面试题
- 软件测试面试题
- EJB面试题
- Spring面试题
- Javascript面试题
- Python面试题
- QTP面试题
- Servlet面试题
- J2EE面试题
- Database面试题
- ASP面试题
- Unix面试题
- Loadrunner面试题
- 通信面试题
- 嵌入式面试题
- Ruby面试题
- Delphi面试题
- Mobile开发面试题
- SOA面试题
- JAVA面试题面试题
- 研发工程师面试题
- 软件架构师面试题
- 系统工程师面试题
- Android面试题
- JAVA程序员
- C#面试题