- Time类中的+,-等运算符,精确到秒,不能进行微妙级别的运算
a = Time.at(02).utc # => 1970-01-01 00:00:20 UTC
b = a + 2 # => 1970-01-01 00:41:42 UTC
a = Time.at(02,500).utc # => 1970-01-01 00:00:20 UTC
b = a + 2500 # => 1970-01-01 00:41:42 UTC
- puts "a" if 0 # a
- 浮点数的类. Float与C语言的double相同,其精度要看环境而定。浮动数采用近似表示,例如0.1可能会存储为0.099999999999999…。
puts 0.3-0.2==0.1 #false
puts (0.3-0.2).round(3)==0.1.round(3) #true
round四舍五入至小数点后第n位
- Size, length and count's difference
For arrays and hashes size is an alias for length. They are synonyms and do exactly the same thing.
count is more versatile - it can take an element or predicate and count only those items that match.
> [1,2,3].count{|x| x > 2 }
=> 1