Ruby 特殊笔记

  • 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
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值