红宝石 |数字非零?函数
nonzero?()是 Ruby 中的一个内置方法,如果数字非零,则返回 self,否则返回 nil。
Syntax: num.nonzero?()
Parameters: The function needs a number which is to be checked.
Return Value: It returns self or nil.
示例 1 :
# Ruby program for nonzero?
# method in Numeric
# Initialize a number
num1 = 12
# Prints if nonzero or not
puts num1.nonzero?()
输出:
12
示例 2 :
# Ruby program for nonzero?
# method in Numeric
# Initialize a number
num1 = 0
# Prints if nonzero or not
puts num1.nonzero?()
输出: