红宝石 |范围覆盖?()函数
cover?()是 Ruby 中的一个内置方法,如果给定对象在给定范围内,则返回布尔值 true,否则返回 false。对象可以是元素或范围。
Syntax: range1.cover?(obj)
Parameters: The function accepts an object which is to be checked for.
Return Value: It returns a boolean value true if the given object lies within the given range, else it returns false.
示例 1 :
# Ruby program for cover? method in Range
# Initialize range
range1 = (0..10)
# Prints if lies or not
puts range1.cover?(6)
puts range1.cov
输出:
true
false
示例 2 :
# Ruby program for cover? method in Range
# Initialize range
range1 = (0..5)
# Prints if lies or not
puts range1.cover?((8))
puts range1.cover?((18))
输出:
false
false