日期数字的between用法

首先,我们来看下,判断某个时间是否处于给定的时间段。

 

    yesterday = Time.now - (3600 * 24)
    tomorrow = Time.now + (3600 * 24)
    today = Time.now

    puts today.between? yesterday, tomorrow  # => true
    puts today.between? tomorrow, yesterday  # => false
    puts today.between? tomorrow, tomorrow   # => false

 

    t = Time.now
    time = "9am-5pm".match(/(\w+)-(\w+)/).captures.map {|x| Time.parse(x)}

    puts "9am-5pm" if t.between? time[0], time[1]  # => 9am-5pm
    puts "9am-5pm" if t.between? *time  # => 9am-5pm

 

接着,再来看下,某个数值是否处于某个区间。

 

    num = 3
    puts num.between? 1, 4  # => true
    puts (1..4) === num     # => true

 

你可能感兴趣的:(between)