Ruby 中的日期时间处理、反射机制、Tk 工具包及元编程
1. 日期和时间处理
在 Ruby 中,日期和时间的处理非常方便。可以对时间进行加减操作,例如:
# Add three hours (60*60*3)
stop + 60*60*3 # => Tue Jan 30 07:13:00 -0700 2007
# Add a day (60*60*24)
stop + 60*60*24 # => Wed Jan 31 04:13:00 -0700 2007
1.1 Date 类的使用
要使用 Date 类,首先需要引入它:
require 'date'
创建 Date 对象可以使用 new 方法或其同义词 civil :
date = Date.new( 2006, 11, 8 )
date.to_s # => "2006-11-08"
还可以使用 today 类方法创建基于当前日期的对象:
today = Date.today
today.to_s # => "2007-01-30"
可以使用表达式替换来以用户指定的格式显
超级会员免费看
订阅专栏 解锁全文
12

被折叠的 条评论
为什么被折叠?



