2月9日 Time and Date(Ruby基础) \n 2月10日 ,使用Proc,block对象化。
20.1Time类 ,Data类
Time.now或者Time.new:获取当前时间。
相关Method:
year, month, hour, min ,sec,
wday: 一周中的第几天,返回整数。(0表示星期天)
mday: the day in a month
yday: the day in one year
zone: 时区time zone , => "CST"
Time.mktime(参数)
sleep(10) #等待多少秒
20.3 Time对象可以互相比较和运算。
> t = Time.now #=> 2018-02-11 09:39:39 +0800
t.to_s #转换为字符串
t.strftime() #客制化某种格式的字符串
20.5 本地时间
t.utc #得到一个新对象,获取国际协调时间UTC
t.localtime #得到一个新对象, 本地时间
20.6 从字符串获取时间。
获得字符串:如
"2010年12月23日下午8点17分50秒"
解析成Time对象:
用Time.prase(str),但中文的无法解析,特殊格式的也无法解析。 需要自定义Method。
例子:把中文时间,转换为Time对象?
def cparsedate(str) now = Time.now year = now.year month = now.month day = now.day hour = now.hour min = now.min sec = now.sec str.scan(/(上午|下午)?(\d+)(年|月|日|点|分|秒|时)/) do case $3 when "年" year = $2.to_i when "月" month = $2.to_i when "日" day = $2.to_i when "点" hour = $2.to_i hour += 12 if $1 == "下午"
when "时"hour = $2.to_i
when "分" min = $2.to_i when "秒" sec = $2.to_i end end return Time.mktime(year, month, day, hour, min, sec) end p cparsedate("2010年12月23日下午8点17分50秒") p cparsedate("12月23日下午8点17分50秒") p cparsedate("上午8时17分50秒") p cparsedate("8点17分50秒")
20.7 获取日期
require "date" 获取Date类需要引用date库
Date.today
相关method和Time一样。
指定生成日期使用:Date.new(2018, 1, 31) ⚠️ :和Time.mktime()区分
20.8计算日期
Date对象减法,返回天数。对象是Rational对象
<<,>>返回前/后一个月相同的日期
=> #<Date: 2018-02-11 ((2458161j,0s,0n),+0s,2299161j)>
20.11
to_time 和 to_date互相转换Time对象和Date对象。
GC:Garbage Collection ,ruby自动支持。
根据生日计算年龄。
定义一个Person类,内含初始化method, age method.
require "date" class Person attr_reader :birth_date # 通过 Person.new 获取关键参数生年月日 def initialize(birth_date) @birth_date = birth_date end # 返回某个日期的年龄。没有指定日期则返回今天的年龄。 def age(date=Date.today) # 如果是出生前则返回 -1 (错误) return -1 if date < birth_date years = date.year - birth_date.year if date.month < birth_date.month # #没满一年,所以减一年 years -= 1 elsif date.month == birth_date.month && date.day < birth_date.day # 还差不到一个月生日,所以减一年 years -= 1 end return years end end ruby = Person.new(Date.new(1993, 2, 24)) p ruby.birth_date # 生年月日 p ruby.age # 今天 p ruby.age(Date.new(2013, 2, 23)) # 20岁的前一天 19 p ruby.age(Date.new(2013, 2, 24)) # 20岁的生日 20 p ruby.age(Date.new(1988, 2, 24)) # 出生之前 -1
上午的练习,设计其他的知识点,没仔细学。
第21章,Proc 对象。
(已完成练习。)
21.1Proc类是什么?使块对象化的类。
创建和执行Proc对象:
Proc.new():典型方法
proc{...} :对proc方法指定block
Proc#call 执行block. 参数是块变量,块中最后的表达式的值返回, call(params,...) → obj
例子:
a_proc = Proc.new {|scalar, *values| values.map {|value| value*scalar } }
答案:
a_proc.call(9, 1, 2, 3) #=> [9, 18, 27] a_proc[9, 1, 2, 3] #=> [9, 18, 27] a_proc.(9, 1, 2, 3) # 仅仅隐藏了call a_proc.yield(9, 1, 2, 3) #=> [9, 18, 27]*values:代表传入的参数转化为数组。
21.11 lambda 表达式 ,简写:->(块变量){处理}
和Proc.new, proc一样,但有区别,lambda创建的Proc对象的行为更接近method
区别:
第一,参数检查严格。
a_proc = proc {|a,b| [a,b] } a_proc.call(1) #=> [1, nil] a_proc = lambda {|a,b| [a,b] } a_proc.call(1) # ArgumentError: wrong number of arguments
第二,lambda可以使用return将值从block返回。
# 或者简写 : ->(x){return x**n}
21.12 通过Proc参数接收块
&block
21.2 Proc特征
Proc对象可以作为无名函数(lambda)或方法使用,但不是单纯的将“处理”对象化,
还可以将处理的内容,本地变量的作用域等定义block时的状态一起保存下来。
闭包closure:将处理内容,变量等环境同时保存的对象。 (保存数据)
def counter c = 0 # 初始化计数器 Proc.new do # 每调用1次call方法,计数器加1 c += 1 # 返回加1后的Proc对象 end end # 创建计数器c1并计数 c1 = counter p c1.call #=> 1 p c1.call #=> 2 p c1.call #=> 3 # 创建计数器c2并计数 c2 = counter # 创建计数器c2 p c2.call #=> 1 p c2.call #=> 2 # 再次用c1计数 p c1.call #=> 4
21.3 Proc类的 instance method
prc === arg
在Proc对象作为case语句的条件时使用,通过===指定的参数只能是1个,这是语法的🚫。
prc.parameters ->[种类,参数名]
prc,lambda?
浙公网安备 33010602011771号